dstd2129 2010-06-05 10:37
浏览 857
已采纳

使用openssl_public_encrypt加密的数据每次都不同?

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=

  • 写回答

1条回答 默认 最新

  • doumanju2533 2010-06-05 11:24
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型