Java source code: {code...} Source: www. cnblogs. commailin... 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"; // defines the encryption Algorithm. DES, DESede, Blowfish // keybyte can be used as the encryption key, the length is 24 bytes // src is the encrypted data buffer (source) public static byte [] encryptMode (byte [] keybyte, byte [] src) {try {// Generate secret key secret EY = new SecretKeySpec (keybyte, Algorithm); // encrypt Cipher c1 = Cipher. getInstance (Algorithm); c1.init (Cipher. ENCRYPT_MODE, encryption EY); 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 secret key secret EY = new SecretKeySpec (keybyte, Algorithm); // decrypt Cipher c1 = Cipher. getInstance (Algorithm); c1.init (Cipher. DECRYPT_MODE, cipher EY); 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
Source: 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"; // defines the encryption Algorithm. DES, DESede, Blowfish // keybyte can be used as the encryption key, the length is 24 bytes // src is the encrypted data buffer (source) public static byte [] encryptMode (byte [] keybyte, byte [] src) {try {// Generate secret key secret EY = new SecretKeySpec (keybyte, Algorithm); // encrypt Cipher c1 = Cipher. getInstance (Algorithm); c1.init (Cipher. ENCRYPT_MODE, encryption EY); 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 secret key secret EY = new SecretKeySpec (keybyte, Algorithm); // decrypt Cipher c1 = Cipher. getInstance (Algorithm); c1.init (Cipher. DECRYPT_MODE, cipher EY); 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
Source: http://www.cnblogs.com/mailin...
Resolved self-answer
class EasyCrypt3Des { private $_key = "helloworld"; public function __construct($key=null) { if($key !== null) $this->_key = $key; } 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); } 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