public EncryptSettingsProvider() { //read settings from configuration var useHashingString = ConfigurationManager.AppSettings["UseHashingForEncryption"]; var useHashing = System.String.Compare(useHashingString, "false", System.StringComparison.OrdinalIgnoreCase) != 0; _encryptionPrefix = ConfigurationManager.AppSettings["EncryptionPrefix"]; if (string.IsNullOrWhiteSpace(_encryptionPrefix)) { _encryptionPrefix = "encryptedHidden_"; } var key = ConfigurationManager.AppSettings["EncryptionKey"]; if (useHashing) { var hash = new SHA256Managed(); _encryptionKey = hash.ComputeHash(Encoding.UTF8.GetBytes(key)); hash.Clear(); hash.Dispose(); } else { _encryptionKey = Encoding.UTF8.GetBytes(key); } }
static SettingsProvider() { //read settings from configuration var useHashingString = ConfigurationManager.AppSettings["UseHashingForEncryption"]; bool useHashing = true; if (string.Compare( useHashingString, "false", true ) == 0) { useHashing = false; } _encryptionPrefix = ConfigurationManager.AppSettings["EncryptionPrefix"]; if (string.IsNullOrEmpty( _encryptionPrefix )) { _encryptionPrefix = "encryptedHidden_"; } _saltGeneratorKey = ConfigurationManager.AppSettings["EncryptionSaltGeneratorKey"]; if (string.IsNullOrEmpty( _saltGeneratorKey )) { _saltGeneratorKey = "encryptionSaltKey"; } var key = ConfigurationManager.AppSettings["EncryptionKey"]; if (useHashing) { var hash = new SHA256Managed(); _encryptionKey = hash.ComputeHash( UTF8Encoding.UTF8.GetBytes( key ) ); hash.Clear(); //hash.Dispose(); } else { _encryptionKey = UTF8Encoding.UTF8.GetBytes( key ); } }
public static string SHA256Encrypt(string str) { System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed(); byte[] byte1; byte1 = s256.ComputeHash(Encoding.Default.GetBytes(str)); s256.Clear(); return(BitConverter.ToString(byte1).Replace("-", "").ToLower()); //64 }
public string Encrypt(string content) { SHA256Managed hasher = new SHA256Managed(); byte[] textWithSaltBytes = Encoding.UTF8.GetBytes(string.Concat(content, "ultraSafeSalt")); byte[] hashedBytes = hasher.ComputeHash(textWithSaltBytes); hasher.Clear(); return Convert.ToBase64String(hashedBytes); }
/// <summary> /// SHA256加密,不可逆转 /// </summary> /// <param name="str">string str:被加密的字符串</param> /// <returns>返回加密后的字符串</returns> private static string SHA256Encrypt(string str) { System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed(); byte[] byte1; byte1 = s256.ComputeHash(Encoding.Default.GetBytes(str)); s256.Clear(); return(Convert.ToBase64String(byte1)); }
/// <summary> /// SHA256加密 /// </summary> /// <param name="insertStr"></param> /// <returns></returns> public static string SHA256Encrypt(string insertStr) { byte[] tmpByte; SHA256 sha256 = new SHA256Managed(); tmpByte = sha256.ComputeHash(GetKeyByteArray(insertStr)); sha256.Clear(); return GetStringValue(tmpByte); }
/// <summary> /// sha256加密返回base64编码 /// </summary> /// <param name="strIN"></param> /// <returns></returns> public static byte[] SHA256EncryptOutByte(string strIN) { System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed(); byte[] byte1; byte1 = s256.ComputeHash(Encoding.Default.GetBytes(strIN)); s256.Clear(); return(byte1); }
/// <summary> /// SHA256加密,不可逆转 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ToSHA256(this string obj) { System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed(); byte[] byte1; byte1 = s256.ComputeHash(Encoding.Default.GetBytes(obj)); s256.Clear(); return(BitConverter.ToString(byte1).Replace("-", "")); }
/// <summary> /// SHA256 散列算法加密 /// </summary> /// <param name="data">要加密的内容</param> /// <returns></returns> public static string SHA256Encrypt(string data) { using (var sha256 = new SHA256Managed()) { byte[] buffer = Encoding.UTF8.GetBytes(data); byte[] sha256Data = sha256.ComputeHash(buffer); sha256.Clear(); return BitConverter.ToString(sha256Data).Replace("-", "").ToLower(); } }
public static string CreateStringHash(string unHashedString) { var encodedUnHashedString = Encoding.Unicode.GetBytes(unHashedString); var hashingObj = new SHA256Managed(); var hashCode = hashingObj.ComputeHash(encodedUnHashedString); var hashBase64 = Convert.ToBase64String(hashCode); var stringWithHash = unHashedString + hashBase64; hashingObj.Clear(); return (stringWithHash); }
public static string FromString(string input, HashType hashtype) { Byte[] clearBytes; Byte[] hashedBytes; string output = String.Empty; switch (hashtype) { case HashType.RIPEMD160: clearBytes = new UTF8Encoding().GetBytes(input); RIPEMD160 myRIPEMD160 = RIPEMD160Managed.Create(); hashedBytes = myRIPEMD160.ComputeHash(clearBytes); output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); break; case HashType.MD5: clearBytes = new UTF8Encoding().GetBytes(input); hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes); output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); break; case HashType.SHA1: clearBytes = Encoding.UTF8.GetBytes(input); SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); sha1.ComputeHash(clearBytes); hashedBytes = sha1.Hash; sha1.Clear(); output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); break; case HashType.SHA256: clearBytes = Encoding.UTF8.GetBytes(input); SHA256 sha256 = new SHA256Managed(); sha256.ComputeHash(clearBytes); hashedBytes = sha256.Hash; sha256.Clear(); output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); break; case HashType.SHA384: clearBytes = Encoding.UTF8.GetBytes(input); SHA384 sha384 = new SHA384Managed(); sha384.ComputeHash(clearBytes); hashedBytes = sha384.Hash; sha384.Clear(); output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); break; case HashType.SHA512: clearBytes = Encoding.UTF8.GetBytes(input); SHA512 sha512 = new SHA512Managed(); sha512.ComputeHash(clearBytes); hashedBytes = sha512.Hash; sha512.Clear(); output = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); break; } return output; }
public static string GenerateSha2PasswordHash(string userId, string password, string credentialSalt) { if (userId == null || password == null || userId.Contains(Separator) || password.Contains(Separator)) return null; var hashTool = new SHA256Managed(); var saltedInput = userId + Separator + password + Separator + credentialSalt; var saltedBytes = Encoding.UTF8.GetBytes(saltedInput); var encryptedBytes = hashTool.ComputeHash(saltedBytes); hashTool.Clear(); var output = Convert.ToBase64String(encryptedBytes); return output; }
/** * SHA256加密 */ public string SHA256Encrypt(string str) { System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed(); var bs = s256.ComputeHash(Encoding.UTF8.GetBytes(str)); s256.Clear(); var sb = new StringBuilder(); foreach (byte b in bs) { sb.Append(b.ToString("x2")); } return(sb.ToString()); }
public static byte[] Encriptar(string strPassword, string strSalt, int intIteraciones) { string _strPasswordSalt = strPassword + strSalt; SHA256Managed _objSha256 = new SHA256Managed(); byte[] _objTemporal = null; try { _objTemporal = System.Text.Encoding.UTF8.GetBytes(_strPasswordSalt); for (int i = 0; i <= intIteraciones - 1; i++) _objTemporal = _objSha256.ComputeHash(_objTemporal); } finally { _objSha256.Clear(); } return _objTemporal; }
/// <summary> /// Encrypt a string depend on sha256 algorithm /// </summary> /// <param name="inputValue">A String to be encrypted.</param> /// <returns>Encrypted string.</returns> public static byte[] GetSHA256(string passwordCustomerNumber) { if (string.IsNullOrEmpty(passwordCustomerNumber)) { return(null); } System.Security.Cryptography.SHA256Managed hashAlgorithm = new System.Security.Cryptography.SHA256Managed(); try { return(hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(passwordCustomerNumber))); } finally { hashAlgorithm.Clear(); } }
public Crypto(bool useSalt) { if (!Initialized) { Initialized = true; string strKeyPhrase = @"Apple*Crisp7"; byte[] byteKeyPhrase = Encoding.ASCII.GetBytes(strKeyPhrase); SHA256Managed sha256 = new SHA256Managed(); sha256.ComputeHash(byteKeyPhrase); Key = new byte[24]; for (int i = 0; i < 24; i++) Key[i] = sha256.Hash[i]; string strIVPhrase = @"Peach%Pie5"; byte[] byteIVPhrase = Encoding.ASCII.GetBytes(strIVPhrase); sha256.ComputeHash(byteIVPhrase); IV = new byte[16]; for (int i = 0; i < 16; i++) IV[i] = sha256.Hash[i]; sha256.Clear(); } UseSalt = useSalt; }
public static bool TestReceivedStringHash(string stringWithHash, out string originalStr) { if (stringWithHash.Length < 45) { originalStr = null; return (true); } var hashCodeString = stringWithHash.Substring(stringWithHash.Length - 44); var unHashedString = stringWithHash.Substring(0, stringWithHash.Length - 44); var hashCode = Convert.FromBase64String(hashCodeString); var encodedUnHashedString = Encoding.Unicode.GetBytes(unHashedString); var hashingObj = new SHA256Managed(); var receivedHashCode = hashingObj.ComputeHash(encodedUnHashedString); bool hasBeenTamperedWith = false; for (int counter = 0; counter < receivedHashCode.Length; counter++) { if (receivedHashCode[counter] != hashCode[counter]) { hasBeenTamperedWith = true; break; } } if (!hasBeenTamperedWith) { originalStr = unHashedString; } else { originalStr = null; } hashingObj.Clear(); return (hasBeenTamperedWith); }
/// <summary> /// This is the descrypt method from the ABC4Trust Lite doc appendix B p. 227 /// It will decrypt the chipher based on the N provided. /// Please note that the smartcard return Big-endian byte stream and descrypt expect /// little endian data. /// </summary> /// <param name="keyPair">keys used to decrypt the data</param> /// <param name="chipher">data to descrypt</param> /// <returns>Byte stream of the descrypted data.</returns> public static byte[] Decrypt(KeyPair keyPair, byte[] chipher) { BigInteger d = ModInverse(new BigInteger(3), keyPair.Phi); BigInteger iChiper = new BigInteger(chipher); BigInteger plain = BigInteger.ModPow(iChiper, d, keyPair.N); byte[] NArray = keyPair.N.ToByteArray(); byte[] plainBytes = plain.ToByteArray(); Array.Reverse(plainBytes, 0, plainBytes.Length); byte[] plainBytesWithZero = new byte[NArray.Length]; Buffer.BlockCopy(plainBytes, 0, plainBytesWithZero, plainBytesWithZero.Length - plainBytes.Length, plainBytes.Length); byte[] pad = new byte[plainBytesWithZero.Length - 32]; byte[] h = new byte[32]; Buffer.BlockCopy(plainBytesWithZero, 0, pad, 0, pad.Length); Buffer.BlockCopy(plainBytesWithZero, pad.Length, h, 0, 32); SHA256 shaM = new SHA256Managed(); byte[] hPrime = shaM.ComputeHash(pad); if (!Utils.ByteArrayCompare(hPrime, h)) { Utils.PrintByteArrayToConsole(hPrime); Utils.PrintByteArrayToConsole(h); } shaM.Clear(); //dispose the sha256 object. byte[] L = new byte[2]; Buffer.BlockCopy(pad, 1, L, 0, 2); Array.Reverse(L, 0, L.Length); int LSize = BitConverter.ToInt16(L, 0); byte[] data = new byte[LSize]; Buffer.BlockCopy(pad, 3, data, 0, data.Length); return data; }
/// <summary> /// SHA256加密 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string SHA256Encrypt(string str) { SHA256 s256 = new SHA256Managed(); byte[] bytes; bytes = s256.ComputeHash(ASCIIEncoding.Default.GetBytes(str)); s256.Clear(); // 创建一个新的Stringbuilder来保存字节,并且创建一个字符串。 StringBuilder sBuilder = new StringBuilder(); // 循环哈希值的每一个字节,并以十六进制格式保存为字符串 for (int i = 0; i < bytes.Length; i++) { sBuilder.Append(bytes[i].ToString("x2")); } return sBuilder.ToString(); }
/// <summary> /// SHs the a256. /// </summary> /// <returns>The a256.</returns> /// <param name="str">String.</param> protected string SHA256(string str) { byte[] tmpByte; SHA256 sha256 = new SHA256Managed(); tmpByte = sha256.ComputeHash(Encoding.UTF8.GetBytes(str)); sha256.Clear(); string result = string.Empty; foreach (byte x in tmpByte) { result += string.Format("{0:x2}", x); } return result.ToUpper(); }
/// <summary> /// 计算输入数据的 SHA256 哈希值 /// </summary> /// <param name="strIN">输入的数据.</param> /// <returns></returns> public static string SHA256Encrypt(string strIN) { //string strIN = getstrIN(strIN); byte[] tmpByte; SHA256 sha256 = new SHA256Managed(); tmpByte = sha256.ComputeHash(StringToBytes(strIN)); sha256.Clear(); return BytesToString(tmpByte); }
/// <summary> /// Generates a key and vector based on the Hash algorithm in SHA256Managed. Basically it takes an /// string and generates a 256 bit hash value for that. /// </summary> /// <param name="inputValue">Symmetric key</param> private void GenerateKeyVector(string inputValue) { byte[] keyValue = Encoding.UTF8.GetBytes(inputValue); byte[] resultValue; int idx; using (SHA256 hashMgr = new SHA256Managed()) { resultValue = hashMgr.ComputeHash(keyValue); hashMgr.Clear(); for (idx = 0; idx < 24; idx++) { symKey[idx] = resultValue[idx]; } for (idx = 24; idx < 32; idx++) { symIV[idx - 24] = resultValue[idx]; } } }
/// <summary> /// SHA256加密,不可逆转 /// </summary> /// <param name="str">string str:被加密的字符串</param> /// <returns>返回加密后的字符串</returns> public static string SHA256Encrypt(string str) { System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed(); byte[] byte1; byte1 = s256.ComputeHash(Encoding.Default.GetBytes(str)); s256.Clear(); return Convert.ToBase64String(byte1); }
public string SHA256Encrypt(string strIN) { byte[] tmpByte; SHA256 sha256 = new SHA256Managed(); tmpByte = sha256.ComputeHash(System.Text.Encoding.ASCII.GetBytes(strIN)); string strResult = BitConverter.ToString(tmpByte); strResult = strResult.Replace("-", ""); sha256.Clear(); sha256 = null; return strResult; }
private static byte[] HashAndTransform(byte[] pbData, byte[] pbTrfKey32, ulong uTrfRounds) { SHA256Managed sha256 = new SHA256Managed(); byte[] pbHash = sha256.ComputeHash(pbData); sha256.Clear(); if(!CompositeKey.TransformKeyManaged(pbHash, pbTrfKey32, uTrfRounds)) return null; sha256 = new SHA256Managed(); pbHash = sha256.ComputeHash(pbHash); sha256.Clear(); return pbHash; }
public string SHA256Encrypt(string strIN) { //string strIN = getstrIN(strIN); byte[] tmpByte; SHA256 sha256 = new SHA256Managed(); tmpByte = sha256.ComputeHash(GetKeyByteArray(strIN)); sha256.Clear(); return GetStringValue(tmpByte); }
public static string SHA256Sign(string message) { var result = string.Empty; var sha256 = new SHA256Managed(); byte[] s = sha256.ComputeHash(Encoding.UTF8.GetBytes(message)); for (int i = 0; i < s.Length; i++) { result += s[i].ToString("X2"); } sha256.Clear(); return result; }
/// <summary> /// Gets the hash. /// </summary> /// <param name="value">The value.</param> /// <returns></returns> public static string GetBase64Hash(string value) { byte[] bytesPass = Encoding.UTF8.GetBytes(value); SHA256Managed sha = new SHA256Managed(); byte[] bytesHash = sha.ComputeHash(bytesPass); sha.Clear(); return Convert.ToBase64String(bytesHash); }
/// <summary> /// Generates a key and vector based on the Hash algorithm in SHA256Managed. Basically it takes an /// string and generates a 256 bit hash value for that. /// </summary> /// <param name="key">String symmetric key</param> /// <returns>None</returns> private void GenerateKeyVector(string inputValue) { byte[] keyValue = Encoding.UTF8.GetBytes(inputValue); //Byte Key byte[] resultValue; // Result Key- contains the hash value of actual key int iIdx; // Indexer using (SHA256 hashMgr = new SHA256Managed()) { //Generate the hashed key- 256 bits/ 32 bytes resultValue = hashMgr.ComputeHash(keyValue); //clear resources hashMgr.Clear(); //now Initialize the 'Key' array with the lower 16 bytes/128 bits of the //Hash of the 'key' string provided for (iIdx = 0; iIdx < 16; iIdx++) { symKey[iIdx] = resultValue[iIdx]; } //Initialize the 'Vector' array with the upper 16 Bytes/128 bits of //Hash result for (iIdx = 16; iIdx < 32; iIdx++) { symIV[iIdx - 16] = resultValue[iIdx]; } } }
/// <summary> /// Encrypts a given string (password) using the SHA1 cryptography algorithm /// </summary> /// <param name="password">string (passowrd) to encrypt</param> /// <returns>Encrypted hash for the supplied string (password)</returns> public string HashPassword(string password) { Byte[] passwordBytes = Encoding.Unicode.GetBytes(password); SHA256Managed hashProvider = new SHA256Managed(); hashProvider.Initialize(); passwordBytes = hashProvider.ComputeHash(passwordBytes); hashProvider.Clear(); return Convert.ToBase64String(passwordBytes); }
/// <summary> /// /// </summary> /// <param name="bytIN"></param> /// <returns></returns> public string SHA256Encrypt(byte[] bytIN) { byte[] tmpByte; SHA256 sha256 = new SHA256Managed(); tmpByte = sha256.ComputeHash(bytIN); sha256.Clear(); return GetStringValue(tmpByte); }
private static void DeriveKeyAndIV(byte[] data, byte[] salt, int count, out byte[] key, out byte[] iv) { List<byte> hashList = new List<byte>(); byte[] currentHash = new byte[0]; int preHashLength = data.Length + ((salt != null) ? salt.Length : 0); byte[] preHash = new byte[preHashLength]; System.Buffer.BlockCopy(data, 0, preHash, 0, data.Length); if (salt != null) System.Buffer.BlockCopy(salt, 0, preHash, data.Length, salt.Length); SHA256Managed hash = new SHA256Managed(); currentHash = hash.ComputeHash(preHash); for (int i = 1; i < count; i++) { currentHash = hash.ComputeHash(currentHash); } hashList.AddRange(currentHash); while (hashList.Count < 48) // for 32-byte key and 16-byte iv { preHashLength = currentHash.Length + data.Length + ((salt != null) ? salt.Length : 0); preHash = new byte[preHashLength]; System.Buffer.BlockCopy(currentHash, 0, preHash, 0, currentHash.Length); System.Buffer.BlockCopy(data, 0, preHash, currentHash.Length, data.Length); if (salt != null) System.Buffer.BlockCopy(salt, 0, preHash, currentHash.Length + data.Length, salt.Length); currentHash = hash.ComputeHash(preHash); for (int i = 1; i < count; i++) { currentHash = hash.ComputeHash(currentHash); } hashList.AddRange(currentHash); } hash.Clear(); key = new byte[32]; iv = new byte[16]; hashList.CopyTo(0, key, 0, 32); hashList.CopyTo(32, iv, 0, 16); }