I'm trying to replicate example from Java in c# with partial success
CMSEnvelopedDataStreamGenerator gen = new CMSEnvelopedDataStreamGenerator();
// NOTE: Uses the RECEIVER's PUBLIC encryption key
gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(remoteEncryptionCert, rsaesOaepIdentifier()));
OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_GCM).setProvider(BC).build();
try (FileOutputStream fileStream = new FileOutputStream(OUTPUT_FILE); OutputStream encryptingOutputStream = gen.open(fileStream, encryptor)) {
//
// write file
//
encryptingOutputStream.flush();
}
I've tried so far
Using System.Security.Cryptography.Pkcs
public byte[] Encrypt(byte[] plainBytes, X509Certificate2 recipientCert)
{
// create ContentInfo
ContentInfo plainContent = new ContentInfo(plainBytes);
// EnvelopedCms represents encrypted data
Oid encryptAlgoOid = new Oid("2.16.840.1.101.3.4.1.46"); // AES-256-GCM,
//Oid encryptAlgoOid = new Oid("2.16.840.1.101.3.4.1.42"); // AES-256-CBC
EnvelopedCms encryptedData = new EnvelopedCms(plainContent, new AlgorithmIdentifier(encryptAlgoOid));
// add a recipient
CmsRecipient recipient = new CmsRecipient(recipientCert);
// encrypt data with public key of recipient
encryptedData.Encrypt(recipient); //Throws "Unknown cryptographic algorithm."
// create PKCS #7 byte array
byte[] encryptedBytes = encryptedData.Encode();
// return encrypted data
return encryptedBytes;
}
error stack trace
Unknown cryptographic algorithm.
at Internal.Cryptography.Pal.Windows.PkcsPalWindows.EncodeHelpers.CreateCryptMsgHandleToEncode(CmsRecipientCollection recipients, Oid innerContentType, AlgorithmIdentifier contentEncryptionAlgorithm, X509Certificate2Collection originatorCerts, CryptographicAttributeObjectCollection unprotectedAttributes)
at Internal.Cryptography.Pal.Windows.PkcsPalWindows.Encrypt(CmsRecipientCollection recipients, ContentInfo contentInfo, AlgorithmIdentifier contentEncryptionAlgorithm, X509Certificate2Collection originatorCerts, CryptographicAttributeObjectCollection unprotectedAttributes)
at System.Security.Cryptography.Pkcs.EnvelopedCms.Encrypt(CmsRecipientCollection recipients)
at System.Security.Cryptography.Pkcs.EnvelopedCms.Encrypt(CmsRecipient recipient)
at ConsoleApp1.Program.Encrypt() in Program.cs:line 91
Using Org.BouncyCastle.Cms
public byte[] Encrypt(X509Certificate2 recipientCert)
{
// file stream
FileStream fileEncrypted = new FileStream(pathToFile)
CmsEnvelopedDataStreamGenerator gen = new CmsEnvelopedDataStreamGenerator();
gen.AddKeyTransRecipient(recipientCert);
var outEncryptedStream = gen.Open(fileEncrypted, "2.16.840.1.101.3.4.1.46");
// Throws "KeyGenerator 2.16.840.1.101.3.4.1.46 not recognised." CmsEnvelopedDataGenerator doesn't
// have named constant for aes256gcm
return outEncryptedStream
}
error stack trace
KeyGenerator 2.16.840.1.101.3.4.1.46 not recognised.
at Org.BouncyCastle.Security.GeneratorUtilities.GetKeyGenerator(String algorithm)
at Org.BouncyCastle.Cms.CmsEnvelopedDataStreamGenerator.Open(Stream outStream, String encryptionOid)
at ConsoleApp1.Program.Encrypt() in Program.cs:line 128
I have to make it work in a way so i can encrypt files with c# code and decrypt with java and vice versa.
What i noticed that i if i encrypt file in c# using Aes256CBC i can decrypt it in java, hows that possible? Does that mean that i implemented encryption wrong?
So what are mine options to make this work?