Encryption in TripleDES (3DES)

0 votes
asked Jul 19, 2013 by thinky (190 points)
edited Jul 19, 2013

Using Rebex Encryption Methods: How may I produce the same encrypted output as the following one produced by BouncyCastle:

 Public Sub encryptFile(ByVal recipientCert As X509Certificate, ByVal path As String)
    Dim mailBytes() As Byte = File.ReadAllBytes("C:\somedir\message.eml")
    Dim keysize As Int16 = 128

    Dim input As New CmsProcessableByteArray(mailBytes)
    Dim generator As New CmsEnvelopedDataGenerator()
    generator.AddKeyTransRecipient(DotNetUtilities.FromX509Certificate(recipientCert))
    Dim encrypted As CmsEnvelopedData = generator.Generate(input, PkcsObjectIdentifiers.DesEde3Cbc.Id, keysize)

    File.WriteAllBytes(path, encrypted.GetEncoded())

End Sub

1 Answer

0 votes
answered Jul 19, 2013 by Lukas Pokorny (123,150 points)
edited Jul 19, 2013

Use EnvelopedData object from Rebex.Security.Cryptography.Pkcs namespace (part of Rebex.Common.dll). Sample code:

Public Sub encryptFile(ByVal recipientCert As X509Certificate, ByVal path As String)
    Dim mailBytes() As Byte = File.ReadAllBytes("C:\somedir\message.eml")
    Dim keysize As Int16 = 128

    Dim content As New ContentInfo(mailBytes)
    Dim encrypted As New EnvelopedData(content, "1.2.840.113549.3.7", keySize) //  DesEde3Cbc
    encrypted.RecipientInfos.Add(new KeyTransRecipientInfo(recipientCert))
    encrypted.Encrypt()

    File.WriteAllBytes(path, encrypted.Encode())

End Sub
...