1
public static String encryptByPublicKey(byte[] data, String key)
throws Exception {
byte[] keyBytes = decryptBASE64(key);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return new String(cipher.doFinal(data));
}
我有一個公鑰,如MFWww.........EAAQ==
。當我將該字符串傳遞給該參數時,加密的消息是一些未知字符。因此,我懷疑我應該在將密鑰傳遞給函數之前對密鑰做些什麼。但我不怎麼做到這一點。所以看到有人可以幫忙。通過Android/Java中的RSA加密內容
謝謝
請參閱BouncyCastle和Jasypt庫,但我不確定您是否可以在android上使用它們 – rgrebski