LitDev.StringEncryption.DecryptString C# (CSharp) Method

DecryptString() public static method

public static DecryptString ( string ciphertext, string passphrase ) : string
ciphertext string
passphrase string
return string
        public static string DecryptString(string ciphertext, string passphrase)
        {
            try
            {
                var inputs = ciphertext.Split(":".ToCharArray(), 3);
                var iv = Convert.FromBase64String(inputs[0]); // Extract the IV
                var salt = Convert.FromBase64String(inputs[1]); // Extract the salt
                var ciphertextBytes = Convert.FromBase64String(inputs[2]); // Extract the ciphertext

                // Derive the key from the supplied passphrase and extracted salt
                byte[] key = DeriveKeyFromPassphrase(passphrase, salt);

                // Decrypt
                byte[] plaintext = DoCryptoOperation(ciphertextBytes, key, iv, false);

                // Return the decrypted string
                return Encoding.UTF8.GetString(plaintext);
            }
            catch (Exception ex)
            {
                Utilities.OnError(Utilities.GetCurrentMethod(), ex);
            }
            return "";
        }

Usage Example

Example #1
0
 /// <summary>
 /// Decrypt an AES encrypted cypher (previously encrypted) using a password key.
 /// </summary>
 /// <param name="cypher">The encrypted text (cypher).</param>
 /// <param name="password">The password key for the encryption.</param>
 /// <returns>The original unencrypted text or "" if password and cypher don't match.</returns>
 public static Primitive AESDecrypt(Primitive cypher, Primitive password)
 {
     return(StringEncryption.DecryptString(cypher, password));
 }