public bool TryDecrypt(string encryptedItem, string password, out string decryptedItem) { if (string.IsNullOrEmpty (encryptedItem) || string.IsNullOrEmpty (password)) { decryptedItem = ""; return false; } try { byte[] cipherBytes = Convert.FromBase64String (encryptedItem); using (var memoryStream = new MemoryStream(cipherBytes)) { var des = new DESCryptoServiceProvider (); byte[] iv = new byte[8]; memoryStream.Read (iv, 0, iv.Length); var rfc2898DeriveBytes = new Rfc2898DeriveBytes (password, iv, ITERATIONS); byte[] key = rfc2898DeriveBytes.GetBytes (8); using (var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(key, iv), CryptoStreamMode.Read)) using (var streamReader = new StreamReader(cryptoStream)) { decryptedItem = streamReader.ReadToEnd (); return true; } } } catch (Exception ex) { Logger.instance.Log (new Logger.Message (this, ex)); decryptedItem = ""; return false; } }
static int Main () { string filename = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "encrypt.tmp"); string data = "this is sensitive data"; DESCryptoServiceProvider des = new DESCryptoServiceProvider (); des.GenerateIV (); des.GenerateKey (); // ----------- WRITING ENCRYPTED SERIALIZED DATA ------------------ Stream stream = new FileStream (filename, FileMode.Create, FileAccess.Write); stream = new CryptoStream (stream, des.CreateEncryptor (), CryptoStreamMode.Write); BinaryFormatter bformatter = new BinaryFormatter (); bformatter.Serialize (stream, data); stream.Close (); stream = null; bformatter = null; data = string.Empty; // ----------- READING ENCRYPTED SERIALIZED DATA ------------------ stream = new FileStream (filename, FileMode.Open, FileAccess.Read); stream = new CryptoStream (stream, des.CreateDecryptor (), CryptoStreamMode.Read); bformatter = new BinaryFormatter (); data = (string) bformatter.Deserialize (stream); stream.Close (); //----------- CHECK RESULTS ---------------- if (data != "this is sensitive data") return 1; return 0; }
public static string Decrypt(string originalString, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[originalString.Length / 2]; for (int x = 0; x < originalString.Length / 2; x++) { int i = (Convert.ToInt32(originalString.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(); //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象 StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); }
private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV) { //Create the file streams to handle the input and output files. FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0); //Create variables to help with read and write. byte[] bin = new byte[100]; //This is intermediate storage for the encryption. long rdlen = 0; //This is the total number of bytes written. long totlen = fin.Length; //This is the total length of the input file. int len; //This is the number of bytes to be written at a time. SymmetricAlgorithm des = new DESCryptoServiceProvider(); des.Padding = PaddingMode.PKCS7; CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write); Console.WriteLine("Decrypting..."); //Read from the input file, then encrypt and write to the output file. while(rdlen < totlen) { len = fin.Read(bin, 0, 100); encStream.Write(bin, 0, len); rdlen = rdlen + len; Console.WriteLine("{0} bytes processed", rdlen); } encStream.Close(); }
/// <summary> /// 对文件内容进行DES解密 /// </summary> /// <param name="sourceFile">待解密的文件绝对路径</param> /// <param name="destFile">解密后的文件保存的绝对路径</param> public static void DecryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(key); byte[] btIV = Encoding.Default.GetBytes(iv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } }
//cmThe function used to decrypt the text private static string Decrypt(string strText, string SaltKey) { byte[] byKey = { }; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef }; byte[] inputByteArray = new byte[strText.Length + 1]; try { byKey = System.Text.Encoding.UTF8.GetBytes(SaltKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return encoding.GetString(ms.ToArray()); } catch (Exception ex) { return ex.Message; } }
private string DeCryption(string content, string sKey, string sIV) { try { byte[] inputByteArray = Convert.FromBase64String(content); using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider()) { //byte[] inputByteArray = Convert.FromBase64String(content); desProvider.Key = System.Text.Encoding.ASCII.GetBytes(sKey); desProvider.IV = System.Text.Encoding.ASCII.GetBytes(sIV); System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (CryptoStream cs = new CryptoStream(ms,desProvider.CreateDecryptor(), CryptoStreamMode.Write)) { //cs.Clear(); cs.Write(inputByteArray,0,inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); } string str = System.Text.Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return str; } } catch { return "String Not Base64 Encode!!!"; } }
/// <summary> /// 对数据进行解密 /// </summary> /// <param name="decryptstring">需要解密的数据</param> /// <returns></returns> public static string Decode(string data) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); byte[] byEnc; try { byEnc = Convert.FromBase64String(data); } catch { return null; } try { DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(byEnc); CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read); StreamReader sr = new StreamReader(cst); return sr.ReadToEnd(); } catch { return null; } }
public static string Decrypt(string stringToDecrypt) { string sEncryptionKey = "thuynnxkey"; byte[] key = { }; byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 }; byte[] inputByteArray = new byte[stringToDecrypt.Length]; try { key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(stringToDecrypt); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); Encoding encoding = Encoding.UTF8; return encoding.GetString(ms.ToArray()); } catch (System.Exception ex) { SiAuto.Main.LogString("Lỗi :", ex.ToString()); return (string.Empty); } }
public static SaveData Load() { if (!File.Exists("save01.sav")) return null; SaveData data = null; using(Stream stream = File.Open("save01.sav", FileMode.Open)) { BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Binder = new VersionDeserializationBinder(); byte[] byKey = Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8)); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { using (Stream cryptoStream = new CryptoStream(stream, des.CreateDecryptor(byKey, dv), CryptoStreamMode.Read)) { Log.debug("Save", "Loading..."); try { data = (SaveData) binaryFormatter.Deserialize(cryptoStream); } catch (Exception e) { Log.debug("Save", ""+e); return new SaveData(); } Log.debug("Save", "Loaded (level={0})", data.level); cryptoStream.Close(); } } stream.Close(); } return data; }
public string Decrypt(string SourceData) { byte[] EncryptedDataBytes = Convert.FromBase64String(SourceData); MemoryStream TempStream = new MemoryStream(EncryptedDataBytes, 0, EncryptedDataBytes.Length); DESCryptoServiceProvider Decryptor = new DESCryptoServiceProvider(); CryptoStream DecryptionStream = new CryptoStream(TempStream, Decryptor.CreateDecryptor(Key, Iv), CryptoStreamMode.Read); StreamReader AllDataReader = new StreamReader(DecryptionStream); return AllDataReader.ReadToEnd(); }
public static string DESDecrypt(string input) { byte[] bytes = Encoding.ASCII.GetBytes((string)ConfigurationSettings.AppSettings["CipherKeyProd"]); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream (Convert.FromBase64String(input)); CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read); StreamReader reader = new StreamReader(cryptoStream); return reader.ReadToEnd(); }
/**/ /// <summary> /// 对称加密解密的密钥 /// </summary> public static string Key { get { return key; } set { key = value; } } #endregion Properties #region Methods /**/ /// <summary> /// DES解密 /// </summary> /// <param name="decryptString"></param> /// <returns></returns> public static string DesDecrypt(string decryptString) { byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8)); byte[] keyIV = keyBytes; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); }
public string Decrypt(string input) { string output = ""; DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(input)); CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(cryptBytes, cryptBytes), CryptoStreamMode.Read); StreamReader reader = new StreamReader(cryptoStream); output = reader.ReadToEnd(); return output; }
public static string DecryptDES(string text, byte[] key) { if(text != null && text != "") { DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(text)); CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(key, key), CryptoStreamMode.Read); StreamReader reader = new StreamReader(cryptoStream); text = reader.ReadToEnd(); } return text; }
//by me public string Decrypt(string oldword) { if (String.IsNullOrEmpty(oldword)) { throw new ArgumentNullException("The string which needs to be decrypted can not be null."); } DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(oldword)); CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read); StreamReader reader = new StreamReader(cryptoStream); return reader.ReadToEnd(); }
static Boolean Test() { Byte[] PlainText = {0, 1, 2, 3, 4, 5, 6, 7}; //, 8, 9, 10, 11, 12, 13, 14, 15}; Byte[] Key = {1, 1, 1, 1, 1, 1, 1, 2}; Byte[] IV = {1, 1, 1, 1, 1, 1, 1, 1}; Console.WriteLine("Encrypting the following bytes:"); PrintByteArray(PlainText); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Mode = CipherMode.ECB; // des.FeedbackSize = 0; des.Padding = PaddingMode.None; Console.WriteLine("DES default key size = " + des.KeySize); ICryptoTransform sse = des.CreateEncryptor(Key, IV); Console.WriteLine("SSE mode = " + des.Mode); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write); cs.Write(PlainText,0,PlainText.Length); cs.FlushFinalBlock(); byte[] CipherText = ms.ToArray(); cs.Close(); Console.WriteLine("Cyphertext:"); PrintByteArray(CipherText); Console.WriteLine("Decrypting..."); // DESCryptoServiceProvider des = new DESCryptoServiceProvider(); // des.Mode = CipherMode.ECB; // des.FeedbackSize = 0; ICryptoTransform ssd = des.CreateDecryptor(Key, IV); Console.WriteLine("SSD mode = " + des.Mode); cs = new CryptoStream(new MemoryStream(CipherText), ssd, CryptoStreamMode.Read); byte[] NewPlainText = new byte[8]; cs.Read(NewPlainText,0,8); PrintByteArray(NewPlainText); if (!Compare(PlainText, NewPlainText)) { Console.WriteLine("ERROR: roundtrip failed"); return false; } return true; }
/// <summary> /// Standard DES decryption /// </summary> /// <param name="val">Value of decrypted</param> /// <returns>Returns decrypted value as string</returns> public static string Decrypt(string val) { string decrpted = ""; if (val != "") { DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); //convert from string to byte byte[] buffer = Convert.FromBase64String(val); MemoryStream ms = new MemoryStream(buffer); CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); decrpted = sr.ReadToEnd(); } return decrpted; }
// 解密字符串 public string DecryptString(string sInputString, string sKey) { string[] sInput = sInputString.Split("-".ToCharArray()); var data = new byte[sInput.Length]; for (int i = 0; i < sInput.Length; i++) { data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber); } var DES = new DESCryptoServiceProvider(); DES.Key = Encoding.ASCII.GetBytes(sKey); DES.IV = Encoding.ASCII.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateDecryptor(); byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length); return Encoding.UTF8.GetString(result); }
/// <summary> /// MD5解密 /// </summary> /// <param name="Source">需要解密的字符串</param> /// <returns>MD5解密后的字符串</returns> public string Md5Decrypt(string Source) { //将解密字符串转换成字节数组 byte[] bytIn = System.Convert.FromBase64String(Source); //给出解密的密钥和偏移量,密钥和偏移量必须与加密时的密钥和偏移量相同 byte[] iv = { 103, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量 byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥 DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider(); mobjCryptoService.Key = iv; mobjCryptoService.IV = key; //实例流进行解密 System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length); ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader strd = new StreamReader(cs, Encoding.Default); return strd.ReadToEnd(); }
private string Decrypt(string pToDecrypt, string sKey) { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); byte[] buffer = new byte[pToDecrypt.Length / 2]; for (int i = 0; i < (pToDecrypt.Length / 2); i++) { int num2 = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 0x10); buffer[i] = (byte)num2; } provider.Key = Encoding.ASCII.GetBytes(sKey); provider.IV = Encoding.ASCII.GetBytes(sKey); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); new StringBuilder(); return Encoding.Default.GetString(stream.ToArray()); }
public static string Decrypt(string pstrText) { pstrText = pstrText.Replace(" ", "+"); string pstrDecrKey = "1239;[pewGKG)NisarFidesTech"; byte[] byKey = { }; byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 }; byte[] inputByteArray = new byte[pstrText.Length]; byKey = System.Text.Encoding.UTF8.GetBytes(pstrDecrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(pstrText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), 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="pToDecrypt"></param> /// <param name="sKey"></param> /// <returns></returns> static public string DesDecrypt(string pToDecrypt, string 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.UTF8.GetString(ms.ToArray()); }
public static string DesDecrypt(string toDecryptString,string keyStr) { byte[] toDecryptBytes = Convert.FromBase64String(toDecryptString); using (var des = new DESCryptoServiceProvider()) { des.Key = Encoding.GetBytes(keyStr); des.IV = Encoding.GetBytes(keyStr); var ms = new MemoryStream(); using (var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(toDecryptBytes, 0, toDecryptBytes.Length); cs.FlushFinalBlock(); cs.Close(); } string decryptedString = Encoding.GetString(ms.ToArray()); ms.Close(); return decryptedString; } }
/// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string DecryptDES(string decryptString, string decryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } }
/// <summary> /// MD5���� /// </summary> public static string MD5Decrypt(string Text, string sKey) { DESCryptoServiceProvider des = new 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(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); }
public static string DESDecrypt(string str, string key) { try { str = str.Replace('*', '+'); byte[] rgbKey = System.Text.Encoding.UTF8.GetBytes(key); byte[] rgbIV = rgbKey; byte[] inputByteArray = Convert.FromBase64String(str); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); System.IO.MemoryStream mStream = new System.IO.MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return System.Text.Encoding.UTF8.GetString(mStream.ToArray()); } catch { return ""; } }
public string Decrypt(string stringToDecrypt, string sEncryptionKey) { byte[] inputByteArray = new byte[stringToDecrypt.Length + 1]; try { key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(stringToDecrypt); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return encoding.GetString(ms.ToArray()); } catch (Exception e) { return e.Message; } }
/// DES解密 public string MD5Decrypt(string pToDecrypt, string sKey) { var des = new DESCryptoServiceProvider(); var 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 = Encoding.ASCII.GetBytes(sKey); des.IV = Encoding.ASCII.GetBytes(sKey); var ms = new MemoryStream(); var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); var ret = new StringBuilder(); return Encoding.Default.GetString(ms.ToArray()); }
/// /// dec 解密过程 /// /// /// /// public string decrypt(string ptodecrypt, string 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(); //建立stringbuild对象,createdecrypt使用的是流对象,必须把解密后的文本变成流对象 return(System.Text.Encoding.Default.GetString(ms.ToArray())); }
public static string DecryptData(string STRING) { try { //Have a Key byte[] myKey = Encoding.ASCII.GetBytes("ActisKey"); //Have a vector required to create a Crypto stream byte[] myVector = Encoding.ASCII.GetBytes("ActisKey"); //Create a Service Provider object DESCryptoServiceProvider myCryptoProvider = new DESCryptoServiceProvider(); //Create a memory strem MemoryStream myMemoryStream = new MemoryStream(Convert.FromBase64String(STRING)); //Create a Cryptro memory stream CryptoStream myCryptoStream = new CryptoStream(myMemoryStream, myCryptoProvider.CreateDecryptor(myKey, myVector), CryptoStreamMode.Read); //HAve a reader StreamReader myReader = new StreamReader(myCryptoStream); return(myReader.ReadToEnd()); } catch (Exception ex) { throw ex; } }
//C# DES解密方法 public static string DESDecrypt(string encryptedValue, string key, string iv) { key = Util.MD5Encrypt32(key).Substring(0, 8); using (DESCryptoServiceProvider sa = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(iv) }) { using (ICryptoTransform ct = sa.CreateDecryptor()) { byte[] byt = Convert.FromBase64String(encryptedValue); using (var ms = new MemoryStream()) { using (var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write)) { cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); } return(Encoding.UTF8.GetString(ms.ToArray())); } } } }
public void button1_Click(object sender, EventArgs e) { t.Start(); TcpClient p = t.AcceptTcpClient(); ns = p.GetStream(); byte[] buffer = new byte[1024]; new Thread(() => { while (true) { int size = ns.Read(buffer, 0, buffer.Length); MemoryStream tw = new MemoryStream(); CryptoStream we = new CryptoStream(tw, provider.CreateDecryptor(), CryptoStreamMode.Write); we.Write(buffer, 0, size); we.FlushFinalBlock(); Invoke(new Action(() => { listBox1.Items.Add(Encoding.ASCII.GetString(tw.ToArray())); })); } }).Start(); }
/// <summary> /// DES解密 /// </summary> /// <param name="data">解密数据</param> /// <param name="key">8位字符的密钥字符串(需要和加密时相同)</param> /// <param name="iv">8位字符的初始化向量字符串(需要和加密时相同)</param> /// <returns></returns> public static string DesDecrypt(string data, string key, string iv) { var byKey = Encoding.ASCII.GetBytes(key); var byIv = Encoding.ASCII.GetBytes(iv); byte[] byEnc; try { byEnc = Convert.FromBase64String(data); } catch { return(null); } var cryptoProvider = new DESCryptoServiceProvider(); var ms = new MemoryStream(byEnc); var cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIv), CryptoStreamMode.Read); var sr = new StreamReader(cst); return(sr.ReadToEnd()); }
public static string Decrypt(string pToDecrypt) { string s1 = "SeiweLL0"; string s2 = "0SeiweLL"; DESCryptoServiceProvider cryptoServiceProvider = new DESCryptoServiceProvider(); byte[] buffer = new byte[pToDecrypt.Length / 2]; for (int index = 0; index < pToDecrypt.Length / 2; ++index) { int int32 = Convert.ToInt32(pToDecrypt.Substring(index * 2, 2), 16); buffer[index] = (byte)int32; } cryptoServiceProvider.Key = Encoding.ASCII.GetBytes(s1); cryptoServiceProvider.IV = Encoding.ASCII.GetBytes(s2); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, cryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write); cryptoStream.Write(buffer, 0, buffer.Length); cryptoStream.FlushFinalBlock(); StringBuilder stringBuilder = new StringBuilder(); return(Encoding.Default.GetString(memoryStream.ToArray())); }
/// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string DesDecrypt(string decryptString, string decryptKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); int len; len = decryptString.Length / 2; byte[] inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { i = Convert.ToInt32(decryptString.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(GetMD5(decryptKey).Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(GetMD5(decryptKey).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())); }
/// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <returns></returns> public static string DESDecrypt(this string decryptString) { try { var bytes = Convert.FromBase64String(decryptString); using (var des = new DESCryptoServiceProvider()) { using (var stream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(stream, des.CreateDecryptor(RgbKey, RgbIV), CryptoStreamMode.Write)) { cryptoStream.Write(bytes, 0, bytes.Length); cryptoStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(stream.ToArray())); } } } } catch { return(string.Empty); } }
/// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string Decode(string decryptString, string decryptKey = "www.GMS.com") { try { decryptKey = MD5(decryptKey); decryptKey = decryptKey.Substring(0, 8); decryptKey = decryptKey.PadRight(8, ' '); byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(mStream.ToArray())); } catch { return(""); } }
public string Decrypt(string pText, string pDecrKey) { string str = ""; byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef }; byte[] buffer = new byte[pText.Length + 1]; try { byte[] bytes = Encoding.UTF8.GetBytes(Strings.Left(pDecrKey, 8)); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); buffer = Convert.FromBase64String(pText); MemoryStream stream2 = new MemoryStream(); CryptoStream stream = new CryptoStream(stream2, provider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Write); stream.Write(buffer, 0, buffer.Length); stream.FlushFinalBlock(); str = Encoding.UTF8.GetString(stream2.ToArray()); } catch (Exception exception1) { str = "Decrypt error" + exception1.StackTrace; } return(str); }
/// <summary> /// DES对称解密,密钥必须为8位 /// </summary> /// <param name="textToDecrypt"></param> /// <param name="key"></param> /// <returns></returns> public static string DESDecrypt(string textToDecrypt, string key) { if (string.IsNullOrEmpty(key) || key.Length != 8) { throw new Error("请提供有效的8位密钥"); } byte[] inputByteArray = Convert.FromBase64String(textToDecrypt); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = ASCIIEncoding.ASCII.GetBytes(key); des.IV = ASCIIEncoding.ASCII.GetBytes(key); System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); } string str = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return(str); } }
//DES解密 private static string DESDecode(string decodeStr) { string key = "nimeiaaa"; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[decodeStr.Length / 2]; for (int x = 0; x < decodeStr.Length / 2; x++) { int i = (Convert.ToInt32(decodeStr.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(key); des.IV = ASCIIEncoding.ASCII.GetBytes(key); 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())); }
static string DESDecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV) { string plaintext = null; using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = Key; des.IV = new byte[IV.Length]; des.Padding = PaddingMode.PKCS7; des.BlockSize = 64; des.Mode = CipherMode.ECB; ICryptoTransform decryptor = des.CreateDecryptor(des.Key, des.IV); using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { plaintext = srDecrypt.ReadToEnd(); } } } } return(plaintext); }
// Methods public static string Decrypt(this string cryptedString, byte[] key) { if (string.IsNullOrEmpty(cryptedString)) { throw new ArgumentNullException("The string which needs to be decrypted can not be null."); } if (key.Length < 8) { throw new ArgumentException("Password key length should be nore than 8"); } byte[] destinationArray = new byte[8]; Array.Copy(key, destinationArray, 8); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); MemoryStream stream = new MemoryStream(Convert.FromBase64String(cryptedString)); CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(destinationArray, destinationArray), CryptoStreamMode.Read); string str = new StreamReader(stream2).ReadToEnd(); if (string.CompareOrdinal(str, EMPTY) == 0) { return(string.Empty); } return(str); }
public static string Decrypt(string EncryptedText) { byte[] inputByteArray = new byte[EncryptedText.Length + 1]; byte[] rgbIV = { 0x21, 0x43, 0x56, 0x87, 0x10, 0xfd, 0xea, 0x1c }; byte[] key = { }; try { key = System.Text.Encoding.UTF8.GetBytes("A0D1nX0Q"); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(EncryptedText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, rgbIV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return(encoding.GetString(ms.ToArray())); } catch (Exception e) { return(e.Message); } }
public static string DecryptDESECBPKCS7(string source, string key) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] sourceArray = Convert.FromBase64String(source); des.Mode = CipherMode.ECB; des.Padding = PaddingMode.PKCS7; des.Key = Encoding.UTF8.GetBytes(key); des.IV = Encoding.UTF8.GetBytes(key); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(sourceArray, 0, sourceArray.Length); cs.FlushFinalBlock(); cs.Close(); ms.Close(); return(Encoding.UTF8.GetString(ms.ToArray())); } catch (Exception) { return(""); } }
/// <summary> /// 解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string DecryptDES(string decryptString, string decryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write)) { cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(mStream.ToArray())); } } } catch { return(decryptString); } }
/// <summary> /// 对DES加密后的字符串进行解密 /// </summary> /// <param name="encryptedString">待解密的字符串</param> /// <returns>解密后的字符串</returns> public static string Decrypt(string encryptedString) { byte[] btKey = Encoding.Default.GetBytes(key); byte[] btIV = Encoding.Default.GetBytes(iv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Convert.FromBase64String(encryptedString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Encoding.Default.GetString(ms.ToArray()); } catch { throw; } } }
/// <summary> /// Metoda koja dekriptira sadržaj na temelju ključa i inicijalizacijskoga vektora /// </summary> /// <param name="dataToDecrypt"></param> /// <returns></returns> public override string DecryptData(string dataToDecrypt) { string result; var data = Convert.FromBase64String(dataToDecrypt); using (var des = new DESCryptoServiceProvider()) { des.Mode = CipherMode.CBC; des.Padding = PaddingMode.PKCS7; des.Key = key; des.IV = IV; using (var memoryStream = new MemoryStream()) { var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(), CryptoStreamMode.Write); cryptoStream.Write(data, 0, data.Length); cryptoStream.FlushFinalBlock(); var decryptBytes = memoryStream.ToArray(); result = Encoding.UTF8.GetString(decryptBytes); return(result); } } }
public static string Decrypt(string str, string shortKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Mode = CipherMode.ECB;//java默认是ECB des.Padding = PaddingMode.PKCS7; byte[] inputByteArray = new byte[str.Length / 2]; for (int x = 0; x < str.Length / 2; x++) { int i = (Convert.ToInt32(str.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = Encoding.UTF8.GetBytes(shortKey); des.IV = Encoding.UTF8.GetBytes(shortKey); 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())); }
/// <summary> /// 解密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Decrypt(string Text, string sKey) { DESCryptoServiceProvider des = new 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(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return(Encoding.Default.GetString(ms.ToArray())); }
/// <summary> /// Decrypts the specified string to decrypt. /// </summary> /// <param name="stringToDecrypt">The string to decrypt.</param> /// <param name="decryptionKey">The decryption key.</param> /// <returns>Return string</returns> public string Decrypt(string stringToDecrypt, string decryptionKey) { ///// byte[] inputByteArray = new byte[stringToDecrypt.Length + 1]; byte[] inputByteArray; try { stringToDecrypt = stringToDecrypt.Replace(" ", "+"); stringToDecrypt = stringToDecrypt.Replace("-", "/"); this.key = System.Text.Encoding.UTF8.GetBytes(this.Left(decryptionKey, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(stringToDecrypt); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(this.key, this.iV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return(encoding.GetString(ms.ToArray())); } catch (Exception e) { return(e.Message); } }
/// <summary> /// 解密 /// </summary> /// <param name="key"></param> /// <param name="data"></param> /// <returns></returns> public static string Decrypt(string key, string data) { if (string.IsNullOrEmpty(data)) { return(string.Empty); } byte[] rgbKey = Encoding.UTF8.GetBytes(key); byte[] rgbIV = Encoding.UTF8.GetBytes(key); byte[] inputByteArray = HexStringToByteArray(data); DESCryptoServiceProvider desc = new DESCryptoServiceProvider(); desc.Mode = CipherMode.CBC; desc.Padding = PaddingMode.PKCS7; MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, desc.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(mStream.ToArray())); }
/// DES解密 /// <param >待解密的字符串</param> /// <param >解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string Decrypt(string decryptString) { try { Encoding ed = Encoding.UTF8; decryptString = ed.GetString(Convert.FromBase64String(decryptString)); string decryptKey = KeyValue; byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(mStream.ToArray())); } catch { //return decryptString; return("Decrypt Failed!"); } }
public void Decrypt(string inputFilename, string outputFilename, string sKey) { sKey = Base64Encode(sKey); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); FileStream fsread = new FileStream(inputFilename, FileMode.Open, FileAccess.Read); ICryptoTransform desdecrypt = DES.CreateDecryptor(); CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read); StreamWriter fsDecrypted = new StreamWriter(outputFilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); }
/// <summary> /// DES解密字符串 /// </summary> /// <param name="toDecryptString">待解密的字符串</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string Decrypt(string toDecryptString) { try { if (toDecryptString == null || toDecryptString == "" || toDecryptString == string.Empty) { return(""); } byte[] rgbKey = Encoding.UTF8.GetBytes(key); byte[] rgbIV = IV; byte[] inputByteArray = Convert.FromBase64String(toDecryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(mStream.ToArray())); } catch { return(toDecryptString); } }
///MD5解密 public static string MD5Decrypt(string pToDecrypt, string 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())); }
public static string Decrypt(string text) { try { key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Byte[] byteArray = Convert.FromBase64String(text); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(key, IV), CryptoStreamMode.Write); cryptoStream.Write(byteArray, 0, byteArray.Length); cryptoStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(memoryStream.ToArray())); } catch (Exception ex) { throw ex; } }
/// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string DecryptDES(string decryptString) { if (decryptString == "") { return(""); } try { byte[] rgbKey = Encoding.UTF8.GetBytes("openblog"); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return(Encoding.UTF8.GetString(mStream.ToArray())); } catch (Exception ex) { return(ex.Message); } }
/// <summa /// 解密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Decrypt(string Text, string sKey) { DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider(); int num = Text.Length / 2; byte[] array = new byte[num]; for (int i = 0; i < num; i++) { int num2 = Convert.ToInt32(Text.Substring(i * 2, 2), 16); array[i] = (byte)num2; } dESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(MD5Lower(sKey).Substring(0, 8)); dESCryptoServiceProvider.IV = Encoding.ASCII.GetBytes(MD5Lower(sKey).Substring(0, 8)); using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write)) { cryptoStream.Write(array, 0, array.Length); cryptoStream.FlushFinalBlock(); return(Encoding.Default.GetString(memoryStream.ToArray())); } } }
/// <summary> /// DES解密 /// </summary> /// <param name="pwd">密文</param> /// <param name="key">密钥</param> /// <returns>明文</returns> public static string DESDecrypt(string decryptString, string key) { //默认向量 byte[] ivs = { 0x13, 0x24, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; using (DESCryptoServiceProvider dES = new DESCryptoServiceProvider()) { byte[] inputByteArray = Convert.FromBase64String(decryptString); dES.Key = ASCIIEncoding.ASCII.GetBytes(key); dES.IV = ivs; MemoryStream memory = new MemoryStream(); using (CryptoStream csStream = new CryptoStream(memory, dES.CreateDecryptor(), CryptoStreamMode.Write)) { csStream.Write(inputByteArray, 0, inputByteArray.Length); csStream.FlushFinalBlock(); } string result = Encoding.UTF8.GetString(memory.ToArray()); memory.Close(); return(result); } }