RSA - 用PHP加密Android / Decrypt

时间:2016-07-26 09:17:48

标签: java php android encryption rsa

我在java中加密了一个单词,我在php中解密它时遇到了麻烦。

这是我在android中创建键的方法:

public void GenerateAndSaveToFile(){
    try {

        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(1024);
        KeyPair kp = kpg.genKeyPair();
        Key publicKey = kp.getPublic();
        Key privateKey = kp.getPrivate();
        KeyFactory fact = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
        RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
        //////////////////////////
        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        File myDir = new File(root + "/keys");
        myDir.mkdirs();
        File file = new File (myDir, "private.key");
        FileOutputStream fileout1 = new FileOutputStream(file);
        ObjectOutputStream oout1 = new ObjectOutputStream(fileout1);
        oout1.writeObject(priv.getModulus());
        oout1.writeObject( priv.getPrivateExponent());
        oout1.close();
        ///////////////////////
        File file2 = new File (myDir, "public.key");
        FileOutputStream fileout2 = new FileOutputStream(file2);
        ObjectOutputStream oout2 = new ObjectOutputStream(new BufferedOutputStream(fileout2));
        oout2.writeObject(pub.getModulus());
        oout2.writeObject( pub.getPublicExponent());
        oout2.close();

    }
    catch (Exception ex){
        Exception e = ex;
    }

}

这是我如何在android中使用生成的公钥加密单词:

byte[] u1Encrypted = RSAEncrypt(String.valueOf(inputEmail.getText()).getBytes());

 public byte[] RSAEncrypt(byte[] data) {
    try {
        PublicKey pubKey = ReadPublicKey();
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        byte[] cipherData = cipher.doFinal(data);
        return cipherData;
    }        
    catch (Exception ex)
    {
        Exception e = ex;
        return null;
    }

}


private PublicKey ReadPublicKey() throws IOException {
    try {
        AssetManager assetManager = getAssets();
        InputStream in = assetManager.open("public.key");
        ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));

        try {

            BigInteger m = (BigInteger) oin.readObject();
            BigInteger e = (BigInteger) oin.readObject();
            RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
            KeyFactory fact = KeyFactory.getInstance("RSA");
            PublicKey pubKey = fact.generatePublic(keySpec);
            return pubKey;
        } catch (Exception e) {
            throw new RuntimeException("Spurious serialisation error", e);
        } finally {
            oin.close();
        }
    }
    catch (Exception ex)
    {
        Exception e = ex;
        return null;
    }
}

然后我将加密的字符串转换为android中的base64:

String u1EncryptedBase64 = Base64.encodeToString(u1Encrypted, Base64.DEFAULT);

在php中我解码了base64字符串:

$encryptedString = base64_decode(u1EncryptedBase64);

获取私钥:

$keytmp = fopen("../../../private.key", "r") or die("Unable to open file!");
$key = fread($keytmp,filesize("../../../private.key"));
$res = openssl_get_privatekey($key);

最后我尝试解密php中的字符串:

if (openssl_private_decrypt($encryptedString, $decrypted, $res)) {

    echo $decrypted;
}
else
{
    echo "problem";
}

我得到的错误是:

警告:openssl_private_decrypt():key参数不是有效的私钥......

请指导我如何完成这项任务。 感谢

1 个答案:

答案 0 :(得分:0)

我可以向您展示如何在android中完成加密和在php中进行解密。效果很好,也许您可​​以从中获得一些帮助。在android中,代码如下:

     final private static String RSA_public_key="MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEApeRuOhn71+wcRtlN6JoW\n" +
        "JLrVE5HKLPukFgpMdguNskBwDOPrrdYKP1e3rZMHN9oVB/QTTpkQM4CrGYlstUmT\n" +
        "u5nEfdsH4lHRxe3qhi9+zOknWKJCnW4Cq70oITCAK08BIJ/4ZcGM1SUyv1+0u1aB\n" +
        "cx6g1aKhthttRjNpck2LBaHVolt7Z4FTb5SdZMwJKRyEv8fuP6yyR0CJGEbQKZKA\n" +
        "ODNKyqJ42sVzUQ2AMQIWdhkFdAFahKCL4MChGvKU6F20cHdvokyxXJjU3sZobjNf\n" +
        "i+8FzH9hd7y8kmi4o3AKP69p5asgflXoXHpo135i3NzZqlNJ+Bs9pY+90u9iLScp\n" +
        "JwIBAw==";

static String enccriptData(String txt)
{
    String encoded = "";
    byte[] encrypted = null;
    try {
        byte[] publicBytes = Base64.decode(RSA_public_key, Base64.DEFAULT);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey pubKey = keyFactory.generatePublic(keySpec);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); //or try with "RSA"
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        encrypted = cipher.doFinal(txt.getBytes());
        encoded = Base64.encodeToString(encrypted, Base64.DEFAULT);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return encoded;
}

我将在上面的函数中返回的编码字符串粘贴到$ string下面。这是php的代码:

<?php
   $string="W39VlV1Q96QzDIR/jINzaEL7rh2Z+Z90uxJ1DtaSfkVKzIgt2TIkxsuRY+A2icwPtTdq6+9j1Xb009KT+ck2KD+dot3wPL5UaHqApbZSi6UZan/nDbFCNJdffTlTWsPS2ThEefeMMSs8HE2ORSt42D8cxYlogOvkvlLr60cHYKwfC1itLSqPuYR+C/gPAf6yCteLbj//EkJp8TemlmPi0eSsH492FgrmBqiTOS4LzpzsPFpSI4KJbuM2dvqp5jkt7MnpR1laILmyC37fA5XPUQmEQChoG5eSbMxqO7SPboYC1BH9ATy6uTS1MGXGDzJ2FSJ41MMRV1Ul/4UNFVA8Ng==";
$encryptedString = base64_decode($string);
$key="-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCl5G46GfvX7BxG
2U3omhYkutUTkcos+6QWCkx2C42yQHAM4+ut1go/V7etkwc32hUH9BNOmRAzgKsZ
iWy1SZO7mcR92wfiUdHF7eqGL37M6SdYokKdbgKrvSghMIArTwEgn/hlwYzVJTK/
X7S7VoFzHqDVoqG2G21GM2lyTYs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/l1qlwEtnhpPOoLnJgrx1drt6pFMchRZnM8qYm2XUmWBCcfjz9w340SdCXQ/mD
jOFamgUS1m/OZ0wKxKpzjWh+6KUTjSzFbtP/iKgAqvp3iACfghYF1fwenMmY7z5q
P84dKpv5DSPtqO/n9fUsWM9/3aTNo09z0HjYjhcCgYEAoN+1/ZzonPWDIY/u+7bW
e8BkcuBVpMZe7qBYHeVix89yvyuVE+erKqurnG7fN7YIDX8V1OcVP+qHw8fJNQKL
wd03nNIIJyTPXodgty3HYjBUe8fVn/8P+JOv2U7bJCkUGlFeyZIMZWEsYd8t5794
3fotiYXOUug9bFtnGHRyY7I=
-----END PRIVATE KEY-----";
openssl_private_decrypt($encryptedString, $decrypted, $key);
$string1=$decrypted;
echo $string1;


?>

私钥和公钥是相互建立的。