From 3502ef44e21b022bce297891a935e9dc669b86b2 Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Sat, 13 Nov 2021 13:36:37 +0100 Subject: [PATCH] =?UTF-8?q?Erweiterung=20AES/TripleDES=20f=C3=BCr=20string?= =?UTF-8?q?=20<>=20string=20(Base64/UTF-8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AMWD.Common/Utilities/CryptographyHelper.cs | 110 ++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/AMWD.Common/Utilities/CryptographyHelper.cs b/AMWD.Common/Utilities/CryptographyHelper.cs index ab43fa3..71a83c7 100644 --- a/AMWD.Common/Utilities/CryptographyHelper.cs +++ b/AMWD.Common/Utilities/CryptographyHelper.cs @@ -72,6 +72,32 @@ namespace System.Security.Cryptography return AesEncrypt(plain, password); } + /// + /// Decrypts a Base64 string using the AES algorithm and a password to an UTF-8 string. + /// + /// The encrypted Base64 encoded string. + /// The password to use for decryption (optional). + /// The decrypted UTF-8 string string. + public string DecryptAes(string cipherStr, string password = null) + { + byte[] cipher = Convert.FromBase64String(cipherStr); + byte[] plain = DecryptAes(cipher, password); + return Encoding.UTF8.GetString(plain); + } + + /// + /// Encrypts an UTF-8 string using the AES algorithm and a password to a Base64 string. + /// + /// The UTF-8 string to encrypt. + /// The password to use for encryption (optional). + /// The encrypted Base64 encoded string. + public string EncryptAes(string plainStr, string password = null) + { + byte[] plain = Encoding.UTF8.GetBytes(plainStr); + byte[] cipher = EncryptAes(plain, password); + return Convert.ToBase64String(cipher); + } + #endregion AES #region Triple DES @@ -110,6 +136,38 @@ namespace System.Security.Cryptography return TripleDesEncrypt(plain, password); } + /// + /// Decrypts a Base64 encoded string using the triple DES algorithm and a password. + /// + /// + /// When the parameter is null, the key from the file (set on initialize) is used instead. + /// + /// The encrypted Base64 encoded string. + /// The password to use for decryption (optional). + /// The decrypted UTF-8 string. + public string DecryptTripleDes(string cipherStr, string password = null) + { + byte[] cipher = Convert.FromBase64String(cipherStr); + byte[] plain = DecryptTripleDes(cipher, password); + return Encoding.UTF8.GetString(plain); + } + + /// + /// Encrypts an UTF-8 string using the triple DES algorithm and a password. + /// + /// + /// When the parameter is null, the key from the file (set on initialize) is used instead. + /// + /// The UTF-8 string to encrypt. + /// The password to use for encryption (optional). + /// The encrypted Base64 encoded string. + public string EncryptTripleDes(string plainStr, string password = null) + { + byte[] plain = Encoding.UTF8.GetBytes(plainStr); + byte[] cipher = EncryptTripleDes(plain, password); + return Convert.ToBase64String(cipher); + } + #endregion Triple DES #endregion Instance methods @@ -176,6 +234,32 @@ namespace System.Security.Cryptography return ms.ToArray(); } + /// + /// Decrypts a Base64 string using the AES algorithm and a password to an UTF-8 string. + /// + /// The encrypted Base64 encoded string. + /// The password to use for decryption. + /// The decrypted UTF-8 string string. + public static string AesDecrypt(string cipherStr, string password) + { + byte[] cipher = Convert.FromBase64String(cipherStr); + byte[] plain = AesDecrypt(cipher, password); + return Encoding.UTF8.GetString(plain); + } + + /// + /// Encrypts an UTF-8 string using the AES algorithm and a password to a Base64 string. + /// + /// The UTF-8 string to encrypt. + /// The password to use for encryption. + /// The encrypted Base64 encoded string. + public static string AesEncrypt(string plainStr, string password) + { + byte[] plain = Encoding.UTF8.GetBytes(plainStr); + byte[] cipher = AesEncrypt(plain, password); + return Convert.ToBase64String(cipher); + } + #endregion AES #region Triple DES @@ -236,6 +320,32 @@ namespace System.Security.Cryptography return ms.ToArray(); } + /// + /// Decrypts an Base64 encoded string using the triple DES algorithm and a password. + /// + /// The encrypted Base64 encoded string. + /// The password to use for decryption. + /// The decrypted UTF-8 string. + public static string TripleDesDecrypt(string cipherStr, string password) + { + byte[] cipher = Convert.FromBase64String(cipherStr); + byte[] plain = TripleDesDecrypt(cipher, password); + return Encoding.UTF8.GetString(plain); + } + + /// + /// Encrypts an UTF-8 string using the triple DES algorithm and a password. + /// + /// The UTF-8 string to encrypt. + /// The password to use for encryption. + /// The encrypted Base64 encoded string. + public static string TripleDesEncrypt(string plainStr, string password) + { + byte[] plain = Encoding.UTF8.GetBytes(plainStr); + byte[] cipher = TripleDesEncrypt(plain, password); + return Convert.ToBase64String(cipher); + } + #endregion Triple DES #endregion Encryption