Android Question Problems wtih RSA encrypt/decrypt between B4A and PHP

toby

Well-Known Member
Licensed User
Longtime User
I want to be able to encrypt some data in B4A and decrypt it with PHP on the server and vice versa. I use this online tool to generate the public and private keys. Both PHP and B4A use same keys and the test data is "this is a test".

What I've achieved so far:
1. Encrypt and decrypt successfully within B4A
2. Encrypt and decrypt successfully within PHP

Problems I'm facing
1. B4A decrypted data encrypted by PHP with some issue: The expected result, "this is a test", is at the end. How to get rid of those unwanted chars before that?
Log data:
�������s��A�|y��yɍ�'0ռ׺�`z7<�j��R�����this is a test

2. PHP failed to decrypt data encrypted by B4A.
openssl_private_decrypt() failed and didn't return any related message.

I'm sure that I've made some mistakes. Could someone point me to the right direction? Test B4A project atttached.

TIA


php encrypt file:
<?php
$publicKey="-----BEGIN PUBLIC KEY-----
MFswDQYJKoZIhvcNAQEBBQADSgAwRwJAauoxTNicD/dVlWJQMjBmX/Ba2FlIw/21
k8VtRo3V2VhBHtRCBXDCNbb+Ld6nXvBxMhv1E5rdGWzsQyC/mtmhvQIDAQAB
-----END PUBLIC KEY-----";
    echo 'public key=', $publicKey1, '<br>';
    $msg="this is a test";
    
    if (openssl_public_encrypt($msg, $strEncrypted, $publicKey))
    {
        echo "strlen=", strlen($strEncrypted), "<br>";
        $base64Encoded=base64_encode($strEncrypted);
        echo 'base64Encoded=', $base64Encoded;
    }
    else
    {
        print ("Error: Encryption failed");
    }
    echo "<br>end";

?>

php decrypt file:
<?php

    $privateKey="-----BEGIN RSA PRIVATE KEY-----
MIIBOQIBAAJAauoxTNicD/dVlWJQMjBmX/Ba2FlIw/21k8VtRo3V2VhBHtRCBXDC
Nbb+Ld6nXvBxMhv1E5rdGWzsQyC/mtmhvQIDAQABAkBVQVL22HP6N1zDtpYEr5uz
PVLz2p6vywyUPYaaEFhK646Ed7G6Mp5fRSxdLvpJ7NA3CTC60+5mGfgM5QGrIj4B
AiEAwGmd5RwywGv4vYuMrrqS4a3nXXIQX+pGn1JpXMFnqh0CIQCOP1dyb55Ud4cO
mSI48jzd5P6cbnmStVlzaNgTlvFEIQIhAJMYq0MaGZqNcK3HX5e8vsmVH0mLCAWr
nq3yXOYyXUPpAiBSrIncnvb8zMOYPnRfeJrbtJ4uuR67n98vbn43VIFBQQIgAKVC
JfIzLObXD3o9CDD4pQ73bSM6KWwlZ4VqXgiCOFU=
-----END RSA PRIVATE KEY-----";

    $msgEncryptedB4A="BbT7yBJVMJi8NbCotK7DDvn1ej2OpJwIXjDp+433Lke9c9DQNDjgwhtx+ZQICmEqo1dE3DE0g6ahfRY6bdgvzA==";
    //$msgEncrypted="WFxMzF821+EqoUQ+ygnhyoIOqo3u29aKJBr0u532E2twEzRN+r+hYqjwuUWi4+ItKxW4jb+qTZX6TkkqyqzD+g==";
    $msgBytes = base64_decode($msgEncryptedB4A, true);

     if (openssl_private_decrypt($msgBytes, $msgDecrypted, $privateKey))
    {
        print $msgDecrypted;
    }
    else
    {
        print ("Error: Decryption failed");
    }
?>
 

Attachments

  • rsaEncrypt.zip
    15 KB · Views: 214

toby

Well-Known Member
Licensed User
Longtime User
By specifying the optional paramter as follows, PHP can now decrypt data encrypted by B4A
B4X:
openssl_public_encrypt($msg, $strEncrypted, $publicKey, OPENSSL_PKCS1_PADDING))

Note: OPENSSL_PKCS1_PADDING is not recommended any more due to security risk; we should instead use OPENSSL_PKCS1_AEP_PADDING which is not supported by current version of the Encryption library.
 
Upvote 0
Top