DES加密算法
des对称加密,是一种比较传统的加密方式,其加密运算、解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码),是一种对称加密算法。
这里直接贴代码:
加密结果为Base64字符串。
C#版本DES加密方法 :
/// <summary> /// 加密 算法 /// 参考,C# php 通用加密算法。 Des加密 /// http://www.it1352.com/422514.html /// </summary> public class DESEncrypt { /// <summary> /// 加密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Encrypt(string Text) { return Encrypt(Text, "abcd1234");// 这里更新为一个至少8位长度的密码 } public static string Encrypt(string strText, string decryptKey) { string outString = ""; string result = string.Empty; try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Mode = CipherMode.ECB; byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); des.Key = ASCIIEncoding.ASCII.GetBytes(decryptKey.Substring(0, 8));//8位 偏移向量,一般为8位。 des.IV = ASCIIEncoding.ASCII.GetBytes(decryptKey.Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); byte[] bytelist = ms.ToArray(); outString = Convert.ToBase64String(bytelist);// 转base64位输出 //StringBuilder ret = new StringBuilder(); //foreach (byte b in ms.ToArray()) //{ // ret.AppendFormat("{0:X2}", b); //} //return ret.ToString(); } catch (System.Exception) { outString = ""; } return outString; } #region ========解密======== /// <summary> /// 解密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Decrypt(string Text) { return Decrypt(Text, "abcd1234"); } /// <summary> /// 解密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Decrypt(string Text, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Mode = CipherMode.ECB; //int len; //len = Text.Length / 2; //byte[] inputByteArray = new byte[len]; //int x, i; //for (x = 0; x < len; x++) //{ // i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); // inputByteArray[x] = (byte)i; //} byte[] inputByteArray; inputByteArray = Convert.FromBase64String(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey.Substring(0, 8));//8位 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey.Substring(0, 8)); //des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); //des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } #endregion }
PHP版本的des加密代码:
<?php
/* * DES加密、解密 * To change this template file, choose Tools | Templates * and open the template in the editor. */ /* 秘钥:abcd1234,des加密ecb模式。 输入: 输出: */ class Des { public static function encrypt($key, $input) { $size = mcrypt_get_block_size('des', 'ecb'); // $size = 8 ; $input = self::pkcs5_pad($input, $size); // // $key = $this->key; $td = mcrypt_module_open('des', '', 'ecb', ''); $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); //向量随机 @mcrypt_generic_init($td, $key, $iv); $data = mcrypt_generic($td, $input); mcrypt_generic_deinit($td); mcrypt_module_close($td); $data = base64_encode($data); return $data; } public static function decrypt($key, $encrypted) { $encrypted = base64_decode($encrypted); // $key =$this->key; $td = mcrypt_module_open('des','','ecb',''); //使用MCRYPT_DES算法,cbc模式 $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); $ks = mcrypt_enc_get_key_size($td); @mcrypt_generic_init($td, $key, $iv); //初始处理 $decrypted = mdecrypt_generic($td, $encrypted); //解密 mcrypt_generic_deinit($td); //结束 mcrypt_module_close($td); $y= self::pkcs5_unpad($decrypted); return $y; } public static function pkcs5_pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } public static 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); }
}