Erweiterung AES/TripleDES für string <> string (Base64/UTF-8)
This commit is contained in:
@@ -72,6 +72,32 @@ namespace System.Security.Cryptography
|
|||||||
return AesEncrypt(plain, password);
|
return AesEncrypt(plain, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decrypts a Base64 string using the AES algorithm and a password to an UTF-8 string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cipherStr">The encrypted Base64 encoded string.</param>
|
||||||
|
/// <param name="password">The password to use for decryption (optional).</param>
|
||||||
|
/// <returns>The decrypted UTF-8 string string.</returns>
|
||||||
|
public string DecryptAes(string cipherStr, string password = null)
|
||||||
|
{
|
||||||
|
byte[] cipher = Convert.FromBase64String(cipherStr);
|
||||||
|
byte[] plain = DecryptAes(cipher, password);
|
||||||
|
return Encoding.UTF8.GetString(plain);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Encrypts an UTF-8 string using the AES algorithm and a password to a Base64 string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plainStr">The UTF-8 string to encrypt.</param>
|
||||||
|
/// <param name="password">The password to use for encryption (optional).</param>
|
||||||
|
/// <returns>The encrypted Base64 encoded string.</returns>
|
||||||
|
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
|
#endregion AES
|
||||||
|
|
||||||
#region Triple DES
|
#region Triple DES
|
||||||
@@ -110,6 +136,38 @@ namespace System.Security.Cryptography
|
|||||||
return TripleDesEncrypt(plain, password);
|
return TripleDesEncrypt(plain, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decrypts a Base64 encoded string using the triple DES algorithm and a password.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// When the <paramref name="password"/> parameter is <c>null</c>, the key from the file (set on initialize) is used instead.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="cipherStr">The encrypted Base64 encoded string.</param>
|
||||||
|
/// <param name="password">The password to use for decryption (optional).</param>
|
||||||
|
/// <returns>The decrypted UTF-8 string.</returns>
|
||||||
|
public string DecryptTripleDes(string cipherStr, string password = null)
|
||||||
|
{
|
||||||
|
byte[] cipher = Convert.FromBase64String(cipherStr);
|
||||||
|
byte[] plain = DecryptTripleDes(cipher, password);
|
||||||
|
return Encoding.UTF8.GetString(plain);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Encrypts an UTF-8 string using the triple DES algorithm and a password.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// When the <paramref name="password"/> parameter is <c>null</c>, the key from the file (set on initialize) is used instead.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="plainStr">The UTF-8 string to encrypt.</param>
|
||||||
|
/// <param name="password">The password to use for encryption (optional).</param>
|
||||||
|
/// <returns>The encrypted Base64 encoded string.</returns>
|
||||||
|
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 Triple DES
|
||||||
|
|
||||||
#endregion Instance methods
|
#endregion Instance methods
|
||||||
@@ -176,6 +234,32 @@ namespace System.Security.Cryptography
|
|||||||
return ms.ToArray();
|
return ms.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decrypts a Base64 string using the AES algorithm and a password to an UTF-8 string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cipherStr">The encrypted Base64 encoded string.</param>
|
||||||
|
/// <param name="password">The password to use for decryption.</param>
|
||||||
|
/// <returns>The decrypted UTF-8 string string.</returns>
|
||||||
|
public static string AesDecrypt(string cipherStr, string password)
|
||||||
|
{
|
||||||
|
byte[] cipher = Convert.FromBase64String(cipherStr);
|
||||||
|
byte[] plain = AesDecrypt(cipher, password);
|
||||||
|
return Encoding.UTF8.GetString(plain);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Encrypts an UTF-8 string using the AES algorithm and a password to a Base64 string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plainStr">The UTF-8 string to encrypt.</param>
|
||||||
|
/// <param name="password">The password to use for encryption.</param>
|
||||||
|
/// <returns>The encrypted Base64 encoded string.</returns>
|
||||||
|
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
|
#endregion AES
|
||||||
|
|
||||||
#region Triple DES
|
#region Triple DES
|
||||||
@@ -236,6 +320,32 @@ namespace System.Security.Cryptography
|
|||||||
return ms.ToArray();
|
return ms.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decrypts an Base64 encoded string using the triple DES algorithm and a password.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cipherStr">The encrypted Base64 encoded string.</param>
|
||||||
|
/// <param name="password">The password to use for decryption.</param>
|
||||||
|
/// <returns>The decrypted UTF-8 string.</returns>
|
||||||
|
public static string TripleDesDecrypt(string cipherStr, string password)
|
||||||
|
{
|
||||||
|
byte[] cipher = Convert.FromBase64String(cipherStr);
|
||||||
|
byte[] plain = TripleDesDecrypt(cipher, password);
|
||||||
|
return Encoding.UTF8.GetString(plain);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Encrypts an UTF-8 string using the triple DES algorithm and a password.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plainStr">The UTF-8 string to encrypt.</param>
|
||||||
|
/// <param name="password">The password to use for encryption.</param>
|
||||||
|
/// <returns>The encrypted Base64 encoded string.</returns>
|
||||||
|
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 Triple DES
|
||||||
|
|
||||||
#endregion Encryption
|
#endregion Encryption
|
||||||
|
|||||||
Reference in New Issue
Block a user