0 votes
by (190 points)
edited

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
by (144k points)
edited

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
...