I have a PEM private key and I need to sign a string. But the code continues to fail with the exception:
java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c0890ba:ASN.1 encoding routines:asn1_check_tlen:WRONG_TAG
key line:
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIICxjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQI4P/+9mJV6RwCAggA
MBQGCCqGSIb3DQMHBAg/ZWGXeLHgeASCAoAhExhFxfcikmIKbFP0rgAlJuj1r999
... and so on...
hlgzM2p71FdC6NDVyyxbit/IzbimtJyhkRwOAnZ98yqtXWUEOx2v7CcUqiU8dSLA
K0PsaxNTUeUcQV+Z7yJk/8HxfE1ya3u2CgPXCZsWWmbxQG/+awE0eEnZ
-----END ENCRYPTED PRIVATE KEY-----
I tried many options, looked through many answers, but the results were the same
Edit: using James K Polk, I managed to get the bytes with the secret key, but now I get the java.security.NoSuchAlgorithmException: SecretKeyFactory PBES2 implementation not found.Modified code:
private String sign(String dataString, String pkString, String privateKeyPass) throws Exception {
pkString = pkString.replace("-----BEGIN ENCRYPTED PRIVATE KEY-----", "");
pkString = pkString.replace("-----END ENCRYPTED PRIVATE KEY-----", "");
pkString = pkString.replaceAll("\\s+","");
byte[] privateKeyBytes = decryptPrivateKey(Base64.decode(pkString, Base64.DEFAULT), privateKeyPass);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
Signature instance = Signature.getInstance("SHA1withRSA");
instance.initSign(privateKey);
instance.update(dataString.getBytes(UTF_8));
return Base64.encodeToString(instance.sign(), Base64.DEFAULT);
}
public static byte[] decryptPrivateKey(byte[] key, String pass) throws Exception {
PBEKeySpec passKeySpec = new PBEKeySpec(pass.toCharArray());
EncryptedPrivateKeyInfo encryptedKey = new EncryptedPrivateKeyInfo(key);
Timber.w("encryptedKey.getAlgName(): %s", encryptedKey.getAlgName());
SecretKeyFactory keyFac = SecretKeyFactory.getInstance(encryptedKey.getAlgName());
SecretKey passKey = keyFac.generateSecret(passKeySpec);
Cipher pbeCipher = Cipher.getInstance(encryptedKey.getAlgName());
pbeCipher.init(Cipher.DECRYPT_MODE, passKey, encryptedKey.getAlgParameters());
return pbeCipher.doFinal(encryptedKey.getEncryptedData());
}
EDIT: I ended up using the http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art050 class :
PrivateKey privateKey = KeyImport.readPrivateKeyFile(pkFileName, privateKeyPass);
I save keyString to a file and then pass it readPrivateKeyFile