Des encryption and decryption--java encryption, PHP decryption

Source: Internet
Author: User
Tags decrypt

Recently in the project, the issue of DES Encryption and decryption was encountered.

Scene is the Android app side with Des encryption, PHP this side need to decrypt. No prior contact with Des this encryption and decryption algorithm, but think there will definitely be demo. So Baidu, search the code to use. Online code is also a mixed bag, and finally test the PHP side test encryption and decryption success. In order to ensure that the encryption parameters submitted by the Android app can be decrypted, give a string to the Zhuo people, let them put the Des encrypted string to me, decrypted on the side of PHP. As a result, the encrypted string is inconsistent with the result of my encryption, which is naturally not decrypted.

To Java des Algorithm code, study the process of encryption, including various debugging tests, plus a variety of Baidu, Bing. Find the rules that can be decrypted correctly, the very important three points is the encryption and decryption process, The two sides of the key, encryption mode (such as the ECB, CBC, etc.), as well as IV(some places called it offset, some places called it vector, not in-depth study) need to be consistent . It should be emphasized that this IV, in the ECB encryption mode (Java default encryption mode), does not need this IV, even if it is written, does not affect the results of encryption, and when the encryption mode is CBC, you need the IV parameter, otherwise the parameter will be randomly generated, so that the results of each encryption will change. The key is that the Java side defines the IV, then the PHP side also needs to be consistent with the Java side, so that it can be correctly decrypted.

The back is on top of this IV. View Java side Code, IV is a byte[], that is, a byte array, want to go online to find PHP to convert the string to byte[] type, also tried the cast, decryption failed. And then I think about it. PHP data types, uh, uh, there's no type at all. The foundation is not strong, and take it for granted, think Java has this data type, PHP will have ... Inertial thinking really harm ah. What about this? Need byte[] to decrypt, without the data data type, already feel no solution.

Usually this time, need to rest, rest for a while. After a sudden thought flashed, PHP has a function can be des decryption, the function will not be decrypted with PHP data type, so I tried to convert Java encryption into byte[] type before the string as PHP IV, test, finally decryption success.

Attach the PHP-side decryption code (where Java uses CBC encryption mode):

classdes{ Public Static functionDecrypt$str,$key)    {        $midstr= Hex2bin (Strtolower($str)); //The second parameter, $key, is the $key in the three focus, and the last parameter $key is IV, but Java encryption takes the same string as the second parameter, which is fine depending on the situation .       $str= Mcrypt_decrypt (Mcrypt_des,$key,$midstr,MCRYPT_MODE_CBC,$key);
$pad=Ord($str[($len=strlen($str))-1]); return substr($str, 0,strlen($str) -$pad); } }

Where Hex2bin is to convert 16 binary to binary, PHP comes with the function, no need to define (see many of the function's code on the net). Decryption can be seen as an inverse operation of encryption, so the Java side will encrypt the string into 16, and uppercase, decryption naturally need to convert back.

Java-side encryption code, added tomorrow.

Can look at the following link, well written:

Http://zhidao.baidu.com/link?url=vKbwbKxibbLc8K7oo40cJ17aYz2i2MdfZlaytdj6GLsqBwrPoc_ Owhhsi7ijdafid9fzb52skhddbg_u502cknycqdpe3zskr-adxb46ceo

Des encryption and decryption--java encryption, PHP decryption

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.