[请教]mcrypt_encrypt AES 256加密问题
应用需要由服务器生成一段加密过的信息给客户端。客户端为嵌入式设备。
打算使用AES-256 CEB/CBC
使用AES 256时得到的密文在PC上面使用其它工具解密得不到正确的原文。$privateKey = "1234567890abcdef1234567890abcdef";
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $privateKey, $data, MCRYPT_MODE_ECB);后来使用AES 128时,PC上面的工具可以正确解密原文。$privateKey = "1234567890abcdef";
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_ECB);请有做过的朋友指点一下。 查到以下文章,问题解决了。
http://stackoverflow.com/questions/6770370/aes-256-encryption-in-php
AES-128/AES-192/AES-256都使用16字节的块大小,所以都使用 MCRYPT_RIJNDAEL_128 算法,
仅是密钥长度不一样。
MCRYPT_RIJNDAEL_256 与 AES-256 并不是一回事。<?php
$data = "Sometime it is needed to use 2 way encryption for storing data.";
//$privateKey = "0123456789abcdef"; // AES-128
//$privateKey = "0123456789abcdef01234567"; // AES-192
$privateKey = "0123456789abcdef0123456789abcdef"; // AES-256
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_ECB, '');
header("Content-Encoding: none");
header("Content-type: application/x-msdownload");
header("Cache-Control: private");
header("Accept-Ranges: bytes");
header("Accept-Length: ".strlen($encrypted));
header("Content-Disposition: attachment; filename=test.bin");
header('Pragma: no-cache');
header('Expires: 0');
echo $encrypted;
?>
页:
[1]