static Dictionary<string, string> Decrypt(string cipherText, string encryptionPassword, string salt)
{
using (var algorithm = new AesCryptoServiceProvider
{
Key =GetEncryptionKey(encryptionPassword),
IV = Convert.FromBase64String(salt)
})
using (var decryptor = algorithm.CreateDecryptor())
using (var decryptedTextStream = new MemoryStream())
using (var stringReader = new StreamReader(decryptedTextStream, Encoding.UTF8))
using (var jsonReader = new JsonTextReader(stringReader))
{
try
{
using (var cryptoStream = new CryptoStream(decryptedTextStream, decryptor, CryptoStreamMode.Write))
{
var cipherTextBytes = Convert.FromBase64String(cipherText);
cryptoStream.Write(cipherTextBytes, 0, cipherTextBytes.Length);
cryptoStream.FlushFinalBlock();
var dictionary = new Dictionary<string, string>();
var serializer = new JsonSerializer();
decryptedTextStream.Position = 0;
serializer.Populate(jsonReader, dictionary);
return dictionary;
}
}
catch (CryptographicException cryptoException)
{
throw new CommandException(
"Cannot decrypt sensitive-variables. Check your password is correct.\nError message: " +
cryptoException.Message);
}
}
}