【问题标题】:encrypt/decrypt with Android and PHP使用 Android 和 PHP 加密/解密
【发布时间】:2014-10-03 13:33:55
【问题描述】:

对不起这个问题。我已经阅读了所有以前的问题,但我的代码仍然无法正常工作。 提前感谢任何可以帮助我了解问题所在的人。

在 android 中,我使用此代码读取公钥并生成加密文本:

public static PublicKey getPublicKeyFromString(String stringKey) throws Exception {
    byte[] keyBytes = stringKey.getBytes();
    byte[] decode = Base64.decode(keyBytes, Base64.DEFAULT);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(decode);
    return (PublicKey) fact.generatePublic(x509KeySpec);
}
public static String RSAEncrypt(final String plain, final PublicKey publicKey)
        throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    byte[] encryptedBytes;
    Cipher cipher;      
    cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    encryptedBytes = cipher.doFinal(plain.getBytes());
    return Base64.encodeToString(encryptedBytes, Base64.DEFAULT);
}
//I call these functions in this manner.
private final String pubKeyString = 
//"-----BEGIN PUBLIC KEY-----" +
"MIG..." +
"...";
//"-----END PUBLIC KEY-----"
PublicKey pubKey = RSAFunctions.getPublicKeyFromString(pubKeyString);
String encData = RSAFunctions.RSAEncrypt("prova", pubKey);

在php中生成publickey.php和privatekey.php文件,代码如下:

<?php
include('./Crypt/RSA.php');
$rsa = new Crypt_RSA();
extract($rsa->createKey()); // == $rsa->createKey(1024) where 1024 is the key size
$File1 = "./privatekey.php"; 
$Handle = fopen($File1, 'w');
fwrite($Handle, "<?php \$privatekey=\"" . $privatekey . "\"?>"); 
fclose($Handle); 
$File2 = "./publickey.php"; 
$Handle = fopen($File2, 'w');
fwrite($Handle, "<?php \$publickey=\"" . $publickey . "\"?>"); 
fclose($Handle); 
?>

在 php 中,我使用此代码来解密数据:

<?php
include('Crypt/RSA.php');
require('privatekey.php');

$rsa = new Crypt_RSA();
$rsa->loadKey($privatekey); // private key

$base64_string = $_GET["data"];
$base64_string = str_replace(' ', '+', $base64_string); 

$ciphertext = base64_decode( $base64_string );

//$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$plaintext = $rsa->decrypt($ciphertext);

echo $plaintext;
?>

我还制作了一个 php crypt 脚本来测试我的解密 php 函数。这是我的 encrypt.php 的代码:

<?php
include('Crypt/RSA.php');
require('publickey.php');

$rsa = new Crypt_RSA();
$rsa->loadKey($publickey); // public key

$plaintext = $_GET["data"];

//$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
$ciphertext = $rsa->encrypt($plaintext);
echo base64_encode( $ciphertext );
?>

我只使用php加密和解密文本没有问题,但是如果我使用android应用程序制作的加密数据,php在解密时会给我一个错误。

感谢您的关注。

【问题讨论】:

    标签: php android encryption cryptography phpseclib


    【解决方案1】:

    在你的 php 中有: CRYPT_RSA_ENCRYPTION_PKCS1

    但是当您在 Android 上创建 Cipher 对象时,您会省略模式和填充

    cipher = Cipher.getInstance("RSA");
    

    你应该尝试类似的东西

    Cipher c = Cipher.getInstance("AES/CBC/PKCS1Padding");
    

    参考: http://developer.android.com/reference/javax/crypto/Cipher.html

    【讨论】:

    • RSA 与 AES 不太一样。
    • true,对不起,我忘记将 AES 更改为 RSA,我只是想强调他应该在两种情况下都将选项设置为相同的事实。密码密码 = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");可能更适合
    • 我试图将 RSA 放在 Cipher.getInstance 中,但我也将它放在 KeyFactory.getInstance 中,它给了我一个错误。非常感谢。
    • 我希望您意识到这很容易受到 Daniel Bleichenbacher 于 1998 年首次发布的 padding oracle 攻击。
    猜你喜欢
    • 1970-01-01
    • 2015-11-10
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 2011-04-29
    • 2011-10-13
    相关资源
    最近更新 更多