php5.x版本,要添加php扩展php_mcrypt。
PHP版:
class STD3Des<BR> {<BR> private $key = "";<BR> private $iv = "";<br><br> /**<BR> * 构造,传递二个已经进行base64_encode的KEY与IV<BR> *<BR> * @param string $key<BR> * @param string $iv<BR> */<BR> function __construct ($key, $iv)<BR> {<BR> if (empty($key) || empty($iv)) {<BR> echo 'key and iv is not valid';<BR> exit();<BR> }<BR> $this->key = $key;<BR> $this->iv = $iv;<BR> }<br><br> /**<BR> *加密<BR> * @param $value<BR> * @return <BR> */<BR> public function encrypt ($value)<BR> {<BR> $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');<BR> $iv = base64_decode($this->iv);<BR> $value = $this->PaddingPKCS7($value);<BR> $key = base64_decode($this->key);<BR> mcrypt_generic_init($td, $key, $iv);<BR> $ret = base64_encode(mcrypt_generic($td, $value));<BR> mcrypt_generic_deinit($td);<BR> mcrypt_module_close($td);<BR> return $ret;<BR> }<br><br> /**<BR> *解密<BR> * @param $value<BR> * @return <BR> */<BR> public function decrypt ($value)<BR> {<BR> $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');<BR> $iv = base64_decode($this->iv);<BR> $key = base64_decode($this->key);<BR> mcrypt_generic_init($td, $key, $iv);<BR> $ret = trim(mdecrypt_generic($td, base64_decode($value)));<BR> $ret = $this->UnPaddingPKCS7($ret);<BR> mcrypt_generic_deinit($td);<BR> mcrypt_module_close($td);<BR> return $ret;<BR> }<br><br> private function PaddingPKCS7 ($data)<BR> {<BR> <a style="color:transparent">来@源gao*daima.com搞@代#码网</a><strong>搞gaodaima代码</strong> $block_size = mcrypt_get_block_size('tripledes', 'cbc');<BR> $padding_char = $block_size - (strlen($data) % $block_size);<BR> $data .= str_repeat(chr($padding_char), $padding_char);<BR> return $data;<BR> }<br><br> private function UnPaddingPKCS7($text)<BR> {<BR> $pad = ord($text{strlen($text) - 1});<BR> if ($pad > strlen($text)) {<BR> return false;<BR> }<BR> if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {<BR> return false;<BR> }<BR> return substr($text, 0, - 1 * $pad);<BR> }<BR> }<br><br> <BR> //使用<BR> include('STD3Des.class.php');<BR> $key='abcdefgh';<BR> $iv='abcdefgh';<BR> $msg='test string';<BR> $des=new STD3Des(base64_encode($key),base64_encode($iv));<BR> $rs1=$des->encrypt($msg);<BR> echo $rs1.'<br />';<BR> $rs2=$des->decrypt($rs1);<BR> echo $rs2;<BR>
.net版本
sealed public class CryptoHelper<BR> {<BR> /// <summary><BR> /// Encrypts the specified input.<BR> /// </summary><BR> /// The input.<BR> /// key<BR> /// iv<BR> /// <BR> public static string EncryptDes(string input, byte[] key, byte[] iv)<BR> {<BR> if (input == null || input.Length == 0)<BR> return String.Empty;<br><br> DESCryptoServiceProvider des = new DESCryptoServiceProvider();<BR> MemoryStream ms = null;<BR> CryptoStream encStream = null;<BR> StreamWriter sw = null;<BR> string result = String.Empty;<br><br> try<BR> {<BR> ms = new MemoryStream();<br><br> // Create a CryptoStream using the memory stream and the <BR> // CSP DES key. <BR> //des.Mode = CipherMode.CBC;<BR> //des.Padding = PaddingMode.PKCS7; <BR> encStream = new CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write);<br><br> // Create a StreamWriter to write a string<BR> // to the stream.<BR> sw = new StreamWriter(encStream);<br><br> // Write the plaintext to the stream.<BR> sw.Write(input);<br><br> sw.Flush();<BR> encStream.FlushFinalBlock();<BR> ms.Flush();<br><br> <BR> result = Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length, CultureInfo.InvariantCulture));<BR> }<BR> finally<BR> {<BR> //close objects<BR> if (sw != null)<BR> sw.Close();<BR> if (encStream != null)<BR> encStream.Close();<BR> if (ms != null)<BR> ms.Close();<BR> }<br><br> // Return the encrypted string<BR> return result;<BR> }<BR> /// <summary><BR> /// Decrypts the specified input.<BR> /// </summary><BR> /// the input.<BR> /// key<BR> /// iv<BR> /// <BR> public static string DecryptDes(string input, byte[] key, byte[] iv)<BR> {<BR> byte[] buffer;<BR> try { buffer = Convert.FromBase64String(input); }<BR> catch (System.ArgumentNullException) { return String.Empty; }<BR> // length is zero, or not an even multiple of four (plus a few other cases)<BR> catch (System.FormatException) { return String.Empty; }<br><br> DESCryptoServiceProvider des = new DESCryptoServiceProvider();<BR> MemoryStream ms = null;<BR> CryptoStream encStream = null;<BR> StreamReader sr = null;<BR> string result = String.Empty;<br><br> try<BR> {<BR> ms = new MemoryStream(buffer);<br><br> // Create a CryptoStream using the memory stream and the <BR> // CSP DES key. <BR> encStream = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);<br><br> // Create a StreamReader for reading the stream.<BR> sr = new StreamReader(encStream);<br><br> // Read the stream as a string.<BR> result = sr.ReadToEnd();<BR> }<BR> finally<BR> {<BR> //close objects<BR> if (sr != null)<BR> sr.Close();<BR> if (encStream != null)<BR> encStream.Close();<BR> if (ms != null)<BR> ms.Close();<BR> }<br><br> return result;<BR> }<BR> }<br><br> <BR> //调用<br><br> string key = "abcdefgh";<BR> string iv = "abcdefgh";<BR> string msg="test string";<BR> string rs1 = CryptoHelper.EncryptDes(msg, System.Text.Encoding.ASCII.GetBytes(key), System.Text.Encoding.ASCII.GetBytes(iv));<BR> string rs2 = CryptoHelper.DecryptDes(rs1, System.Text.Encoding.ASCII.GetBytes(key), System.Text.Encoding.ASCII.GetBytes(iv));<BR>