Skip to content

Commit

Permalink
refactor and optimize code
Browse files Browse the repository at this point in the history
Reduce nesting, improve variable names, use modern C# features where possible
  • Loading branch information
midwan committed Mar 20, 2024
1 parent 51e9f81 commit 595b542
Show file tree
Hide file tree
Showing 9 changed files with 1,619 additions and 1,604 deletions.
132 changes: 64 additions & 68 deletions MB_SubSonic/Domain/AesEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,76 @@
using System.Security.Cryptography;
using System.Text;

namespace MusicBeePlugin.Domain
namespace MusicBeePlugin.Domain;

/// <summary>
/// Utility class that handles encryption
/// </summary>
internal static class AesEncryption
{
/// <summary>
/// Utility class that handles encryption
/// Encrypts a string
/// </summary>
internal static class AesEncryption
/// <param name="plainText">Text to be encrypted</param>
/// <param name="password">Password to encrypt with</param>
/// <param name="salt">Salt to encrypt with</param>
/// <param name="passwordIterations">Number of iterations to do</param>
/// <param name="initialVector">Needs to be 16 ASCII characters long</param>
/// <param name="keySize">Can be 128, 192, or 256</param>
/// <returns>An encrypted string</returns>
public static string Encrypt(string plainText, string password, string salt = "Preposterous",
int passwordIterations = 1000, string initialVector = "OFRna73m*aze01xY", int keySize = 256)
{
#region Static Functions
if (string.IsNullOrEmpty(plainText))
return string.Empty;

var initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
var saltValueBytes = Encoding.ASCII.GetBytes(salt);
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
var derivedPassword = new Rfc2898DeriveBytes(password, saltValueBytes, passwordIterations);
var keyBytes = derivedPassword.GetBytes(keySize / 8);
var symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC };

using var encryptor = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes);
using var memStream = new MemoryStream();
using var cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write);

cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();

return Convert.ToBase64String(memStream.ToArray());
}

/// <summary>
/// Decrypts a string
/// </summary>
/// <param name="cipherText">Text to be decrypted</param>
/// <param name="password">Password to decrypt with</param>
/// <param name="salt">Salt to decrypt with</param>
/// <param name="passwordIterations">Number of iterations to do</param>
/// <param name="initialVector">Needs to be 16 ASCII characters long</param>
/// <param name="keySize">Can be 128, 192, or 256</param>
/// <returns>A decrypted string</returns>
public static string Decrypt(string cipherText, string password, string salt = "Preposterous",
int passwordIterations = 1000, string initialVector = "OFRna73m*aze01xY", int keySize = 256)
{
if (string.IsNullOrEmpty(cipherText))
return string.Empty;

var initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
var saltValueBytes = Encoding.ASCII.GetBytes(salt);
var cipherTextBytes = Convert.FromBase64String(cipherText);
var derivedPassword = new Rfc2898DeriveBytes(password, saltValueBytes, passwordIterations);
var keyBytes = derivedPassword.GetBytes(keySize / 8);
var symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC };

/// <summary>
/// Encrypts a string
/// </summary>
/// <param name="plainText">Text to be encrypted</param>
/// <param name="password">Password to encrypt with</param>
/// <param name="salt">Salt to encrypt with</param>
/// <param name="passwordIterations">Number of iterations to do</param>
/// <param name="initialVector">Needs to be 16 ASCII characters long</param>
/// <param name="keySize">Can be 128, 192, or 256</param>
/// <returns>An encrypted string</returns>
public static string Encrypt(string plainText, string password, string salt = "Preposterous",
int passwordIterations = 1000, string initialVector = "OFRna73m*aze01xY", int keySize = 256)
{
if (string.IsNullOrEmpty(plainText))
return "";
var initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
var saltValueBytes = Encoding.ASCII.GetBytes(salt);
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
var derivedPassword = new Rfc2898DeriveBytes(password, saltValueBytes, passwordIterations);
var keyBytes = derivedPassword.GetBytes(keySize/8);
var symmetricKey = new RijndaelManaged {Mode = CipherMode.CBC};
using var encryptor = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes);
using var memStream = new MemoryStream();
using var cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
var cipherTextBytes = memStream.ToArray();
memStream.Close();
cryptoStream.Close();
symmetricKey.Clear();
return Convert.ToBase64String(cipherTextBytes);
}
using var decryptor = symmetricKey.CreateDecryptor(keyBytes, initialVectorBytes);
using var memStream = new MemoryStream(cipherTextBytes);
using var cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read);

/// <summary>
/// Decrypts a string
/// </summary>
/// <param name="cipherText">Text to be decrypted</param>
/// <param name="password">Password to decrypt with</param>
/// <param name="salt">Salt to decrypt with</param>
/// <param name="passwordIterations">Number of iterations to do</param>
/// <param name="initialVector">Needs to be 16 ASCII characters long</param>
/// <param name="keySize">Can be 128, 192, or 256</param>
/// <returns>A decrypted string</returns>
public static string Decrypt(string cipherText, string password, string salt = "Preposterous",
int passwordIterations = 1000, string initialVector = "OFRna73m*aze01xY", int keySize = 256)
{
if (string.IsNullOrEmpty(cipherText))
return "";
var initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
var saltValueBytes = Encoding.ASCII.GetBytes(salt);
var cipherTextBytes = Convert.FromBase64String(cipherText);
var derivedPassword = new Rfc2898DeriveBytes(password, saltValueBytes, passwordIterations);
var keyBytes = derivedPassword.GetBytes(keySize/8);
var symmetricKey = new RijndaelManaged {Mode = CipherMode.CBC};
var plainTextBytes = new byte[cipherTextBytes.Length];
using var decryptor = symmetricKey.CreateDecryptor(keyBytes, initialVectorBytes);
using var memStream = new MemoryStream(cipherTextBytes);
using var cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read);
var byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memStream.Close();
cryptoStream.Close();
symmetricKey.Clear();
return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount);
}
var plainTextBytes = new byte[cipherTextBytes.Length];
var byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);

#endregion
return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount);
}
}
Loading

0 comments on commit 595b542

Please sign in to comment.