Evaluate the compatibility with the des encryption and decryption functions or classes of PHP5.5 and 5.6

Source: Internet
Author: User
Evaluate the compatibility with the des encryption and decryption functions or classes of PHP5.5 and 5.6
 Key = $ key; if ($ iv = 0) {$ this-> iv = $ key;} else {$ this-> iv = $ iv; // mcrypt_create_iv (encrypt (MCRYPT_DES, decrypt), MCRYPT_DEV_RANDOM) ;}} function encrypt ($ str) {// encrypted, returns an uppercase hexadecimal string $ size = encrypt (MCRYPT_DES, MCRYPT_MODE_CBC); $ str = $ this-> pkcs5Pad ($ str, $ size); return strtoupper (bin2hex (mcrypt_cbc (MCRYPT_DES, $ this-> key, $ str, MCRYPT_ENCRYPT, $ This-> iv);} function decrypt ($ str) {// decrypt $ strBin = $ this-> hex2bin (strtolower ($ str )); $ str = mcrypt_cbc (MCRYPT_DES, $ this-> key, $ strBin, MCRYPT_DECRYPT, $ this-> iv); $ str = $ this-> pkcs5Unpad ($ str ); return $ str;} function hex2bin ($ hexData) {$ binData = ""; for ($ I = 0; $ I <strlen ($ hexData); $ I + = 2) {$ binData. = chr (hexdec (substr ($ hexData, $ I, 2);} return $ binData ;} Function pkcs5Pad ($ text, $ blocksize) {$ pad = $ blocksize-(strlen ($ text) % $ blocksize); return $ text. str_repeat (chr ($ pad), $ pad);} function pkcs5Unpad ($ text) {$ pad = ord ($ text {strlen ($ text)-1 }); if ($ pad> strlen ($ text) return false; if (strspn ($ text, chr ($ pad), strlen ($ text)-$ pad )! = $ Pad) return false; return substr ($ text, 0,-1 * $ pad) ;}$ my_encrypt = new DES ('qwertyuiop '); echo $ my_encrypt-> encrypt ('20140901 ');

None of the above found on the Internet is correct. it seems that there are also various errors in testing due to the PHP version. I don't know how to change it.


Reply to discussion (solution)

Try this: http://www.cnblogs.com/wangchuang/archive/2012/04/23/2466002.html

The test results described above:

Notice: Uninitialized string offset: 6 in C: \ web \ project1 \ web \ dec. php on line 148

Notice: Uninitialized string offset: 7 in C: \ web \ project1 \ web \ dec. php on line 148

Notice: Uninitialized string offset: 6 in C: \ web \ project1 \ web \ dec. php on line 148

Notice: Uninitialized string offset: 7 in C: \ web \ project1 \ web \ dec. php on line 148
MDN2IAFG6iMABKVbI2l + KA =
Hello levenPHP Notice: Uninitialized string offset: 6 in C: \ web \ project1 \ web \ dec. php on line 148 PHP Notice: Uninitialized string offset: 7 in C: \ web \ project1 \ web \ dec. php on line 148 PHP Notice: Uninitialized string offset: 6 in C: \ web \ project1 \ web \ dec. php on line 148 PHP Notice: Uninitialized string offset: 7 in C: \ web \ project1 \ web \ dec. php on line 148

function encrypt($str, $key)  {      $block = mcrypt_get_block_size('des', 'ecb');      $pad = $block - (strlen($str) % $block);      $str .= str_repeat(chr($pad), $pad);      return mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);}function decrypt($str, $key)  {        $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);      $block = mcrypt_get_block_size('des', 'ecb');      $pad = ord($str[($len = strlen($str)) - 1]);      return substr($str, 0, strlen($str) - $pad);  }  
Php5.6.3 passed
It is said to be compatible with java

Your Deprecated: Function mcrypt_cbc () is deprecated
Obviously, it cannot be used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.