提问人:user_8275 提问时间:10/20/2023 最后编辑:user_8275 更新时间:10/25/2023 访问量:98
充气城堡图书馆支持从AES_CBC加密切换到AES_CFB加密
Bouncy Castle Library Support to Switch from AES_CBC to AES_CFB encryption
问:
我正在使用一个库,该库使用使用AES_CFB加密模式的 android 项目中的充气城堡库对数据进行加密。早些时候,这个库使用 CBC,在我的 android 应用程序中,我能够使用以下代码解密密钥。
private fun getPrivateKey(certObject: PKCS8EncryptedPrivateKeyInfo, keyPassPhrase: String): PrivateKey? {
val bouncyCastleProvider = BouncyCastleProvider()
val decryptionProvider: InputDecryptorProvider =
JceOpenSSLPKCS8DecryptorProviderBuilder().setProvider(bouncyCastleProvider)
.build(keyPassPhrase.toCharArray())
val info = certObject.decryptPrivateKeyInfo(decryptionProvider)
val converter = JcaPEMKeyConverter()
return converter.getPrivateKey(info)
}
现在,该库已将加密模式更改为AES_CFB。因此,上面的代码在这一行解密时给出了以下异常
val info = certObject.decryptPrivateKeyInfo(decryptionProvider)
例外:
Method threw 'org.bouncycastle.pkcs.PKCSException' exception.
unable to read encrypted data: no key size for algorithm:2.16.840.1.101.3.4.1.44
有人可以帮忙如何使用充气城堡从 CBC 切换到 CFB 吗?
在此处添加示例代码
public class Application {
private final static String passphrase = "password";
public static void main(String[] args) {
try {
BouncyCastleProvider securityProvider = new BouncyCastleProvider();
Security.addProvider(securityProvider);
PKCS8EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = getPrivateKeyInfo();
System.out.println("Private key algorithm encrypted: " + encryptedPrivateKeyInfo.getEncryptionAlgorithm().getAlgorithm());
InputDecryptorProvider decryptionProvider = new JceOpenSSLPKCS8DecryptorProviderBuilder()
.setProvider(securityProvider)
.build(passphrase.toCharArray());
PrivateKeyInfo privateKeyInfo = encryptedPrivateKeyInfo.decryptPrivateKeyInfo(decryptionProvider);
System.out.println("Private key algorithm decrypted: " + privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm());
}catch (Exception e){
System.out.println(e);
} }
private static PKCS8EncryptedPrivateKeyInfo getPrivateKeyInfo() throws IOException {
InputStream privateKeyInputStream = new FileInputStream("src/main/resources/key.k8");
PEMParser pemParser = new PEMParser(new InputStreamReader(privateKeyInputStream, StandardCharsets.UTF_8));
Object pemObject = pemParser.readObject();
return (PKCS8EncryptedPrivateKeyInfo) pemObject;
}
}
src/main/resources/key.k8 文件包含
-----BEGIN ENCRYPTED PRIVATE KEY-----
<private key encrypted with AES_CFB_256 encryption using bouncy castle>
-----END ENCRYPTED PRIVATE KEY-----
答: 暂无答案
评论