This commit is contained in:
MeysamMoghaddam
2025-10-20 15:20:29 +03:30
parent ee17929da0
commit b30202b7bd
9 changed files with 298 additions and 138 deletions

View File

@@ -0,0 +1,122 @@
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using System.Security.Cryptography;
using System.Text;
namespace FrontOffice.Main.Utilities;
public class MobileNumberEncryptor
{
private readonly string _key;
private readonly string _iv;
public MobileNumberEncryptor(IConfiguration configuration)
{
var encryptionSettings = configuration.GetSection("EncryptionSettings").Get<EncryptionSettings>();
_key = encryptionSettings?.Key ?? throw new ArgumentNullException("Encryption Key not found in configuration");
_iv = encryptionSettings?.IV ?? throw new ArgumentNullException("Encryption IV not found in configuration");
// اعتبارسنجی سایز
ValidateKeyAndIV();
}
public MobileNumberEncryptor(string key, string iv)
{
_key = key;
_iv = iv;
ValidateKeyAndIV();
}
private void ValidateKeyAndIV()
{
try
{
byte[] keyBytes = Convert.FromBase64String(_key);
byte[] ivBytes = Convert.FromBase64String(_iv);
if (keyBytes.Length != 32)
throw new ArgumentException("Key must be 32 bytes in Base64 format");
if (ivBytes.Length != 16)
throw new ArgumentException("IV must be 16 bytes in Base64 format");
}
catch (FormatException)
{
throw new ArgumentException("Key or IV is not valid Base64 string");
}
}
public string EncryptMobileNumber(string mobileNumber)
{
if (string.IsNullOrEmpty(mobileNumber))
throw new ArgumentException("Mobile number cannot be null or empty");
try
{
byte[] key = Convert.FromBase64String(_key);
byte[] iv = Convert.FromBase64String(_iv);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(mobileNumber);
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
aesAlg.Mode = CipherMode.ECB;
aesAlg.Padding = PaddingMode.PKCS7;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(plainTextBytes, 0, plainTextBytes.Length);
csEncrypt.FlushFinalBlock();
byte[] encryptedBytes = msEncrypt.ToArray();
return Convert.ToBase64String(encryptedBytes);
}
}
}
}
catch (Exception ex)
{
throw new Exception($"Encryption failed: {ex.Message}", ex);
}
}
public string DecryptMobileNumber(string encryptedMobileNumber)
{
if (string.IsNullOrEmpty(encryptedMobileNumber))
throw new ArgumentException("Encrypted mobile number cannot be null or empty");
try
{
byte[] key = Convert.FromBase64String(_key);
byte[] iv = Convert.FromBase64String(_iv);
byte[] cipherTextBytes = Convert.FromBase64String(encryptedMobileNumber);
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
aesAlg.Mode = CipherMode.ECB;
aesAlg.Padding = PaddingMode.PKCS7;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherTextBytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
return srDecrypt.ReadToEnd();
}
}
}
}
}
catch (Exception ex)
{
throw new Exception($"Decryption failed: {ex.Message}", ex);
}
}
}