秘密鍵をderファイルから読み取ろうとしています。私はlogcatで次のエラーが発生しています。戻り値は空です。Android - InvalidKeySpecException - Javaランランタイム例外SSLInternal:.derファイルを読み込もうとしたときにTOO_LONG
java.security.spec.InvalidKeySpecException:java.lang.RuntimeException:エラー:0c0000af:ASN.1エンコードのルーチン:OPENSSL_internal:TOO_LONG
私は、これはすでに起こっていたが、見つけることができるような状況を探してみましたなし。このエラーが何を意味し、どのように解決できるかを知りたい。ここで
は私のコードです:
public static String decryptionWithFile(String encrypted,String privateFile2)throws Exception {
PrivateKey privateKey = getPrivateKey(privateFile2);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bts = Hex.decodeHex(encrypted.toCharArray());
bts = cipher.doFinal(bts);
bts = getFinalBytesOfDycryptedString(bts);
String decryptedMessage = new String(cipher.doFinal(encrypted.getBytes()));
return new String(bts,"UTF-8");
}
そして、ここではgetPrivateKey(です)。方法:
private static PrivateKey getPrivateKey(String privateFile2)throws Exception {
File f = new File(privateFile2);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(spec);
return privKey;
}
重要な注意:私は私の資産フォルダに.derファイルを追加して、パスにアクセスするために、内部ストレージ内のファイルにそれを保存 - 私の関数が必要です。あなたは、このプロセス中にファイルに何かが起こったに違いないと思いますか? (それは公開鍵でうまくいった)
ファイルから秘密鍵を正しく取得していますか?あなたはログに印刷して確認しましたか? –