我在我的应用程序中使用RSA加密技术。要存储生成的公钥,我将其转换为字符串,然后将其保存在数据库中。Java中的字符串的密钥RSA
Key publicKey=null;
Key privateKey=null;
KeyPair keyPair=RsaCrypto.getKeyPairRSA(1024);
publicKey=keyPair.getPublic();
privateKey=keyPair.getPrivate();
String publicK=Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);
String privateK=Base64.encodeToString(privateKey.getEncoded(), Base64.DEFAULT);
我保存字符串publicK
和privateK
。 我的问题是,当我想用RSA加密/解密文本并使用我保存的字符串格式的密钥时,我不知道如何将其转换为Key
。
public static String encrypt(Key publicKey, String inputText){
byte[]encodedBytes=null;
String encryptedText="";
try {
Cipher cipher=Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encodedBytes=cipher.doFinal(inputText.getBytes());
} catch (Exception e) {Log.e("Error", "RSA encryption error"); }
encryptedText=Base64.encodeToString(encodedBytes, Base64.DEFAULT);
return encryptedText;
}
你有什么想法吗? 非常感谢
请,请,请不要将未加密的私钥存储在数据库中。如果您必须存储称为托管的私钥,并且您应该使用只有您的应用程序已知的对称密钥对它们进行加密。 – KyleM
谢谢,是的,我正在考虑这个问题。您能否给我一个建议,在应用程序中存储密钥的最佳方式是什么?谢谢 – rgreso
使用受密码保护的Java密钥库。查找Java keytool ... – KyleM