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="">