问题描述
我正在尝试部分成功地使用c#从Java复制示例
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();
}
到目前为止,我已经尝试过
使用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;
}
错误堆栈跟踪
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,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
使用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
}
错误堆栈跟踪
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
我必须使其以某种方式工作,以便我可以使用c#代码加密文件并使用java解密,反之亦然。
我注意到,如果我使用Aes256CBC在C#中加密文件,我可以在Java中对其进行解密,那怎么可能?这是否意味着我实施的加密错误?
那么,什么可以使这项工作可行?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)