Reputation: 405
What does this error mean:
java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c0000b9:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG
my code is:
public static byte[] encryptByPublicKey(byte[] data, String key)
throws Exception {
key = key.replace("-----BEGIN RSA PUBLIC KEY-----\r\n", "").replace("-----END RSA PUBLIC KEY-----", "");
byte[] bytes = decryptBASE64(key);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(bytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey pkPublic = keyFactory.generatePublic(x509KeySpec);
Cipher pkCipher = Cipher.getInstance("RSA");
pkCipher.init(Cipher.ENCRYPT_MODE, pkPublic);
return pkCipher.doFinal(data);
}
I'm sure, i have the correct data and key (that created in php), but i get this error in line:
PublicKey pkPublic = keyFactory.generatePublic(x509KeySpec);
I don't have any problem whit this code in my other android (without php server)apps, and i have correct answer by the same encryption/decryption code.
Is it possible that a function using in two projects, with the same input an output values, in the same software, has different actions? Yes! it's possible, but how about Android Studio, and Phpstorm? Is it about Openssl cer.? or other libraries? or some other reasons? How can I overcome this error?
I appreciate it if you help me. Thanks in advance for your answers.
Upvotes: 2
Views: 10272
Reputation: 21
Please check the key string, maybe contains illegal Base64 char: "\n", "\t", or \s and so on, remove them and try again !
Upvotes: 2