java源码:
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; //定义 加密算法,可用 des,desede,blowfish //keybyte为加密密钥,长度为24字节 //src为被加密的数据缓冲区(源) public static byte[] encryptmode(byte[] keybyte, byte[] src) { try { //生成密钥 secretkey deskey = new secretkeyspec(keybyte, algorithm); //加密 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为加密密钥,长度为24字节 //src为加密后的缓冲区 public static byte[] decryptmode(byte[] keybyte, byte[] src) { try { //生成密钥 secretkey deskey = new secretkeyspec(keybyte, algorithm); //解密 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; } //转换成十六进制字符串 public static string byte2hex(byte[] b) { string hs=; string stmp=; for (int n=0;n
来源:http://www.cnblogs.com/mailin...
回复内容: java源码:
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; //定义 加密算法,可用 des,desede,blowfish //keybyte为加密密钥,长度为24字节 //src为被加密的数据缓冲区(源) public static byte[] encryptmode(byte[] keybyte, byte[] src) { try { //生成密钥 secretkey deskey = new secretkeyspec(keybyte, algorithm); //加密 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为加密密钥,长度为24字节 //src为加密后的缓冲区 public static byte[] decryptmode(byte[] keybyte, byte[] src) { try { //生成密钥 secretkey deskey = new secretkeyspec(keybyte, algorithm); //解密 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; } //转换成十六进制字符串 public static string byte2hex(byte[] b) { string hs=; string stmp=; for (int n=0;n
来源:http://www.cnblogs.com/mailin...
已解决 自问自答
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