views:

81

answers:

1

why is the content of $encrypted every time different?

// aquire public key from server
$server_public_key = openssl_pkey_get_public(file_get_contents("C:\publickey.pem"));

// rsa encrypt
openssl_public_encrypt("123", $encrypted, $server_public_key);

also I have tried this one

$publicKey = "file://C:/publickey.pem";
$privateKey = "file://C:/privatekey.pem";
$plaintext = "String to encrypt";

openssl_public_encrypt($plaintext, $encrypted, $publicKey);
$transfer = base64_encode($encrypted);
openssl_private_decrypt($encrypted, $decrypted, $privateKey);

echo $transfer;  //encrypted string

and $transfer is everytime a different string:...

Z1xyMUquARxcGjqjjSHNAm41CnHI02GXxLyFivvta8YhDkhRJdD4i3kx+8GElljdiSY/NMF9UD3ritWMLGmscdq/QyIf+geYxJFePNd1dNWg+V6zbAKRLaEpsU+aB87jiM/GjytLEkI63dku02BS0ZBgz9UZw/FDNaynV5bTTDM=

mRgLPsPtMoV9la7zzuU+cLzS5xMDp7QUmH6Iv4Sv4/FNjt62zcv9ZMWkfG3uVhS8Z1UDtGl+met1CYjBTcfjHCR6hahbwOkTCICXtkRQcc371vURW04XhQzMNgIIbvN5BBdmIyYI6alrS2vKUq7b3T0h8sJf36zh5CynYzyDCFU=

G5FhMoJGiUwEBvEOeZpDDrEXdxbWX5iaJ6F+VdYJ3CURPRMftskZNlDhat8gA5V0G+3nXVQZptkHjxMkOqPlmwJHjgIqAiFppHLpEKohyT9qNwkAR00Y6PiWrNUJPiEIZqXHAb8TS0AA0Quhc0UAwcc+I8NGOD59k8BrZE6Z5Ew=

+4  A: 

The PKCS#1 encryption algorithm uses some random seed to make the cipher-text different every time.

This protects the cipher-text against several attacks, like frequency analysis, ciphertext matching. For example, if you were using a public key to encrypt all your password without randomness. All the same password will yield the same cipher-text. Someone can figure out all the popular passwords by checking the frequency of the cipher-text.

For symmetric key encryption, IV (Initial Vector) serves a similar purpose.

ZZ Coder
Is it possible to determine the random seed and sent it to another pkcs#1 encryption algorithm which will use it to decrypt some data encrypted from the first pkcs#1 algorithm?
panny
@panny: please read the PKCS#1 specification. You don't need to do what your asking, the PKCS#1 format makes it possible to remove the random bits by the decryptor without knowing them in advance. Your rsa decryption function almost certainly does this for you.
GregS
Symmetric key encryption doesn't have the same problem (of ciphertext matching), since with a symmetric algorithm anyone who can generate ciphertext for a candidate plaintext must already have the key.
caf