/// <summary> /// Decrypt an encrypted text. /// </summary> /// <param name="encryptedText">The encrypted text.</param> /// <param name="options">The decryption options.</param> /// <returns>Returns a decrypted text.</returns> public static string Decrypt(string encryptedText, IDictionary <string, object> options) { var toDecrypt = encryptedText; var decryptedText = string.Empty; if (options == null) { throw new ArgumentNullException(nameof(options)); } var decodeFromBase64 = options.ContainsKey("DecodeFromBase64") && Convert.ToBoolean(options["DecodeFromBase64"], CultureInfo.InvariantCulture); var encodeToBase64 = options.ContainsKey("EncodeToBase64") && Convert.ToBoolean(options["EncodeToBase64"], CultureInfo.InvariantCulture); var method = options.ContainsKey("Method") ? options["Method"].ToString() : string.Empty; switch (method) { // RSA case "RSA": var privateKeyPath = options["PrivateKeyPath"].ToString(); if (privateKeyPath.StartsWith("\"", StringComparison.InvariantCulture)) { privateKeyPath = privateKeyPath.Substring(1, privateKeyPath.Length - 1); } if (privateKeyPath.EndsWith("\"", StringComparison.InvariantCulture)) { privateKeyPath = privateKeyPath.Substring(0, privateKeyPath.Length - 1); } decryptedText = decodeFromBase64 ? RsaUtils.Base64DecodeAndDecrypt(toDecrypt, privateKeyPath) : RsaUtils.Decrypt(toDecrypt, privateKeyPath); break; // AES case "AES": var key = options["key"].ToString(); var initializationValue = options["iniValue"].ToString(); decryptedText = decodeFromBase64 ? AesUtils.Base64DecodeAndDecrypt(toDecrypt, key, initializationValue) : AesUtils.Decrypt(toDecrypt, key, initializationValue); break; // Base64 case "Base64": decryptedText = GeneralUtils.Base64Decode(toDecrypt); break; } return(encodeToBase64 ? GeneralUtils.Base64Encode(decryptedText) : decryptedText); }