[WPF]C#和php实现DES加密和解密
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2017-08-26 22:01:35
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
加密解密的时候如果只传一个key值那么向量值默认跟key值一样
c#中实现加密和解密的类
Des.cs
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Ank.Class { class Des { ////加解密密钥 //private static string skey = "12345678"; ////初始化向量 //private static byte[] DESIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; #region DESEnCode DES加密 public static string EnCode(string pToEncrypt, string sKey, string sIv = null) { try { if (sKey.Length < 8) { sKey = sKey.PadRight(8, '0'); } else { sKey = sKey.Substring(0, 8); } if (sIv == null) { sIv = sKey; } DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt); //建立加密对象的密钥和偏移量 //原文使用ASCIIEncoding.ASCII方法的GetBytes方法 //使得输入密码必须输入英文文本 des.Key = Encoding.ASCII.GetBytes(sKey); des.IV = Encoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } return ret.ToString(); } catch (Exception ex) { Console.WriteLine(ex.Message); return ""; } } #endregion /// pToDecrypt待解密的字符串</param> /// sKey 解密密钥,要求为8字节,和加密密钥相同</param> /// returns 解密成功返回解密后的字符串,失败返源串</returns> #region DESDeCode DES解密 public static string DeCode(string pToDecrypt, string sKey, string sIv = null) { try { if (sKey.Length < 8) { sKey = sKey.PadRight(8, '0'); } else { sKey = sKey.Substring(0, 8); } if (sIv == null) { sIv = sKey; } DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } catch (Exception ex) { Console.WriteLine(ex.Message); return ""; } } #endregion } }
php中进行加密解密的类
class DES { var $crypto = null; var $key; var $iv; //偏移量 function DES($key, $iv = null) { //key长度8例如:1234abcd $this->crypto = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, ''); $key = substr($key, 0, 8); if (is_null($iv)) { $this->iv = $key; //mcrypt_create_iv(mcrypt_enc_get_iv_size($this->crypto), MCRYPT_DEV_URANDOM); } else { $this->iv = $iv; } $this->key = $key; } function encrypt($str) { //加密,返回大写十六进制字符串 $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC); $str = $this->pkcs5Pad($str, $size); mcrypt_generic_init($this->crypto, $this->key, $this->iv); $enc_question = mcrypt_generic($this->crypto, $str); mcrypt_generic_deinit($this->crypto); return strtoupper(bin2hex($enc_question)); } function decrypt($str) { //解密 $strBin = $this->hex2bin(strtolower($str)); mcrypt_generic_init($this->crypto, $this->key, $this->iv); $str = mdecrypt_generic($this->crypto, $strBin); mcrypt_generic_deinit($this->crypto); $str = $this->pkcs5Unpad($str); return $str; } function hex2bin($hexData) { $binData = ""; for ($i = 0; $i < strlen($hexData); $i += 2) { $binData .= chr(hexdec(substr($hexData, $i, 2))); } return $binData; } function pkcs5Pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } function pkcs5Unpad($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); } function __destruct() { // mcrypt_generic_deinit($this->crypto); mcrypt_module_close($this->crypto); } } $str = "test string"; $key = '123412349'; $crypt = new DES($key); $mstr = $crypt->encrypt($str); echo "[ $str ]加密:[ $mstr ]<br>"; $str = $crypt->decrypt($mstr); echo "[ $mstr ]解密:[ $str ]<br>";