1

I trying to encrypt and decrypt a simple string with PHPseclib, but when I try to decrypt it, the result is null or empty. Could any one help me please?

here are my functions

public function myEncrypt($conteudoArquivo, $private_key) {
 $rsa = new Crypt_RSA();
 $rsa->loadKey($private_key);
 $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); 
 $cipherText = base64_encode($rsa->encrypt($conteudoArquivo));
 return $cipherText;
}
public function myDecrypt($cipherText, $public_key) {
 $rsa = new Crypt_RSA();
 $rsa->loadKey($public_key);
 $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); 
 $plainText = base64_decode($rsa->decrypt($cipherText));
 return $plainText;
}

and the keys values are:

$private_key = "-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCRwpD+gV0skm+9SHPWFXAHkWV3r37I3N4fom45Z0imih3fDk9E
Q6PuFb56Vfo8IWiNUoP6Gco/eDHcInzO1SSEn07reh86Aosnnj7m/RMg1N5k7A5C
NH26YlATqgJe4DX8SdS/oKLit7xTo3aR+Wg3kZOQQmE5MyRq6TVDywhNyQIDAQAB
AoGAVFk4mN75sUJogSu9RMURGIAOLM2U293ceIgBqxxW0XEZyiu4uTM/WRaiLJ82
eLeIjkeS8hccj9AZYl9exD5Zq7oGTH8HDQAcmZ6p0h3faI1/sR3gnqEB28GZJyd7
MsFRlRiOEdpJAki33m/6UsJ7hRHC6X/w2K4wofzmmHopOIECQQDCAMW7gc4Gmyb1
scP8OwA75QZWgeLo36d7ynUzjqoN682ckSbIxbT6z4eYhEKE/It0gt07WTNmrL0s
vv4DH15xAkEAwFcTC1/FufpDvxt+EKnANmuH5ekri1zcsY3UMJJnjH9e7szfVTH6 
JfhTRRSfi/oOe1aRinfcp/zMJhkf36VA2QJAO8K5JlWJ/Yb1rWGhGaWjINAf7637
E/kxQnTPPZ6Iy9kDcWNVKyub4FblUhoL06Nn4fAd7hZAOzSi4ZHD9XpIQQJAc1MC
QTygcp1jB3A1i0oszLR23FyNVldMoE044BK4cZ5hTm+arRt1MFUPoIj4DNbW3g8O
3uZ1cGf8BA/mc5NDKQJAexm+LcJ3DOTaenQwHw77bfXhbvvCtcvAlYOIawkyDlTx
AkBweqy8BEJbsVRSiBv7k6Hh+T+u1ZWaKDm/ZZCMkA== 
-----END RSA PRIVATE KEY----- ";
$public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCU+1bLfPmcY7qrF/dTbAtuJlv4R/FVc1WEH9HK
U0jQjX/n/db9vz/x0i3te/bKLNEcwUhBu+PWPnOt/qVURG9BUT6RsCRFUn0CyGiUKoy45o9K/mJA
HmbrNtrUB6ckrYLF75Y50nUNsBVHUDw8yQymmiOBT1gc/KM5s1xTz44LMwIDAQAB
-----END PUBLIC KEY-----";
Artjom B.
62k26 gold badges137 silver badges236 bronze badges
asked Nov 27, 2014 at 19:57
0

1 Answer 1

4

You need to encrypt with the public key and decrypt with the private key. That is how asymmetric encryption works. In RSA "encrypting" with the private key is actually signing and "decrypting" with the public key is verification of the signature.

The other thing is that you don't reverse the Base64 encoding properly. So you need to first decode and then decrypt:

$plainText = $rsa->decrypt(base64_decode($cipherText));

The keys you presented cannot be used with this code, because they have a different modulus. You can generate the keys this way:

$rsa = new Crypt_RSA();
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
$keys = $rsa->createKey(2048);
$private_key = $keys["privatekey"];
$public_key = $keys["publickey"];
answered Nov 27, 2014 at 20:17
Sign up to request clarification or add additional context in comments.

4 Comments

It didn't worked, I tried to change the public and private keys, but the encryption function only works with the private key. And besides, I tried to do what you said and added the base64_decod to the cipherText, but the function decrypt still does not returns any value. Is there anything else I could do?
As a note you should consider using OAEP instead of PKCS1 encryption mode, because it is "optimal".
@FelipeThomé Don't forget to hit the V mark on the left of the answer to accept it, otherwise it remains "open".
Finally did it :D

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.