有没有办法使用 Sun 的 keytool 程序生成适合加密的 128 位密钥对?似乎http://java.sun.com/javase/6/docs/technotes/guides/security/StandardNames.html#KeyPairGenerator中提供的算法要么不受支持,要么不允许短于 512 位的密钥。
密钥对将与 ff 一起使用。代码片段:
Security.addProvider(new BouncyCastleProvider());
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream keyStoreSource = new FileInputStream("keystore");
try {
keyStore.load(keyStoreSource, "password".toCharArray());
} finally {
keyStoreSource.close();
}
String alias = (String) keyStore.aliases().nextElement();
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, "password".toCharArray());
X509Certificate certificate = (X509Certificate) keyStore.getCertificate(alias);
CMSEnvelopedDataStreamGenerator generator = new CMSEnvelopedDataStreamGenerator();
generator.addKeyTransRecipient(certificate);
OutputStream output2 = generator.open(output, CMSEnvelopedDataGenerator.AES128_CBC, BouncyCastleProvider.PROVIDER_NAME);
try {
IOUtils.copy(input, output2);
} finally {
output2.close();
output.close();
}
其中输出是一些将保存加密数据的 OutputStream,输入是一些将读取明文数据的 InputStream。