/// <summary> /// Decrypt the data using the given key and passed IV /// </summary> public static string DecryptData(SymmetricCipherResults cipherResults, SymmetricAlgorithmTypeEnum symmetricAlgorithm, string key) { key = SymmetricOperation.MakeKeyLegalSize(symmetricAlgorithm, key); byte[] IV = Convert.FromBase64String(cipherResults.IV); string PlainText = SymmetricOperation.DecryptFromBase64(symmetricAlgorithm, cipherResults.CipherText, key, IV, System.Text.Encoding.UTF8); return(PlainText); }
/// <summary> /// Encrypt the data using the symmetric algorithm provided and key. Generates the Initialization Vector. /// Returns the IV and the encrypted string /// </summary> public static SymmetricCipherResults EncryptData(string plainText, SymmetricAlgorithmTypeEnum symmetricAlgorithm, string key) { SymmetricAlgorithm algorithm = SymmetricOperation.CreateSymmetricAlgorithmProvider(symmetricAlgorithm); key = SymmetricOperation.MakeKeyLegalSize(symmetricAlgorithm, key); algorithm.GenerateIV(); string iv = Convert.ToBase64String(algorithm.IV); string cipherText = SymmetricOperation.EncryptToBase64(symmetricAlgorithm, plainText, key, algorithm.IV, System.Text.Encoding.UTF8); return(new SymmetricCipherResults() { CipherText = cipherText, IV = iv }); }
/// <summary> /// Decrypt data from the secure string. /// </summary> public static string GetDataFromSecureString(string secretKey, byte[] bufInitVector, string base64Data) { secretKey = SymmetricOperation.MakeKeyLegalSize(AlgorithmType, secretKey); // Decrypt the secret string and make sure that is not expired string plainStr = SymmetricOperation.DecryptFromBase64(AlgorithmType, base64Data, secretKey, bufInitVector, ASCIIEncoding.ASCII); //Parse data to remove the salt and the expiration timestamp plainStr = plainStr.Substring(SaltLength); string sExpirationTim = plainStr.Substring(plainStr.Length - 14); DateTime dtExpirationTim = DateTime.ParseExact(sExpirationTim, "yyyyMMddHHmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo); // Check if the contetn should expire if (dtExpirationTim > DateTime.Now) { return(plainStr.Substring(0, plainStr.Length - 14)); } return(string.Empty); //Bad format }
/// <summary> /// EncryptedString constructor. /// </summary> public EncryptedString(string secretKey, string plainStr, Int32 expirationIntervalMin) { _secretKey = SymmetricOperation.MakeKeyLegalSize(AlgorithmType, secretKey); _plainString = plainStr; _expirationTimeSpan = new TimeSpan(0, expirationIntervalMin, 0); }