How PHP 3DES encryption is compatible with Java

Source: Internet
Author: User
Tags decrypt
Java source code:

Import Java.security.security;import Javax.crypto.cipher;import Javax.crypto.secretkey;import Javax.crypto.spec.secretkeyspec;public class Threedes {private static final String algorithm = "Desede";//define encryption algorithm, available DES, Desede,blowfish//keybyte is the encryption key, the length is 24 bytes//src is the encrypted data buffer (source) public static byte[] Encryptmode (byte[] Keybyte, by            Te[] src) {try {//generate key Secretkey Deskey = new Secretkeyspec (keybyte, algorithm);            Encryption Cipher C1 = cipher.getinstance (algorithm);            C1.init (Cipher.encrypt_mode, Deskey);        return c1.dofinal (SRC);        } catch (Java.security.NoSuchAlgorithmException E1) {e1.printstacktrace ();        } catch (javax.crypto.NoSuchPaddingException E2) {e2.printstacktrace ();        } catch (Java.lang.Exception E3) {e3.printstacktrace ();    } return null; //keybyte is the encryption key, the length is 24 bytes//src is the encrypted buffer public static byte[] Decryptmode (byte[] keybyte, byte[] sRC) {try {//generate key Secretkey Deskey = new Secretkeyspec (keybyte, algorithm);            Decrypt Cipher C1 = cipher.getinstance (algorithm);            C1.init (Cipher.decrypt_mode, Deskey);        return c1.dofinal (SRC);        } catch (Java.security.NoSuchAlgorithmException E1) {e1.printstacktrace ();        } catch (javax.crypto.NoSuchPaddingException E2) {e2.printstacktrace ();        } catch (Java.lang.Exception E3) {e3.printstacktrace ();    } return null;        }//Convert to a hexadecimal string public static string Byte2hex (byte[] b) {string hs= "";        String stmp= ""; for (int n=0;n
  
   
   < code="">
  

来源:http://www.cnblogs.com/mailin...

Reply content:

Java source code:

Import Java.security.security;import Javax.crypto.cipher;import Javax.crypto.secretkey;import Javax.crypto.spec.secretkeyspec;public class Threedes {private static final String algorithm = "Desede";//define encryption algorithm, available DES, Desede,blowfish//keybyte is the encryption key, the length is 24 bytes//src is the encrypted data buffer (source) public static byte[] Encryptmode (byte[] Keybyte, by            Te[] src) {try {//generate key Secretkey Deskey = new Secretkeyspec (keybyte, algorithm);            Encryption Cipher C1 = cipher.getinstance (algorithm);            C1.init (Cipher.encrypt_mode, Deskey);        return c1.dofinal (SRC);        } catch (Java.security.NoSuchAlgorithmException E1) {e1.printstacktrace ();        } catch (javax.crypto.NoSuchPaddingException E2) {e2.printstacktrace ();        } catch (Java.lang.Exception E3) {e3.printstacktrace ();    } return null; //keybyte is the encryption key, the length is 24 bytes//src is the encrypted buffer public static byte[] Decryptmode (byte[] keybyte, byte[] sRC) {try {//generate key Secretkey Deskey = new Secretkeyspec (keybyte, algorithm);            Decrypt Cipher C1 = cipher.getinstance (algorithm);            C1.init (Cipher.decrypt_mode, Deskey);        return c1.dofinal (SRC);        } catch (Java.security.NoSuchAlgorithmException E1) {e1.printstacktrace ();        } catch (javax.crypto.NoSuchPaddingException E2) {e2.printstacktrace ();        } catch (Java.lang.Exception E3) {e3.printstacktrace ();    } return null;        }//Convert to a hexadecimal string public static string Byte2hex (byte[] b) {string hs= "";        String stmp= ""; for (int n=0;n
   
    
    < code="">
   

来源:http://www.cnblogs.com/mailin...

has resolved to ask self-answer

Class Easycrypt3des {Private $_key = "HelloWorld";    Public function __construct ($key =null) {if ($key!== null) $this->_key = $key;        The Public function encrypt ($str) {$td = $this->gettd ();        $ret = Mcrypt_generic ($TD, $this->pkcs5_pad ($STR, 8));        Mcrypt_generic_deinit ($TD);        Mcrypt_module_close ($TD);    return $this->strtohex ($ret);        The public function decrypt ($str) {$td = $this->gettd ();        $ret = $this->pkcs5_unpad (mdecrypt_generic ($TD, $this->hextostr ($STR)));        Mcrypt_generic_deinit ($TD);        Mcrypt_module_close ($TD);    return $ret;        } Private Function Pkcs5_pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize); Return $text.    Str_repeat (Chr ($pad), $pad);        } Private Function Pkcs5_unpad ($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);    } Private Function Getiv () {return Pack (' H16 ', ' 0000000000000000 ');        } Private Function Gettd () {$iv = $this->getiv ();        $TD = Mcrypt_module_open (Mcrypt_3des, ', MCRYPT_MODE_ECB, ');        Mcrypt_generic_init ($TD, $this->_key, $IV);    return $TD;        } Private Function Strtohex ($string) {$hex = '; for ($i =0; $i
    
     < strlen($hex)-1;="" $i+="2){" $string;="">< code="">
    
  • Related Article

    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.