private static string EncryptForObscurity(string dataToEncrypt, int randomization) { if (string.IsNullOrWhiteSpace(dataToEncrypt)) { return(dataToEncrypt); } byte[] key = RandomStringGenerator.GetBytesFromString(encryptKeyArray[randomization]); byte[] iv = RandomStringGenerator.GetBytesFromString(initVectorArray[randomization]); byte[] inputBytes = RandomStringGenerator.GetBytesFromString(dataToEncrypt); var des3Encrypt = CreateEncryptor(); MemoryStream mstream = new MemoryStream(); CryptoStream cstream = new CryptoStream(mstream, des3Encrypt, CryptoStreamMode.Write); cstream.Write(inputBytes, 0, inputBytes.Length); cstream.FlushFinalBlock(); byte[] encryptedDataBytes = mstream.ToArray(); mstream.Close(); cstream.Close(); return(randomization.ToString() + separator + RandomStringGenerator.ToEncodedString(encryptedDataBytes)); }
private static string DecryptForObscurity(string dataToDecrypt, int randomization) { if (string.IsNullOrWhiteSpace(dataToDecrypt)) { return(dataToDecrypt); } byte[] decryptedDataBytes = null; MemoryStream mstream = null; CryptoStream cstream = null; try { byte[] key = RandomStringGenerator.GetBytesFromString(encryptKeyArray[randomization]); byte[] iv = RandomStringGenerator.GetBytesFromString(initVectorArray[randomization]); byte[] encryptedBytes = RandomStringGenerator.FromEncodedString(dataToDecrypt); TripleDESCryptoServiceProvider des3provider = new TripleDESCryptoServiceProvider(); des3provider.Mode = CipherMode.CBC; ICryptoTransform des3decrypt = des3provider.CreateDecryptor(key, iv); mstream = new MemoryStream(); cstream = new CryptoStream(mstream, des3decrypt, CryptoStreamMode.Write); cstream.Write(encryptedBytes, 0, encryptedBytes.Length); cstream.FlushFinalBlock(); decryptedDataBytes = mstream.ToArray(); } catch { //swallow errors } finally { if (mstream != null) { mstream.Close(); } if (cstream != null) { cstream.Close(); } } if (decryptedDataBytes == null) { return(null); } else { return(RandomStringGenerator.GetStringFromBytes(decryptedDataBytes)); } }
public static string ComputeHash(string password, string salt, SaltMethod saltMethod = SaltMethod.DoubleSalt, HashSize hashSize = HashSize.S160) { if (string.IsNullOrWhiteSpace(password)) { throw new ArgumentNullException("password"); } if (string.IsNullOrWhiteSpace(salt)) { throw new ArgumentNullException("salt"); } string saltedPassword = GetSaltedPassword(password, salt, saltMethod); byte[] bytesOfString = RandomStringGenerator.GetBytesFromString(saltedPassword); byte[] hash = ComputeHash(bytesOfString, hashSize); return(RandomStringGenerator.ToEncodedString(hash)); }
public static string Encrypt(string dataToEncrypt) { if (string.IsNullOrWhiteSpace(dataToEncrypt)) { return(dataToEncrypt); } byte[] inputBytes = RandomStringGenerator.GetBytesFromString(dataToEncrypt); var des3Encrypt = CreateEncryptor(); MemoryStream mstream = new MemoryStream(); CryptoStream cstream = new CryptoStream(mstream, des3Encrypt, CryptoStreamMode.Write); cstream.Write(inputBytes, 0, inputBytes.Length); cstream.FlushFinalBlock(); byte[] encryptedDataBytes = mstream.ToArray(); mstream.Close(); cstream.Close(); return(RandomStringGenerator.ToEncodedString(encryptedDataBytes)); }