【发布时间】:2018-10-06 11:48:52
【问题描述】:
我在这里按照示例进行操作:http://www.baeldung.com/java-bouncy-castle
我有几个问题:
public static byte[] encryptData(byte[] data,
X509Certificate encryptionCertificate)
throws CertificateEncodingException, CMSException, IOException {
byte[] encryptedData = null;
if (null != data && null != encryptionCertificate) {
CMSEnvelopedDataGenerator cmsEnvelopedDataGenerator
= new CMSEnvelopedDataGenerator();
JceKeyTransRecipientInfoGenerator jceKey
= new JceKeyTransRecipientInfoGenerator(encryptionCertificate);
cmsEnvelopedDataGenerator.addRecipientInfoGenerator(transKeyGen);
CMSTypedData msg = new CMSProcessableByteArray(data);
OutputEncryptor encryptor
= new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC)
.setProvider("BC").build();
CMSEnvelopedData cmsEnvelopedData = cmsEnvelopedDataGenerator
.generate(msg,encryptor);
encryptedData = cmsEnvelopedData.getEncoded();
}
return encryptedData;
}
将此应用到我的真实世界场景中,我只有一个 RSA 公钥用于收件人,而不是整个 X509Certificate。我戳了一下,但我不知道我怎么能做到这一点。有可能吗?
另一件事是我看到 JceCMSEncryptorBuilder 需要一个 ASN1ObjectIdentifier。我们目前正在做这样的事情:
KeyGenerator cryptKeyGenerator = KeyGenerator.getInstance("AES", "BC");
cryptKeyGenerator.init(256);
Key encryptionKey = cryptKeyGenerator.generateKey();
Cipher symmetricCipher = Cipher.getInstance("AES/CTS/NoPadding", "BC");
symmetricCipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(ivBytes));
在 CMSAlgorithm 类中,我看不到任何 CTS 选项。我错过了什么还是有办法仍然使用 CTS?
【问题讨论】:
标签: java encryption bouncycastle public-key-encryption