public static byte[] DES_Dec(string str, string key, string vit) { //判断向量是否为空,进行默认赋值; if (string.IsNullOrEmpty(key)) { key = "KCCT"; } //判断向量是否为空,进行默认赋值; if (string.IsNullOrEmpty(vit)) { vit = "MNSN"; } try { //实例化加解密类的对象; using (var descsp = new System.Security.Cryptography.DESCryptoServiceProvider()) { //定义字节数组,用来存储要解密的字符串; var data = System.Convert.FromBase64String(str); //实例化内存流对象; using (var mStream = new System.IO.MemoryStream()) { //使用内存流实例化解密流对象; using (var cStream = new System.Security.Cryptography.CryptoStream(mStream, descsp.CreateDecryptor(System.Text.Encoding.Unicode.GetBytes(key), System.Text.Encoding.Unicode.GetBytes(vit)), System.Security.Cryptography.CryptoStreamMode.Write)) { //向解密流中写入数据; cStream.Write(data, 0, data.Length); //释放解密流; cStream.FlushFinalBlock(); //返回解密后的字符串; return mStream.ToArray(); } } } } catch { return null; } }
public static byte[] D1(byte[] data) { var stream = new MemoryStream(); var des = new System.Security.Cryptography.DESCryptoServiceProvider(); des.Mode = System.Security.Cryptography.CipherMode.CBC; des.Padding = System.Security.Cryptography.PaddingMode.PKCS7; var cs = new System.Security.Cryptography.CryptoStream(stream, des.CreateDecryptor(ES.Key, ES.IV), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); return(stream.ToArray()); }
/// <summary> /// DES解密 /// </summary> public static string DesDecrypt(string encrypted) { //小于40位时,不是有效的加密串 if (encrypted.Length <= 40) { return(""); } else if (encrypted.Length % 2 != 0) { return(""); //非偶数位不是有效的加密串 } //32位MD5校验码 string _md5 = encrypted.Substring(0, 16); _md5 += encrypted.Substring(encrypted.Length - 16); //移除MD5 encrypted = encrypted.Substring(16); encrypted = encrypted.Substring(0, encrypted.Length - 16); //进行MD5验证是否被修改 //if (_md5 != System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(encrypted, "MD5")) if (_md5 != Md5(encrypted)) { return(""); } //截取,移除前8位向量长度 string iv = encrypted.Substring(0, 8); //移除向量值 encrypted = encrypted.Substring(8); //32位整型转换器 System.ComponentModel.Int32Converter _int32 = new System.ComponentModel.Int32Converter(); //字符串转换成数组 byte[] _datas = new byte[encrypted.Length / 2]; for (int i = 0; i < _datas.Length; i++) { _datas[i] = Convert.ToByte(_int32.ConvertFromInvariantString("0x" + encrypted.Substring(i * 2, 2))); } byte[] rgbKey = System.Text.Encoding.ASCII.GetBytes("SolinArt"); byte[] rgbIV = System.Text.Encoding.ASCII.GetBytes(iv); System.Security.Cryptography.DESCryptoServiceProvider _des = new System.Security.Cryptography.DESCryptoServiceProvider(); System.Security.Cryptography.ICryptoTransform _encrypt = _des.CreateDecryptor(rgbKey, rgbIV); byte[] _result = _encrypt.TransformFinalBlock(_datas, 0, _datas.Length); _encrypt.Dispose(); _des.Clear(); return(System.Text.Encoding.UTF8.GetString(_result)); }
/// <summary> /// 从加密的密钥交换数据中提取机密信息<br/> /// </summary> /// <param name="str">被加密的字符串</param> /// <param name="key">密钥</param> /// <returns>还原的字符串</returns> public static string DecryptString(string str, string key) { try { //义访问数据加密标准 (DES) 算法的加密服务提供程序 (CSP) 版本的包装对象. System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //对密钥进行编码. byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //初始化机密适配器.. des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //返回由以 64 为基的二进制数组 byte[] bytesIn = System.Convert.FromBase64String(str); System.IO.MemoryStream msIn = new System.IO.MemoryStream(bytesIn); //定义基本的加密转换运算. System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateDecryptor(); //定义将数据流链接到加密转换的流。. System.Security.Cryptography.CryptoStream cryptStreem = new System.Security.Cryptography.CryptoStream(msIn, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Read); //以UTF8编码从字节流中读取字符. System.IO.StreamReader srOut = new System.IO.StreamReader(cryptStreem, System.Text.Encoding.UTF8); //以UTF8编码从字节流中读取字符. string result = srOut.ReadToEnd(); srOut.Close(); cryptStreem.Close(); msIn.Close(); return result; } catch (Exception ep) { throw (ep); // return ""; } }
/// <summary> /// DES解密 /// </summary> /// <param name="input">待解密字串</param> /// <param name="key">解密key</param> /// <returns></returns> private static string DESDecrypt(string input, string key) { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); des.Key = Encoding.UTF8.GetBytes(key); des.IV = new byte[8]; System.Security.Cryptography.ICryptoTransform cTransform = des.CreateDecryptor(des.Key, des.IV); System.IO.MemoryStream mStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, cTransform, System.Security.Cryptography.CryptoStreamMode.Write); byte[] byt = Convert.FromBase64String(input); cStream.Write(byt, 0, byt.Length); cStream.FlushFinalBlock(); cStream.Close(); return(Encoding.UTF8.GetString(mStream.ToArray())); }
/// <summary> /// 暗号化された文字列を復号化する /// </summary> /// <param name="str">暗号化された文字列</param> /// <param name="key">パスワード</param> /// <returns>復号化された文字列</returns> public static string DecryptString(string str, string key) { if (str == null) { return(""); } if (str.Length % 4 > 0) { while (str.Length % 4 != 0) { str += "="; } } //DESCryptoServiceProviderオブジェクトの作成 System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //共有キーと初期化ベクタを決定 //パスワードをバイト配列にする byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //共有キーと初期化ベクタを設定 des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //Base64で文字列をバイト配列に戻す byte[] bytesIn = System.Convert.FromBase64String(str); //暗号化されたデータを読み込むためのMemoryStream MemoryStream msIn = new System.IO.MemoryStream(bytesIn); //DES復号化オブジェクトの作成 System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateDecryptor(); //読み込むためのCryptoStreamの作成 System.Security.Cryptography.CryptoStream cryptStreem = new System.Security.Cryptography.CryptoStream(msIn, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Read); //復号化されたデータを取得するためのStreamReader StreamReader srOut = new StreamReader(cryptStreem, System.Text.Encoding.UTF8); //復号化されたデータを取得する string result = srOut.ReadToEnd(); //閉じる srOut.Close(); cryptStreem.Close(); msIn.Close(); return(result); }
//DecryptFile public static System.IO.StreamReader DecryptFile(string inputfilename, string key) { System.IO.StreamReader streamreader = null; try { System.Security.Cryptography.DESCryptoServiceProvider DESCtoSP = new System.Security.Cryptography.DESCryptoServiceProvider(); DESCtoSP.Key = ASCIIEncoding.ASCII.GetBytes(key); DESCtoSP.IV = ASCIIEncoding.ASCII.GetBytes(key); System.IO.FileStream filestream = new System.IO.FileStream(inputfilename, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.Security.Cryptography.ICryptoTransform ICtoTf = DESCtoSP.CreateDecryptor(); System.Security.Cryptography.CryptoStream cryptostream = new System.Security.Cryptography.CryptoStream(filestream, ICtoTf, System.Security.Cryptography.CryptoStreamMode.Read); streamreader = new System.IO.StreamReader(cryptostream); } catch (System.IO.FileNotFoundException ex) { System.Windows.Forms.MessageBox.Show("Connect error 009: " + ex.Message, "Notifications", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(streamreader); }
/// <summary> /// 解密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> private static string Decrypt(string Text, string sKey) { try { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); 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; } 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(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } catch { return Text; } }
/// <summary> /// 使用DES解密 /// </summary> /// <param name="encryptedValue">待解密的字符串</param> /// <param name="key">密钥(最大长度8)</param> /// <param name="IV">m初始化向量(最大长度8)</param> /// <returns>解密后的字符串</returns> public static string DESDecrypt(string encryptedValue, string key) { if (string.IsNullOrEmpty(encryptedValue)) { return(""); } try { //将key和IV处理成8个字符 key += "12345678"; string IV = key + "12345678"; key = key.Substring(0, 8); IV = IV.Substring(0, 8); System.Security.Cryptography.SymmetricAlgorithm sa; System.Security.Cryptography.ICryptoTransform ct; System.IO.MemoryStream ms; System.Security.Cryptography.CryptoStream cs; byte[] byt; sa = new System.Security.Cryptography.DESCryptoServiceProvider(); sa.Key = System.Text.Encoding.UTF8.GetBytes(key); sa.IV = System.Text.Encoding.UTF8.GetBytes(IV); ct = sa.CreateDecryptor(); byt = Convert.FromBase64String(encryptedValue); ms = new System.IO.MemoryStream(); cs = new System.Security.Cryptography.CryptoStream(ms, ct, System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); cs.Close(); return(System.Text.Encoding.UTF8.GetString(ms.ToArray())); } catch { } return(""); }
private static string DesDecrypt(string DecryptString) { try { byte[] inputByteArray = Convert.FromBase64String(DecryptString); System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); des.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; des.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; System.IO.MemoryStream mStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(System.Text.Encoding.UTF8.GetString(mStream.ToArray())); } catch { return(""); } }
public static string Decrypt_Des(this object value, string desKey, string desIV) { string cryptedString = value as string; byte[] Key = GetBytes(desKey); byte[] IV = GetBytes(desIV); // Check arguments. if (string.IsNullOrEmpty(cryptedString)) throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV"); System.Security.Cryptography.DESCryptoServiceProvider cryptoProvider = new System.Security.Cryptography.DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString)); System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(Key, IV), System.Security.Cryptography.CryptoStreamMode.Read); StreamReader reader = new StreamReader(cryptoStream); return reader.ReadToEnd(); }
public string DecryptFile(string FileName) { System.IO.FileStream fs = null; string s = string.Empty; try { string sKey = GenerateKey(); System.Security.Cryptography.DESCryptoServiceProvider DES = new System.Security.Cryptography.DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); System.IO.FileInfo dirFile = new System.IO.FileInfo(FileName); if (!dirFile.Exists) { return(string.Empty); } fs = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.Security.Cryptography.ICryptoTransform desdecrypt = DES.CreateDecryptor(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(fs, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Read); s = new System.IO.StreamReader(cs).ReadToEnd(); //Return fs.Close(); } catch (Exception ex) { throw ex; } finally { if ((fs != null)) { fs.Close(); } } return(s); }
public static string Decrypt(string text) { string inputText = text; if (string.IsNullOrWhiteSpace(text)) { return(""); } if (inputText.Contains("-")) { inputText = inputText.Replace("-", ""); } if (inputText.Contains(" ")) { inputText = inputText.Replace(" ", ""); } if (inputText.Length % 2 != 0) { throw new ArgumentException("要解密的字符串长度无效。"); } byte[] byteArray = new byte[inputText.Length / 2]; for (int i = 0; i < byteArray.Length; i++) { byteArray[i] = byte.Parse(inputText.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber); } using (System.Security.Cryptography.DESCryptoServiceProvider desCSP = new System.Security.Cryptography.DESCryptoServiceProvider()) { using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { System.Security.Cryptography.ICryptoTransform iCryptoTransform = desCSP.CreateDecryptor(KEY, IV); using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, iCryptoTransform, System.Security.Cryptography.CryptoStreamMode.Write)) { cryptoStream.Write(byteArray, 0, byteArray.Length); cryptoStream.FlushFinalBlock(); } return(Encoding.UTF8.GetString(memoryStream.ToArray())); } } }
/// <summary> /// 暗号化された文字列を復号化する /// </summary> /// <param name="str">暗号化された文字列</param> /// <param name="key">パスワード</param> /// <returns>復号化された文字列</returns> public static string DecryptString(string str, string key) { //DESCryptoServiceProviderオブジェクトの作成 var des = new System.Security.Cryptography.DESCryptoServiceProvider(); // 共有キーと初期化ベクタを決定 // パスワードをバイト配列にする var bytesKey = System.Text.Encoding.UTF8.GetBytes(key); // 共有キーと初期化ベクタを設定 des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); // Base64で文字列をバイト配列に戻す var bytesIn = System.Convert.FromBase64String(str); // 暗号化されたデータを読み込むためのMemoryStream var msIn = new System.IO.MemoryStream(bytesIn); // DES復号化オブジェクトの作成 var desdecrypt = des.CreateDecryptor(); // 読み込むためのCryptoStreamの作成 var cryptStreem = new System.Security.Cryptography.CryptoStream(msIn, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Read); // 復号化されたデータを取得するためのStreamReader var srOut = new System.IO.StreamReader(cryptStreem, System.Text.Encoding.UTF8); // 復号化されたデータを取得する var result = srOut.ReadToEnd(); // 閉じる srOut.Close(); cryptStreem.Close(); msIn.Close(); return result; }
/// <summary> /// 暗号化された文字列を復号化する /// </summary> /// <param name="str">暗号化された文字列</param> /// <param name="key">パスワード</param> /// <returns>復号化された文字列</returns> public static string DecryptString(string str, string key) { //DESCryptoServiceProviderオブジェクトの作成 var des = new System.Security.Cryptography.DESCryptoServiceProvider(); // 共有キーと初期化ベクタを決定 // パスワードをバイト配列にする var bytesKey = System.Text.Encoding.UTF8.GetBytes(key); // 共有キーと初期化ベクタを設定 des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); // Base64で文字列をバイト配列に戻す var bytesIn = System.Convert.FromBase64String(str); // 暗号化されたデータを読み込むためのMemoryStream var msIn = new System.IO.MemoryStream(bytesIn); // DES復号化オブジェクトの作成 var desdecrypt = des.CreateDecryptor(); // 読み込むためのCryptoStreamの作成 var cryptStreem = new System.Security.Cryptography.CryptoStream(msIn, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Read); // 復号化されたデータを取得するためのStreamReader var srOut = new System.IO.StreamReader(cryptStreem, System.Text.Encoding.UTF8); // 復号化されたデータを取得する var result = srOut.ReadToEnd(); // 閉じる srOut.Close(); cryptStreem.Close(); msIn.Close(); return(result); }
/// <summary> /// DES 解密 /// </summary> /// <param name="toDecryptString"></param> /// <returns></returns> public static string Decrypt(string toDecryptString) { string r = string.Empty; if (toDecryptString.IsNullOrWhiteSpace()) { return(string.Empty); } byte[] buffer = new byte[toDecryptString.Length]; buffer = Convert.FromBase64String(toDecryptString); System.Security.Cryptography.DESCryptoServiceProvider provider = initProvider(); // System.Security.Cryptography.ICryptoTransform cryptoTransform = provider.CreateEncryptor(sRgbKey, sRgbIV); // 为了兼容其他语言的Des加密算法 不使用 sRgbIV System.Security.Cryptography.ICryptoTransform cryptoTransform = provider.CreateDecryptor(); using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { using ( System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream ( stream: memoryStream, transform: cryptoTransform, mode: System.Security.Cryptography.CryptoStreamMode.Write ) ) { cryptoStream.Write(buffer, 0, buffer.Length); cryptoStream.FlushFinalBlock(); r = Encoding.UTF8.GetString(memoryStream.ToArray()); } } return(r); }
public static string Decrypt(string txt, string sKey) { System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider(); int len = txt.Length / 2; byte[] inputByteArr = new byte[len]; int i, j; for (i = 0; i < len; i++) { j = Convert.ToInt32(txt.Substring(i * 2, 2), 16); inputByteArr[i] = (byte)j; } dESCryptoServiceProvider.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); dESCryptoServiceProvider.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); //解密器 System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cryptoStream.Write(inputByteArr, 0, inputByteArr.Length); cryptoStream.FlushFinalBlock(); return(Encoding.Default.GetString(memoryStream.ToArray())); }
public static string DesDecrypt(string inputString, string decryptKey) { #region byte[] byKey = null; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; byte[] inputByteArray = new Byte[inputString.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8)); System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(inputString); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(byKey, IV), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = new System.Text.UTF8Encoding(); inputString = encoding.GetString(ms.ToArray()); } catch { throw; } return(inputString); #endregion }
/// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public string DecryptDes(string decryptString, string decryptKey) { try { byte[] rgbKey = System.Text.Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; byte[] inputByteArray = Convert.FromBase64String(decryptString); System.Security.Cryptography.DESCryptoServiceProvider DCSP = new System.Security.Cryptography.DESCryptoServiceProvider(); System.IO.MemoryStream mStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(System.Text.Encoding.UTF8.GetString(mStream.ToArray())); } catch { return(decryptString); } }
public static string Decrypt(string data, string seckey) { byte[] byKey = System.Text.Encoding.ASCII.GetBytes(seckey); byte[] by_IV = System.Text.Encoding.ASCII.GetBytes(seckey); byte[] byEnc; string result; try { byEnc = System.Convert.FromBase64String(data); } catch { result = null; return(result); } System.Security.Cryptography.DESCryptoServiceProvider cryptoProvider = new System.Security.Cryptography.DESCryptoServiceProvider(); System.IO.MemoryStream ms = new System.IO.MemoryStream(byEnc); System.Security.Cryptography.CryptoStream cst = new System.Security.Cryptography.CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, by_IV), System.Security.Cryptography.CryptoStreamMode.Read); System.IO.StreamReader sr = new System.IO.StreamReader(cst); result = sr.ReadToEnd(); return(result); }
public string Descifrar(string Cadena) { try { if (string.IsNullOrEmpty(Cadena)) { return(Cadena); } byte[] PlainText; PlainText = Convert.FromBase64String(Cadena); MemoryStream memdata = new MemoryStream(); System.Security.Cryptography.DESCryptoServiceProvider DES = new System.Security.Cryptography.DESCryptoServiceProvider(); System.Security.Cryptography.CryptoStream cryptostream = new System.Security.Cryptography.CryptoStream(memdata, DES.CreateDecryptor(Encoding.ASCII.GetBytes(Default8Key), Encoding.ASCII.GetBytes(Default8VI)), System.Security.Cryptography.CryptoStreamMode.Write); cryptostream.Write(PlainText, 0, PlainText.Length); cryptostream.FlushFinalBlock(); cryptostream.Close(); return(Encoding.ASCII.GetString(memdata.ToArray())); } catch (Exception ex) { return(null); } }
private string Decrypt(string str) { str = str.Replace(" ", "+"); string DecryptKey = "0806;[pnuLIT)lOFRUtEChnologieS"; byte[] byKey = { }; byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 }; byte[] inputByteArray = new byte[str.Length]; byKey = System.Text.Encoding.UTF8.GetBytes(DecryptKey.Substring(0, 8)); System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); inputByteArray = System.Convert.FromBase64String(str.Replace(" ", "+")); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(byKey, IV), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return(encoding.GetString(ms.ToArray())); }
/// <summary> /// 字符串解密 /// </summary> /// <param name="strText">字符串</param> /// <param name="sDecrKey">密钥8位数字或字母</param> /// <returns></returns> public static string DesDecryptString(string strText, string sDecrKey) { byte[] rgbKey = null; string SIv = "inputvec"; byte[] buffer = new byte[strText.Length]; try { rgbKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8)); System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider(); provider.Key = rgbKey; provider.IV = System.Text.Encoding.UTF8.GetBytes(SIv); provider.Mode = System.Security.Cryptography.CipherMode.ECB; buffer = Convert.FromBase64String(strText); System.IO.MemoryStream stream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream stream2 = new System.Security.Cryptography.CryptoStream(stream, provider.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); System.Text.Encoding encoding = new System.Text.UTF8Encoding(); return(encoding.GetString(stream.ToArray())); } catch (Exception exception) { //return ("error:" + exception.Message + "\r"); throw exception; } }
public string Decrypt(string stringToDecrypt, string encryptionKey) { try { _key = System.Text.Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8)); using (System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider()) { byte[] inputByteArray = Convert.FromBase64String(stringToDecrypt); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(_key, _iv), System.Security.Cryptography.CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return(encoding.GetString(ms.ToArray())); } } } } catch { return(""); } }
/// <summary> /// DES解密算法 /// </summary> /// <param name="Source">要解密的字符串</param> /// <param name="SecretKey">解密密钥(8的整数倍字节数的字符串)</param> /// <returns>解密后的结果字符串</returns> public static string DES_Decode(byte[] Source, string SecretKey) { System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider(); provider.Key = System.Text.Encoding.UTF8.GetBytes(SecretKey); provider.IV = System.Text.Encoding.UTF8.GetBytes(SecretKey); System.IO.MemoryStream stream2 = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream stream = new System.Security.Cryptography.CryptoStream(stream2, provider.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); stream.Write(Source, 0, Source.Length); try { stream.FlushFinalBlock(); } catch (System.Security.Cryptography.CryptographicException) { return ""; } return System.Text.Encoding.UTF8.GetString(stream2.ToArray()); }
public static string DecryptStatic(string decryptString, string decryptKey) { System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); string result; try { System.Text.Encoding uTF = System.Text.Encoding.UTF8; decryptString = uTF.GetString(System.Convert.FromBase64String(decryptString)); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(decryptKey); byte[] keys = SimpleSecurityHelper.Keys; byte[] array = System.Convert.FromBase64String(decryptString); System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider(); System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(bytes, keys), System.Security.Cryptography.CryptoStreamMode.Write); cryptoStream.Write(array, 0, array.Length); cryptoStream.FlushFinalBlock(); result = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()); } catch { result = "Decrypt Failed!"; } finally { memoryStream.Close(); } return(result); }
public WsgFileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) : base(path, mode, access, share) { try { byte[] key = { 77, 7, 77, 7, 77, 7, 77, 7 }; byte[] iv = { 7, 77, 7, 77, 7, 77, 7, 77 }; DES = new System.Security.Cryptography.DESCryptoServiceProvider(); if (this.CanRead) { CryptoReadStream = new System.Security.Cryptography.CryptoStream(this, DES.CreateDecryptor(key, iv), System.Security.Cryptography.CryptoStreamMode.Read); } if (this.CanWrite) { CryptoWriteStream = new System.Security.Cryptography.CryptoStream(this, DES.CreateEncryptor(key, iv), System.Security.Cryptography.CryptoStreamMode.Write); } } catch (System.Exception ex) { throw ex; } }
/// /// DES解密 /// /// 要解密字符串 /// 返回解密后字符串 public static String Decrypt_DES(String str) { if (str.Trim() == "") { return(""); } try { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); Int32 x; Byte[] inputByteArray = new Byte[str.Length / 2]; for (x = 0; x < str.Length / 2; x++) { inputByteArray[x] = (Byte)(Convert.ToInt32(str.Substring(x * 2, 2), 16)); } des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey); des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.StringBuilder ret = new System.Text.StringBuilder(); return(System.Text.Encoding.Default.GetString(ms.ToArray())); } catch (System.Exception ex) { return(""); } }
/// <summary> /// 字符串解密 /// </summary> /// <param name="encryptPassword">加密过的字符串</param> /// <returns>解密后的字符串</returns> public static string PasswordDecrypt(string encryptPassword) { try { string strPasswordKey = encryptPassword.Substring(encryptPassword.Length - 8, 8); string strEncryptPassword = encryptPassword.Substring(0, encryptPassword.Length - 8); System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); int intEncryptPasswordLength = strEncryptPassword.Length / 2; byte[] bytInput = new byte[intEncryptPasswordLength]; for (int intIndex = 0; intIndex < intEncryptPasswordLength; intIndex++) { bytInput[intIndex] = Convert.ToByte(strEncryptPassword.Substring(intIndex * 2, 2), 16); } des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strPasswordKey); des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(strPasswordKey); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream((System.IO.Stream)ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(bytInput, 0, bytInput.Length); cs.FlushFinalBlock(); return(System.Text.Encoding.Default.GetString(ms.ToArray())); } catch { return(""); } }
/// <summary> /// 取得解密 /// </summary> /// <param name="decryptString"></param> /// <returns></returns> public static string Decrypt(string decryptString) { try { Des des = new Des(); byte[] rgbKey = Encoding.UTF8.GetBytes(des.encryptKey.Substring(0, 8)); byte[] rgbIV = Encoding.UTF8.GetBytes(des.encryptIV.Substring(0, 8)); byte[] inputByteArray = Convert.FromBase64String(decryptString); System.Security.Cryptography.DESCryptoServiceProvider DCSP = new System.Security.Cryptography.DESCryptoServiceProvider(); System.IO.MemoryStream mStream = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(mStream.ToArray())); } catch (Exception) { return(decryptString); } }
public static string Decrypt(string toDecrypt, string key) { var des = new System.Security.Cryptography.DESCryptoServiceProvider(); var ms = new System.IO.MemoryStream(); VerifyKey(ref key); des.Key = HashKey(key, des.KeySize / 8); des.IV = HashKey(key, des.KeySize / 8); byte[] inputBytes = HttpServerUtility.UrlTokenDecode(toDecrypt); var cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputBytes, 0, inputBytes.Length); cs.FlushFinalBlock(); var encoding = Encoding.UTF8; return encoding.GetString(ms.ToArray()); }
/// <summary> /// �Í������ꂽ������������� /// </summary> /// <param name="str">�Í������ꂽ������</param> /// <param name="key">�p�X���[�h</param> /// <returns>���������ꂽ������</returns> public static string DecryptString(string str, string key) { if (str == null) { return ""; } if (str.Length % 4 > 0) { while (str.Length % 4 != 0) { str += "="; } } //DESCryptoServiceProvider�I�u�W�F�N�g�̍쐬 System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //���L�L�[�Ə������x�N�^����� //�p�X���[�h��o�C�g�z��ɂ��� byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key); //���L�L�[�Ə������x�N�^��ݒ� des.Key = ResizeBytesArray(bytesKey, des.Key.Length); des.IV = ResizeBytesArray(bytesKey, des.IV.Length); //Base64�ŕ������o�C�g�z��ɖ߂� byte[] bytesIn = System.Convert.FromBase64String(str); //�Í������ꂽ�f�[�^��ǂݍ��ނ��߂�MemoryStream MemoryStream msIn = new System.IO.MemoryStream(bytesIn); //DES�������I�u�W�F�N�g�̍쐬 System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateDecryptor(); //�ǂݍ��ނ��߂�CryptoStream�̍쐬 System.Security.Cryptography.CryptoStream cryptStreem = new System.Security.Cryptography.CryptoStream(msIn, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Read); //���������ꂽ�f�[�^��擾���邽�߂�StreamReader StreamReader srOut = new StreamReader(cryptStreem, System.Text.Encoding.UTF8); //���������ꂽ�f�[�^��擾���� string result = srOut.ReadToEnd(); //���� srOut.Close(); cryptStreem.Close(); msIn.Close(); return result; }