MCRYPT_MODE_ECB (electronic codebook) is suitable for random data, such as encrypting other keys. Since data there is short and random, the disadvantages of ECB have a favorable negative effect. MCRYPT_MODE_CBC (cipher block chaining) is especially suitable for encrypting files where the security is increased over ECB significantly. MCRYPT_MODE_CFB (cipher feedback) is the best mode for encrypting byte streams where single bytes must be encrypted. MCRYPT_MODE_OFB (output feedback, in 8bit) is comparable to CFB, but can be used in applications where error propagation cannot be tolerated. It's insecure (because it operates in 8bit mode) so it is not recommended to use it. MCRYPT_MODE_NOFB (output feedback, in nbit) is comparable to OFB, but more secure because it operates on the block size of the algorithm. MCRYPT_MODE_STREAM is an extra mode to include some stream algorithms like WAKE or RC4.
2楼高手 是否可讲清楚点我是想解密 java里加密过来的des的密文, 唉, 郁闷啊
function idtag_des_decode($key,$encrypted)
{
$encrypted = base64_decode($encrypted); $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,''); //使用MCRYPT_DES算法,cbc模式
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);
mcrypt_generic_init($td, $key, $key); //初始处理 $decrypted = mdecrypt_generic($td, $encrypted); //解密 mcrypt_generic_deinit($td); //结束
mcrypt_module_close($td); $y=pkcs5_unpad($decrypted);
return $y;
}function idtag_des_encode($key,$text)
{ $y=pkcs5_pad($text); $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,''); //使用MCRYPT_DES算法,cbc模式
/// $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);
mcrypt_generic_init($td, $key, $key); //初始处理
$encrypted = mcrypt_generic($td, $y); //解密
mcrypt_generic_deinit($td); //结束
mcrypt_module_close($td); return base64_encode($encrypted);
}function pkcs5_pad($text,$block=8)
{
$pad = $block - (strlen($text) % $block);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) return $text;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return $text;
return substr($text, 0, -1 * $pad);
}$key = 'ABCD1234';
$str = 'JAVA DES 123';
$encstr = idtag_des_encode($key,$str);//$tmp = "BJ1g7SNWtIAcmlrdN+Ftaw==";
$ys=idtag_des_decode($key,$encstr);
echo $ys;
cbc ecb?
MCRYPT_MODE_CBC (cipher block chaining) is especially suitable for encrypting files where the security is increased over ECB significantly.
MCRYPT_MODE_CFB (cipher feedback) is the best mode for encrypting byte streams where single bytes must be encrypted.
MCRYPT_MODE_OFB (output feedback, in 8bit) is comparable to CFB, but can be used in applications where error propagation cannot be tolerated. It's insecure (because it operates in 8bit mode) so it is not recommended to use it.
MCRYPT_MODE_NOFB (output feedback, in nbit) is comparable to OFB, but more secure because it operates on the block size of the algorithm.
MCRYPT_MODE_STREAM is an extra mode to include some stream algorithms like WAKE or RC4.
结果如下:$input="JAVADES123";
$key = "12345678";
PHP des 密文:eBNDnJT8cWfd922muAxfLw==
java des密文: lAyrJ7vmnKcjrdKga+BilA==不知为什么 ?
而且直接那 java 的密文" lAyrJ7vmnKcjrdKga+BilA== " 来解密 ,解出来也是乱码