PHP--des plus decryption function compatible with Java (episode)

Source: Internet
Author: User
Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/46045127

PHP can use mcrypt_encrypt for DES encryption and decryption, but actually operation, you will find it with Java des encryption out of the string, a little different. Basically the first half of the paragraph, the second half is different. Find the PHP official website on this function of the document, someone replied, and gave the code. It is tested so that encryption and decryption are compatible with Java.

<?php 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);}? >  


PHP--des plus decryption function compatible with Java (episode)

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.