在 Android 中解密 Base64 加密图像
Decrypting a Base64 encrypted image in Android
我正在尝试解密从服务器发送到 android 应用程序的文本。
在 PHP 上,我有以下内容:
$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
$key = "-----BEGIN PUBLIC KEY-----\n" . ($PublicKey)
. '-----END PUBLIC KEY-----';
$rsa->loadKey($key);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$imageEncrypt = base64_encode($rsa->encrypt($base64));
编码和加密效果很好。
当我将加密文本发送到 android 时,我无法解密。我使用了代码:
public static String decryptString(String alias,String cipherText) {
try {
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)keyStore.getEntry(alias, null);
// RSAPrivateKey privateKey = (RSAPrivateKey) privateKeyEntry.getPrivateKey();
Cipher output = Cipher.getInstance("RSA/ECB/PKCS1Padding");
output.init(Cipher.DECRYPT_MODE, privateKeyEntry.getPrivateKey());
CipherInputStream cipherInputStream = new CipherInputStream(
new ByteArrayInputStream(Base64.decode(cipherText, Base64.DEFAULT)), output);
ArrayList<Byte> values = new ArrayList<>();
int nextByte;
while ((nextByte = cipherInputStream.read()) != -1) {
values.add((byte)nextByte);
}
byte[] bytes = new byte[values.size()];
for(int i = 0; i < bytes.length; i++) {
bytes[i] = values.get(i).byteValue();
}
String finalText = new String(bytes, 0, bytes.length, "UTF-8");
return finalText;
//decryptedText.setText(finalText);
} catch (Exception e) {
Toast.makeText(context, "Exception " + e.getMessage() + " occured", Toast.LENGTH_LONG).show();
Log.e("DecryptStringTAG", Log.getStackTraceString(e));
}
return "EMPTY";
}
错误是:
java.io.IOException: Error while finalizing cipher
Caused by: javax.crypto.IllegalBlockSizeException
奇怪的是,当我尝试从 PHP 发送像 "Hello" 这样的消息时,android 成功解密了它。但是当我发送加密图像时,我得到了规定的错误。
我一直在努力寻找错误。
有什么帮助吗?
谢谢
RSA 非对称密钥加密,这是 public 密钥加密所使用的,即 RSA 本质上是 public 密钥加密。如果您必须使用 public/private 密钥对加密,答案是混合加密,类似于 SSL。
创建随机对称密钥,使用它通过 AES 加密数据。然后用 RSA public 密钥加密对称密钥。
解密时,首先使用 RSA 私钥解密对称密钥,然后使用对称 AES 解密数据。
如果您正在寻找安全加密,您确实需要让领域专家至少设计和审查实施。安全性很难做到正确,如果不正确就没有安全性。
我正在尝试解密从服务器发送到 android 应用程序的文本。 在 PHP 上,我有以下内容:
$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
$key = "-----BEGIN PUBLIC KEY-----\n" . ($PublicKey)
. '-----END PUBLIC KEY-----';
$rsa->loadKey($key);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$imageEncrypt = base64_encode($rsa->encrypt($base64));
编码和加密效果很好。 当我将加密文本发送到 android 时,我无法解密。我使用了代码:
public static String decryptString(String alias,String cipherText) {
try {
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)keyStore.getEntry(alias, null);
// RSAPrivateKey privateKey = (RSAPrivateKey) privateKeyEntry.getPrivateKey();
Cipher output = Cipher.getInstance("RSA/ECB/PKCS1Padding");
output.init(Cipher.DECRYPT_MODE, privateKeyEntry.getPrivateKey());
CipherInputStream cipherInputStream = new CipherInputStream(
new ByteArrayInputStream(Base64.decode(cipherText, Base64.DEFAULT)), output);
ArrayList<Byte> values = new ArrayList<>();
int nextByte;
while ((nextByte = cipherInputStream.read()) != -1) {
values.add((byte)nextByte);
}
byte[] bytes = new byte[values.size()];
for(int i = 0; i < bytes.length; i++) {
bytes[i] = values.get(i).byteValue();
}
String finalText = new String(bytes, 0, bytes.length, "UTF-8");
return finalText;
//decryptedText.setText(finalText);
} catch (Exception e) {
Toast.makeText(context, "Exception " + e.getMessage() + " occured", Toast.LENGTH_LONG).show();
Log.e("DecryptStringTAG", Log.getStackTraceString(e));
}
return "EMPTY";
}
错误是:
java.io.IOException: Error while finalizing cipher
Caused by: javax.crypto.IllegalBlockSizeException
奇怪的是,当我尝试从 PHP 发送像 "Hello" 这样的消息时,android 成功解密了它。但是当我发送加密图像时,我得到了规定的错误。
我一直在努力寻找错误。
有什么帮助吗?
谢谢
RSA 非对称密钥加密,这是 public 密钥加密所使用的,即 RSA 本质上是 public 密钥加密。如果您必须使用 public/private 密钥对加密,答案是混合加密,类似于 SSL。
创建随机对称密钥,使用它通过 AES 加密数据。然后用 RSA public 密钥加密对称密钥。
解密时,首先使用 RSA 私钥解密对称密钥,然后使用对称 AES 解密数据。
如果您正在寻找安全加密,您确实需要让领域专家至少设计和审查实施。安全性很难做到正确,如果不正确就没有安全性。