/// <summary> /// AES加密 /// </summary> /// <param name="text">加密字符</param> /// <param name="password">加密的密码</param> /// <param name="iv">密钥</param> /// <returns></returns> // 加密 /// <summary> /// AES 加密 /// </summary> /// <param name="str"></param> /// <param name="key"></param> /// <returns></returns> public static string AesEncrypt(string str, string key) { if (key == null) { return("Key為空null"); } // 判断Key是否为16位 if (key.Length != 16) { return("Key長度不是16位元"); } if (string.IsNullOrEmpty(str)) { return(null); } Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), Mode = System.Security.Cryptography.CipherMode.ECB, Padding = System.Security.Cryptography.PaddingMode.PKCS7 }; System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(Convert.ToBase64String(resultArray, 0, resultArray.Length)); }
//Decryption Method public string DecryptTripleDES(string base64Text) { try { string Key = Convert.ToString(configuration.GetSection("appSettings").GetSection("EncryptKey").Value); byte[] Buffer = new byte[0]; System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider(); System.Security.Cryptography.MD5CryptoServiceProvider hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(Key)); DES.Mode = System.Security.Cryptography.CipherMode.ECB; System.Security.Cryptography.ICryptoTransform DESDecrypt = DES.CreateDecryptor(); Buffer = Convert.FromBase64String(base64Text); string DecTripleDES = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); return(DecTripleDES); } catch (Exception) { return(string.Empty); } }
/// <summary> /// AES解密/ECB/NoPadding /// </summary> /// <param name="str"></param> /// <param name="key"></param> /// <returns></returns> public static string AesDecrypt(string str, string key) { if (string.IsNullOrEmpty(str)) { return(null); } Byte[] toEncryptArray = Convert.FromBase64String(str); //key不足16位时用1补齐 if (key.Length % 16 != 0) { int keyLen = 16 - (key.Length % 16); for (int i = 0; i < keyLen; i++) { key += "1"; } } System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), Mode = System.Security.Cryptography.CipherMode.ECB, Padding = System.Security.Cryptography.PaddingMode.None }; System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(Encoding.UTF8.GetString(resultArray).Replace("\0", "")); }
public byte[] Decrypte(byte[] inData) { if (!isCrypte) { //if (true) return(inData); } UInt32 len = 0; switch (pkgLen) { case 2: if (inData.Length < 2) { UnityEngine.Debug.LogError("indata len less then 2." + inData.Length); } len = (UInt32)((UInt16)System.Net.IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(inData, 0))); break; case 4: len = (UInt32)System.Net.IPAddress.NetworkToHostOrder((int)BitConverter.ToUInt32(inData, 0)); break; } System.Security.Cryptography.ICryptoTransform trs = cryto.CreateDecryptor(key, iv); byte[] outData = new byte[len]; byte[] ddate = trs.TransformFinalBlock(inData, pkgLen, inData.Length - pkgLen); Array.Copy(ddate, outData, len); //UnityEngine.Debug.Log("decrypte inData:" + BitConverter.ToString(inData)); //UnityEngine.Debug.Log("decrypte outData:" + BitConverter.ToString(outData)); return(outData); }
/// <summary> /// AES 加密 /// </summary> /// <param name="str">要加密的内容</param> /// <param name="key">两种方式</param> /// <returns></returns> public static string AesEncrypt(string str, string key) { if (string.IsNullOrEmpty(str)) { return(null); } Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str); //方法一:只使用key加密 //System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged //{ // Key = Encoding.UTF8.GetBytes(key), // Mode = System.Security.Cryptography.CipherMode.CBC,//Mode = System.Security.Cryptography.CipherMode.ECB, // Padding = System.Security.Cryptography.PaddingMode.PKCS7 //}; //System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor(); //Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); //return Convert.ToBase64String(resultArray, 0, resultArray.Length); //方法二:使用key和向量IV加密 using (RijndaelManaged rm = new RijndaelManaged()) { System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor(rgbKey, rgbIv); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(Convert.ToBase64String(resultArray, 0, resultArray.Length)); } }
public static string AES_Decrypt(string input_text) { //Please dont change or EDIT ANY PART of THIS CODE // ''' security_Code= [email protected] string securitycode = "[email protected]"; System.Security.Cryptography.RijndaelManaged AES = new System.Security.Cryptography.RijndaelManaged(); System.Security.Cryptography.MD5CryptoServiceProvider Hash_AES = new System.Security.Cryptography.MD5CryptoServiceProvider(); string decrypted = ""; try { byte[] hash = new byte[32]; byte[] temp = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(securitycode)); Array.Copy(temp, 0, hash, 0, 16); Array.Copy(temp, 0, hash, 15, 16); AES.Key = hash; AES.Mode = CipherMode.ECB; // Security.Cryptography.CipherMode.ECB System.Security.Cryptography.ICryptoTransform DESDecrypter = AES.CreateDecryptor(); byte[] Buffer = Convert.FromBase64String(input_text); decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)); } catch (Exception ex) { throw ex; } return(decrypted); }
/// <summary> /// AES解密,与JAVA通用 /// </summary> /// <param name="str"></param> /// <param name="key"></param> /// <returns></returns> public static string AesDecrypt(string str, string key) { if (string.IsNullOrEmpty(str)) { return(""); } try { Byte[] toEncryptArray = Convert.FromBase64String(str); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), Mode = System.Security.Cryptography.CipherMode.ECB, Padding = System.Security.Cryptography.PaddingMode.PKCS7 }; System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(Encoding.UTF8.GetString(resultArray)); } catch (Exception e) { Log.Error(e.Message); return(""); } }
public static string Encrypt(string toEncrypt, string key) { byte[] keyArray; byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); // Get the key from config file System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); hashmd5.Clear(); System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider(); //set the secret key for the tripleDES algorithm tdes.Key = keyArray; //mode of operation. there are other 4 modes. //We choose ECB(Electronic code Book) tdes.Mode = System.Security.Cryptography.CipherMode.ECB; //padding mode(if any extra byte added) tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7; System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateEncryptor(); //transform the specified region of bytes array to resultArray byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); //Release resources held by TripleDes Encryptor tdes.Clear(); //Return the encrypted data into unreadable string format return(Convert.ToBase64String(resultArray, 0, resultArray.Length)); }
/// <summary> /// Transforms an input block. /// </summary> /// <param name="transformField">Either the <see cref="encryptor"/> or <see cref="decryptor"/> field.</param> /// <param name="transformCreator">The function to create a new transformer.</param> /// <param name="data">The input data.</param> /// <param name="iv">The initialization vector.</param> /// <returns>The result of the transform.</returns> private byte[] CipherOperation(ref Platform.ICryptoTransform transformField, Func <SymmetricCryptographicKey, byte[], Platform.ICryptoTransform> transformCreator, byte[] data, byte[] iv) { Requires.NotNull(transformCreator, nameof(transformCreator)); Requires.NotNull(data, nameof(data)); if (this.Padding == SymmetricAlgorithmPadding.None && data.Length == 0) { return(data); } if (iv != null || !this.CanStreamAcrossTopLevelCipherOperations || transformField == null) { transformField?.Dispose(); transformField = transformCreator(this, iv); } if (this.CanStreamAcrossTopLevelCipherOperations) { byte[] outputBlock = new byte[data.Length]; int bytesOutput = transformField.TransformBlock(data, 0, data.Length, outputBlock, 0); Array.Resize(ref outputBlock, bytesOutput); return(outputBlock); } else { return(transformField.TransformFinalBlock(data, 0, data.Length)); } }
/// <summary> /// AES 解密 /// </summary> /// <param name="str">需解密字符串</param> /// <param name="key">密钥</param> /// <param name="isDecodeUrl">是否需要URL解码</param> /// <returns></returns> public static string AesDecrypt(string str, string key, bool isDecodeUrl = true) { if (string.IsNullOrEmpty(str)) { return(null); } if (isDecodeUrl) { str = System.Web.HttpUtility.UrlDecode(str); } Byte[] toEncryptArray = Convert.FromBase64String(str); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), Mode = System.Security.Cryptography.CipherMode.ECB, Padding = System.Security.Cryptography.PaddingMode.PKCS7 }; System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(Encoding.UTF8.GetString(resultArray)); }
/// <summary> /// AES 加密 /// </summary> /// <param name="encryptKey"></param> /// <param name="bizContent"></param> /// <param name="charset"></param> /// <returns></returns> public static string AesEncrypt(string encryptKey, string bizContent, string charset) { Byte[] keyArray = Convert.FromBase64String(encryptKey); Byte[] toEncryptArray = null; if (string.IsNullOrEmpty(charset)) { toEncryptArray = Encoding.UTF8.GetBytes(bizContent); } else { toEncryptArray = Encoding.GetEncoding(charset).GetBytes(bizContent); } System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = System.Security.Cryptography.CipherMode.CBC; rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7; rDel.IV = AES_IV; System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor(rDel.Key, rDel.IV); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(Convert.ToBase64String(resultArray)); }
}//end method /// <summary> /// 3des解密字符6 /// </summary> /// <param name="a_strString">要解密的字符串</param> /// <param name="a_strKey">密钥</param> /// <returns>解密后的字符串</returns> /// <exception cref="">密钥错误</exception> /// <remarks>静态方法,采用默认utf8编码</remarks> public static string Decrypt3DES(this string a_strString, string a_strKey) { System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider(); System.Security.Cryptography.MD5CryptoServiceProvider hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); DES.Key = hashMD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(a_strKey)); DES.Mode = System.Security.Cryptography.CipherMode.ECB; System.Security.Cryptography.ICryptoTransform DESDecrypt = DES.CreateDecryptor(); string result = ""; try { byte[] Buffer = Convert.FromBase64String(a_strString); result = System.Text.Encoding.UTF8.GetString(DESDecrypt.TransformFinalBlock (Buffer, 0, Buffer.Length)); } catch (Exception e) { #if DEBUG Console.WriteLine("错误:{0}", e); #endif//DEBUG throw (new Exception("Invalid Key or input string is not a valid base64 string", e)); } return(result); }//end method
public static string AesEncrypt(string str) { if (string.IsNullOrEmpty(str)) { return(null); } Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str); Byte[] tmpArray = { }; //加密数据不足16位时用0补齐 if (toEncryptArray.Length % 16 != 0) { int len = 16 - (toEncryptArray.Length % 16); tmpArray = Enumerable.Repeat((byte)0, len).ToArray(); } toEncryptArray = toEncryptArray.Concat(tmpArray).ToArray(); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = Encoding.UTF8.GetBytes(Key), Mode = System.Security.Cryptography.CipherMode.ECB, Padding = System.Security.Cryptography.PaddingMode.None }; System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(Convert.ToBase64String(resultArray, 0, resultArray.Length)); }
public static string EncryptText(string _In) { try { string sKey = ""; if (_In == "") { return(_In); } sKey = CreateKey("PASSWORD"); System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); System.Security.Cryptography.MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider(); DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(sKey)); DES.Mode = System.Security.Cryptography.CipherMode.ECB; System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor(); byte[] Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(_In); return(Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))); } catch (Exception ex) { clsMessages.ShowErrorException(ex); return(""); } }
private byte[] RijndealDecryption(byte[] data) { byte[] result = null; if (RIJNDEALPRIVATEKEY == null) { throw (new InvalidOperationException("Encryption key has not been defined")); } if (data != null && data.Length > 0) { try { Rijndael obj = System.Security.Cryptography.Rijndael.Create(); try { using (System.Security.Cryptography.ICryptoTransform encrypt = obj.CreateDecryptor(RIJNDEALPRIVATEKEY, RIJNDEALVECTORKEY)) { result = encrypt.TransformFinalBlock(data, 0, data.Length); } } catch { } } catch { } } return(result); }
public static byte[] DecryptAES(byte[] input, string key) { System.Security.Cryptography.RijndaelManaged AES = new System.Security.Cryptography.RijndaelManaged(); System.Security.Cryptography.MD5CryptoServiceProvider Hash_AES = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] decrypted; try { byte[] hash = new byte[32]; byte[] temp = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key)); Array.Copy(temp, 0, hash, 0, 16); Array.Copy(temp, 0, hash, 15, 16); AES.Key = hash; AES.Mode = System.Security.Cryptography.CipherMode.ECB; System.Security.Cryptography.ICryptoTransform DESDecrypter = AES.CreateDecryptor(); //byte[] Buffer = Convert.FromBase64String(input); //byte[] Buffer = Convert.for; //decrypted = System.Text.ASCIIEncoding.UTF8.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)); decrypted = DESDecrypter.TransformFinalBlock(input, 0, input.Length); return(decrypted); } catch (Exception ex) { throw ex; } }
/// <summary> /// AES加密 /// </summary> /// <param name="encryptStr">明文</param> /// <param name="key">密钥</param> /// <returns></returns> public static string Encrypt() { //获取mac地址 string str = GetFirstMacAddress(); //获取下一天日期 string key = DateTime.Now.AddDays(1).ToString("yyyyMMdd"); //补全key为16个字符的字符串 for (int i = key.Length; i < 16; i++) { key += "0"; } if (string.IsNullOrEmpty(str)) { return(null); } Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str); //开始AES加密 System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = Encoding.UTF8.GetBytes(key), //密钥 Mode = System.Security.Cryptography.CipherMode.ECB, //加密模式 Padding = System.Security.Cryptography.PaddingMode.PKCS7 //填白模式,对于AES, C# 框架中的 PKCS #7等同与Java框架中 PKCS #5 }; //字节编码, 将有特等含义的字符串转化为字节流 System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor(); //加密 Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); //将加密后的字节流转化为字符串,以便网络传输与储存。 return(Convert.ToBase64String(resultArray, 0, resultArray.Length)); }
public static string AES_Encrypt(string input_text, string security_Code) { // ''' security_Code= [email protected] System.Security.Cryptography.RijndaelManaged AES = new System.Security.Cryptography.RijndaelManaged(); System.Security.Cryptography.MD5CryptoServiceProvider Hash_AES = new System.Security.Cryptography.MD5CryptoServiceProvider(); string encrypted = ""; try { byte[] hash = new byte[32]; byte[] temp = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(security_Code)); Array.Copy(temp, 0, hash, 0, 16); Array.Copy(temp, 0, hash, 15, 16); AES.Key = hash; AES.Mode = System.Security.Cryptography.CipherMode.ECB; //'Security.Cryptography.CipherMode.ECB System.Security.Cryptography.ICryptoTransform DESEncrypter = AES.CreateEncryptor(); byte[] Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(input_text); encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)); } catch (Exception ex) { } return(encrypted); }
private static string Encrypt(string text, System.Security.Cryptography.TripleDESCryptoServiceProvider Des) { System.Security.Cryptography.ICryptoTransform desdencrypt = Des.CreateEncryptor(); dynamic MyASCIIEncoding = new System.Text.ASCIIEncoding(); byte[] buff = System.Text.ASCIIEncoding.ASCII.GetBytes(text); return(Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length))); }
//加密 private static string DES3Encrypt(string data, string key) { System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider(); DES.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(key); DES.Mode = System.Security.Cryptography.CipherMode.ECB; DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7; System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor(); byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(data); return(Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length))); }
public static byte[] smethod_10(byte[] byte_0, string string_0) { System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new System.Security.Cryptography.TripleDESCryptoServiceProvider(); tripleDESCryptoServiceProvider.Key = System.Text.Encoding.UTF8.GetBytes(string_0); tripleDESCryptoServiceProvider.Mode = System.Security.Cryptography.CipherMode.ECB; tripleDESCryptoServiceProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7; System.Security.Cryptography.ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateEncryptor(); byte[] result = cryptoTransform.TransformFinalBlock(byte_0, 0, byte_0.Length); tripleDESCryptoServiceProvider.Clear(); return(result); }
/// <summary> /// AES加密 - 16进制 /// </summary> public static String EncryptHex(String content, String key) { Byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); Byte[] encryptArray = UTF8Encoding.UTF8.GetBytes(content); System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = System.Security.Cryptography.CipherMode.ECB; rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7; System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(encryptArray, 0, encryptArray.Length); return(ByteUtil.BytesToHex(resultArray)); }
public static byte[] smethod_22(byte[] byte_0, string string_0) { System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider(); dESCryptoServiceProvider.Key = System.Text.Encoding.UTF8.GetBytes(string_0); dESCryptoServiceProvider.Mode = System.Security.Cryptography.CipherMode.ECB; dESCryptoServiceProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7; System.Security.Cryptography.ICryptoTransform cryptoTransform = dESCryptoServiceProvider.CreateDecryptor(); byte[] array = cryptoTransform.TransformFinalBlock(byte_0, 0, byte_0.Length); dESCryptoServiceProvider.Clear(); System.Array.Resize <byte>(ref array, array.Length - 1); return(array); }
public static byte[] smethod_18(byte[] byte_0, string string_0) { System.Security.Cryptography.RijndaelManaged rijndaelManaged = new System.Security.Cryptography.RijndaelManaged(); byte[] array = new byte[32]; byte[] sourceArray = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.ASCII.GetBytes(string_0)); System.Array.Copy(sourceArray, 0, array, 0, 16); System.Array.Copy(sourceArray, 0, array, 15, 16); rijndaelManaged.Key = array; rijndaelManaged.Mode = System.Security.Cryptography.CipherMode.ECB; System.Security.Cryptography.ICryptoTransform cryptoTransform = rijndaelManaged.CreateEncryptor(); return(cryptoTransform.TransformFinalBlock(byte_0, 0, byte_0.Length)); }
/// <summary> /// 用key*对加密串B做AES-256-ECB解密 /// </summary> /// <param name="decryptStr"></param> /// <param name="key"></param> /// <returns></returns> public static string AESDecrypt(string decryptStr, string key) { byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(key); byte[] toEncryptArray = Convert.FromBase64String(decryptStr); System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = System.Security.Cryptography.CipherMode.ECB; rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7; System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(System.Text.UTF8Encoding.UTF8.GetString(resultArray)); }
public static byte[] AESEncrypt(byte[] input, string Pass) { System.Security.Cryptography.RijndaelManaged AES = new System.Security.Cryptography.RijndaelManaged(); byte[] hash = new byte[32]; byte[] temp = new MD5CryptoServiceProvider().ComputeHash(System.Text.Encoding.ASCII.GetBytes(Pass)); Array.Copy(temp, 0, hash, 0, 16); Array.Copy(temp, 0, hash, 15, 16); AES.Key = hash; AES.Mode = System.Security.Cryptography.CipherMode.ECB; System.Security.Cryptography.ICryptoTransform DESEncrypter = AES.CreateEncryptor(); return(DESEncrypter.TransformFinalBlock(input, 0, input.Length)); }
/// <summary> /// AES解密 /// </summary> /// <param name="str">需要解密的字符串</param> /// <returns>解密后的字符串</returns> public static string AESDecrypt(string str) { Byte[] toEncryptArray = Convert.FromBase64String(str); var rijndael = new System.Security.Cryptography.RijndaelManaged(); rijndael.Key = aseKey; rijndael.Mode = System.Security.Cryptography.CipherMode.ECB; rijndael.Padding = System.Security.Cryptography.PaddingMode.PKCS7; rijndael.IV = aseIV; System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(System.Text.Encoding.UTF8.GetString(resultArray)); }
internal static byte[] AES256Decrypt(byte[] block, byte[] key) { using (System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create()) { aes.Key = key; aes.Mode = System.Security.Cryptography.CipherMode.ECB; aes.Padding = System.Security.Cryptography.PaddingMode.None; using (System.Security.Cryptography.ICryptoTransform decryptor = aes.CreateDecryptor()) { return(decryptor.TransformFinalBlock(block, 0, block.Length)); } } }
public string DecryptPrivateKey(string encryptJson, string passphrase) { KeystoreV3 keystoreV3 = JsonConvert.DeserializeObject <KeystoreV3>(encryptJson); byte[] ciphertext = ByteUtil.HexStringToByteArray(keystoreV3.Crypto.Ciphertext); byte[] iv = ByteUtil.HexStringToByteArray(keystoreV3.Crypto.Cipherparams.Iv); Kdfparams kp = keystoreV3.Crypto.Kdfparams; string kdf = keystoreV3.Crypto.Kdf; byte[] derivedKey; if (kdf == "pbkdf2") { PBKDF2Params pbkdf2Params = new PBKDF2Params(); pbkdf2Params.Salt = ByteUtil.ByteArrayToHexString(kp.Salt); pbkdf2Params.DkLen = 32; pbkdf2Params.Count = 262144; derivedKey = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params); } else { ScryptParams scryptParams = new ScryptParams(); scryptParams.Salt = ByteUtil.ByteArrayToHexString(kp.Salt); scryptParams.DkLen = 32; scryptParams.P = 1; scryptParams.R = 8; scryptParams.N = 8192; derivedKey = GetDerivedKey(Encoding.Default.GetBytes(passphrase), scryptParams); } string mac = ByteUtil.ByteArrayToHexString(HashUtil.GenerateMac(derivedKey, ciphertext)); if (mac.ToUpper() != keystoreV3.Crypto.Mac) { throw new Exception("Failed to decrypt."); } byte[] encryptKey = new byte[16]; Array.Copy(derivedKey, encryptKey, 16); //TODO 加密方法待完善 System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = encryptKey, Mode = CipherMode.CBC, Padding = System.Security.Cryptography.PaddingMode.None }; System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor(); byte[] ciphertextByte = cTransform.TransformFinalBlock(ciphertext, 0, ciphertext.Length); return(ByteUtil.ByteArrayToHexString(ciphertextByte)); }
private static string EncodeInfo(byte[] rData, string info) { Byte[] toEncryptArray = Encoding.UTF8.GetBytes(info); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = rData, Mode = System.Security.Cryptography.CipherMode.ECB, Padding = System.Security.Cryptography.PaddingMode.PKCS7 }; System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return(Convert.ToBase64String(resultArray)); }