【发布时间】:2019-03-22 22:42:28
【问题描述】:
从原始资源文件生成公钥/私钥对并使用它们在 Android 应用程序中加密/解密非常简单。 但是,以下代码在 VirtualBox 中使用 Android-x86-v4.4.4 模拟器运行时无法正确恢复纯文本。谁能指出这段代码有什么问题(它没有给出任何错误或产生任何异常): (更改为 Cipher.getInstance("RSA/NONE/NoPadding") 也无济于事)
PublicKey mPublicKey = null;
PrivateKey mPrivateKey = null;
String mPlainText = "The quick brown fox jumped over the lazy dog" ;
byte[] mEncryptText = null;
byte[] mDecryptText = null;
try {
InputStream mIS = getResources().openRawResource(R.raw.test1_public_key);
DataInputStream dis = new DataInputStream(mIS);
byte [] keyBytes = new byte [(int) mIS.available()];
dis.readFully(keyBytes);
dis.close();
mIS.close();
X509EncodedKeySpec mX509KeySpec = new X509EncodedKeySpec(keyBytes);
mPublicKey = (KeyFactory.getInstance("RSA")).generatePublic(mX509KeySpec);
Toast.makeText(this, "Publickey generated", Toast.LENGTH_LONG).show();
}
catch(Exception e){
Log.e("onButtondecrypt", "exception", e);
Log.e("onButtondecrypt", "exception: " + Log.getStackTraceString(e));
}
try {
InputStream mIS = getResources().openRawResource(R.raw.test1_private_key);
DataInputStream dis = new DataInputStream(mIS);
byte [] keyBytes = new byte [(int) mIS.available()];
dis.readFully(keyBytes);
dis.close();
mIS.close();
PKCS8EncodedKeySpec mPKCS8keySpec = new PKCS8EncodedKeySpec(keyBytes);
mPrivateKey = (KeyFactory.getInstance("RSA")).generatePrivate(mPKCS8keySpec);
Toast.makeText(this, "PRIVATE key generated", Toast.LENGTH_LONG).show();
}
catch(Exception e){
Log.e("onButtondecrypt", "exception", e);
Log.e("onButtondecrypt", "exception: " + Log.getStackTraceString(e));
}
Toast.makeText(this, mPlainText, Toast.LENGTH_LONG).show();
Toast.makeText(this, "Encrypting with Publickey ...", Toast.LENGTH_LONG).show();
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, mPublicKey);
mEncryptText = cipher.doFinal(mPlainText.getBytes());
Toast.makeText(this, mEncryptText.toString(), Toast.LENGTH_LONG).show();
}
catch(Exception e){
Log.e("onButtondecrypt", "exception", e);
Log.e("onButtondecrypt", "exception: " + Log.getStackTraceString(e));
}
Toast.makeText(this, "Decrypting with PRIVATE key ...", Toast.LENGTH_LONG).show();
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, mPrivateKey);
mDecryptText = cipher.doFinal(mEncryptText);
Toast.makeText(this, mDecryptText.toString(), Toast.LENGTH_LONG).show();
}
catch(Exception e){
Log.e("onButtondecrypt", "exception", e);
Log.e("onButtondecrypt", "exception: " + Log.getStackTraceString(e));
}
谢谢大家。
【问题讨论】:
-
mDecryptText.toString()不会像您认为的那样做。请参阅this 问题。 -
string 转换为 byte[] 的方式是错误的,反之亦然。正确的方法是 byte[] st = txt.getBytes("UTF-8");和 String s = new String(bytes);
-
强烈建议在
Cipher.getInstance()的参数中包含完整的 "algorithm/mode/padding" 规范。它还建议过渡到 OAEP 填充以进行 RSA 加密。因此,例如,您可以指定Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
标签: android encryption rsa