| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | namespace Services.DataServices |
| | | 5 | | { |
| | | 6 | | public class CryptographyServices |
| | | 7 | | { |
| | | 8 | | private string _secret = null!; |
| | | 9 | | private byte[] _key = null!; |
| | | 10 | | |
| | | 11 | | public void SetSecret(string secret) |
| | | 12 | | { |
| | 0 | 13 | | _secret = secret; |
| | 0 | 14 | | _key = Encoding.UTF8.GetBytes(_secret); |
| | 0 | 15 | | } |
| | | 16 | | |
| | | 17 | | public string Encrypt(string plainText) |
| | | 18 | | { |
| | 0 | 19 | | byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); |
| | | 20 | | byte[] encryptedBytes; |
| | | 21 | | byte[] iv; |
| | | 22 | | |
| | | 23 | | // Set up the encryption objects |
| | 0 | 24 | | using (Aes aes = Aes.Create()) |
| | | 25 | | { |
| | 0 | 26 | | aes.Key = _key; |
| | 0 | 27 | | aes.GenerateIV(); |
| | 0 | 28 | | iv = aes.IV; |
| | 0 | 29 | | aes.Mode = CipherMode.CBC; |
| | 0 | 30 | | aes.Padding = PaddingMode.PKCS7; |
| | | 31 | | |
| | | 32 | | // Encrypt the input plaintext using the AES algorithm |
| | 0 | 33 | | using (ICryptoTransform encryptor = aes.CreateEncryptor()) |
| | | 34 | | { |
| | 0 | 35 | | encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); |
| | 0 | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | // Combine IV and encrypted bytes |
| | 0 | 40 | | byte[] combinedBytes = new byte[iv.Length + encryptedBytes.Length]; |
| | 0 | 41 | | Array.Copy(iv, 0, combinedBytes, 0, iv.Length); |
| | 0 | 42 | | Array.Copy(encryptedBytes, 0, combinedBytes, iv.Length, encryptedBytes.Length); |
| | | 43 | | |
| | 0 | 44 | | return Convert.ToBase64String(combinedBytes); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public string Decrypt(string cipherText) |
| | | 48 | | { |
| | 0 | 49 | | byte[] combinedBytes = Convert.FromBase64String(cipherText); |
| | 0 | 50 | | byte[] iv = new byte[16]; |
| | 0 | 51 | | byte[] cipherBytes = new byte[combinedBytes.Length - iv.Length]; |
| | | 52 | | byte[] decryptedBytes; |
| | | 53 | | |
| | | 54 | | // Extract IV and cipher text |
| | 0 | 55 | | Array.Copy(combinedBytes, 0, iv, 0, iv.Length); |
| | 0 | 56 | | Array.Copy(combinedBytes, iv.Length, cipherBytes, 0, cipherBytes.Length); |
| | | 57 | | |
| | | 58 | | // Set up the decryption objects |
| | 0 | 59 | | using (Aes aes = Aes.Create()) |
| | | 60 | | { |
| | 0 | 61 | | aes.Key = _key; |
| | 0 | 62 | | aes.IV = iv; |
| | 0 | 63 | | aes.Mode = CipherMode.CBC; |
| | 0 | 64 | | aes.Padding = PaddingMode.PKCS7; |
| | | 65 | | |
| | | 66 | | // Decrypt the input ciphertext using the AES algorithm |
| | 0 | 67 | | using (ICryptoTransform decryptor = aes.CreateDecryptor()) |
| | | 68 | | { |
| | 0 | 69 | | decryptedBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length); |
| | 0 | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return Encoding.UTF8.GetString(decryptedBytes); |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | } |