关于PHP的MCRYPT的解密问题

WBOY
Release: 2016-06-23 14:00:23
Original
923 people have browsed it

最近想找一个PHP的密匙可逆加密函数

几经周折找到了MCRYPT。并在网上找到了一个DEMO

<?php // Designate string to be encrypted $string = "song"; // Encryption/decryption key $key = "memeda"; // Encryption Algorithm $cipher_alg = MCRYPT_RIJNDAEL_128; // Create the initialization vector for added security. $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg, MCRYPT_MODE_ECB), MCRYPT_RAND); // Output original string print "Original string: $string <p>"; // Encrypt $string $encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_CBC, $iv); // Convert to hexadecimal and output to browser print "Encrypted string: ".bin2hex($encrypted_string)."<p>"; $decrypted_string = mcrypt_decrypt($cipher_alg, $key, $encrypted_string, MCRYPT_MODE_CBC, $iv); print "Decrypted string: $decrypted_string"; ?> 
Copy after login


原程序当然可以直接加解密

但是把转换后的16进制的字符串还原成2进制的密文去解密的时候却发现不能解密了

经过对比发现还原成2进制后的密文和直接加密完的密文是一样的,为什么不能解密就很困惑了 希望有人能帮我解答下哈


回复讨论(解决方案)

$iv  的值改变了

$iv  的值改变了

这样的话有解决办法么

储存的时候要同时储存$IV?

是的,这是公开密钥加密算法
有两个密钥
一个是公钥,即 iv 内置于你提供的加解密程序中
一个是私钥,提供给使用者

是的,这是公开密钥加密算法
有两个密钥
一个是公钥,即 iv 内置于你提供的加解密程序中
一个是私钥,提供给使用者

谢谢了那有没有什么函数是可逆的crypt(str,key)这样的呢。。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!