47 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C#
		
	
	
| using System;
 | |
| using System.Security.Cryptography;
 | |
| using System.Text;
 | |
| 
 | |
| namespace IRaCIS.Core.Infrastructure
 | |
| {
 | |
|     public class SymmetricEncryption
 | |
|     {
 | |
|         public static string Encrypt(string plainText, string key)
 | |
|         {
 | |
|             byte[] keyBytes = Encoding.UTF8.GetBytes(key);
 | |
|             byte[] ivBytes = new byte[16]; // Initialization vector (IV)
 | |
| 
 | |
|             using (Aes aes = Aes.Create())
 | |
|             {
 | |
|                 aes.Key = keyBytes;
 | |
|                 aes.IV = ivBytes;
 | |
|                 ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
 | |
| 
 | |
|                 byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
 | |
|                 byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
 | |
|                 return Convert.ToBase64String(encryptedBytes);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public static string Decrypt(string cipherText, string key)
 | |
|         {
 | |
|             byte[] keyBytes = Encoding.UTF8.GetBytes(key);
 | |
|             byte[] ivBytes = new byte[16]; // Initialization vector (IV)
 | |
| 
 | |
|             using (Aes aes = Aes.Create())
 | |
|             {
 | |
|                 aes.Key = keyBytes;
 | |
|                 aes.IV = ivBytes;
 | |
|                 ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
 | |
| 
 | |
|                 byte[] cipherBytes = Convert.FromBase64String(cipherText);
 | |
|                 byte[] decryptedBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
 | |
|                 return Encoding.UTF8.GetString(decryptedBytes);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
| 
 | |
| 
 | |
| }
 |