Php des encryption solution (for. net and JAVA)

Source: Internet
Author: User
Php des encryption solution (for. net and JAVA)
/***** Des. NET version * @ author Administrator **/class DES_NET {var $ key; var $ iv; // offset function DES_NET ($ key, $ iv = 0) {// key Length: 8 for example: 1234 abcd $ this-> key = $ key; if ($ iv = 0) {$ this-> iv = $ key; // $ key is used as the default value.} else {$ this-> iv = $ iv; // mcrypt_create_iv (mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM );}} function encrypt ($ str) {// encrypted, returns an uppercase hexadecimal string $ size = mcrypt_get _ Block_size (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;} func Tion 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> st Rlen ($ text) return false; if (strspn ($ text, chr ($ pad), strlen ($ text)-$ pad )! = $ Pad) return false; return substr ($ text, 0,-1 * $ pad );}} /***** des java version * @ author Administrator **/class DES_JAVA {var $ key; function DES_JAVA ($ key) {$ this-> key = $ key ;} function encrypt ($ encrypt) {$ encrypt = $ this-> pkcs5_pad ($ encrypt); $ iv = mcrypt_create_iv (encrypt (MCRYPT_DES, MCRYPT_MODE_ECB), MCRYPT_RAND ); $ passcrypt = mcrypt_encrypt (MCRYPT_DES, $ this-> key, $ encrypt, MCRYPT _ MODE_ECB, $ iv); return strtoupper (bin2hex ($ passcrypt);} function decrypt ($ decrypt) {// $ decoded = base64_decode ($ decrypt ); $ decoded = pack ("H *", $ decrypt); $ iv = mcrypt_create_iv (Encrypt (MCRYPT_DES, encrypt), MCRYPT_RAND); $ decrypted = mcrypt_decrypt (MCRYPT_DES, $ this-> key, $ decoded, MCRYPT_MODE_ECB, $ iv); return $ this-> pkcs5_unpad ($ decrypted);} function pkcs5_unpad ($ text) {$ pad = or D ($ 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);} function pkcs5_pad ($ text) {$ len = strlen ($ text ); $ mod = $ len % 8; $ pad = 8-$ mod; return $ text. str_repeat (chr ($ pad), $ pad );}}

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.