我正在使用 android 上的 Rsa 加密/解密应用程序。我创建并将我的公钥/私钥保存到 sharedpreferences。我正在阅读它们并使用此代码块进行加密/解密:
public String RSADecrypt(byte[] encryptedBytes) throws NoSuchAlgorithmException, NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, UnsupportedEncodingException {
privateKey = getPrivateKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cipherData = cipher.doFinal(encryptedBytes);
return new String(cipherData,"UTF-16BE");
}
public String RSAEncrypt(String plain) throws NoSuchAlgorithmException, NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, UnsupportedEncodingException {
publicKey = getPublicKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherData = cipher.doFinal(plain.getBytes());
return new String(cipherData,"UTF-16BE");
}
现在,例如我正在尝试加密“BAHADIR”,它向 EditText 写入无意义的单词。我使用了“UTF-8”、“UTF-16”、“UTF-16LE”、“UTF16BE”和“ISO-8859-1”,但每次我再次得到无意义的词,不一样,但它们都没有意义。我哪里错了,你能帮帮我吗?谢谢你。