3
votes

I encrypt a word in java and I have trouble decrypting it in php.

here is how I create the keys in 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;
    }

}

here is how I encrypt the word with generated public key in 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;
    }
}

then I convert the encrypted string to base64 in android:

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

and in php I decode the base64 string:

$encryptedString = base64_decode(u1EncryptedBase64);

get the private key:

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

and finally I try to decrypt the string in php:

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

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

and error I get is:

Warning: openssl_private_decrypt(): key parameter is not a valid private key ...

please guide me how to accompolish this task. thanks

1
openssl_get_privatekey expects a PEM-encoded private key. Is that what you're creating in Java? (and what you want to PEM-encode is probably privateKey.getEncoded()).Michael
@Michael, no I think it's not PEM-encoded. I'm not sure what format key KeyFactory.getInstance("RSA") generates.farhang67

1 Answers

1
votes

I can show you how i have done encryption in my android and decryption in php. It worked wonderfully fine.May be you can get some help from this. In android the code is like:

     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;
}

I have paste that encoded string that is returned in the above function in below $string. This is the code for 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
iWy1SZO7mcR92wfiUdHF7eqGL37M6SdYokKdbgKrvSghMIArTwEg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YOcryWlbztcTyyrV95vAgtouceC2SNGXMCgYEAhv97
wO/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;


?>

The private and public keys are made for each other.