Advertisement
CasperHansen

AES encryption

Apr 2nd, 2012
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.60 KB | None | 0 0
  1. public static class CryptationUtility
  2.     {
  3.         public static string BytesToString(byte[] bytesToConvert)
  4.         {
  5.             return Encoding.ASCII.GetString(bytesToConvert);
  6.         }
  7.  
  8.         public static byte[] StringToBytes(string stringToConvert)
  9.         {
  10.             return Encoding.ASCII.GetBytes(stringToConvert);
  11.         }
  12.  
  13.         public static string ToUrlSafeBase64(byte[] input)
  14.         {
  15.             return Convert.ToBase64String(input).Replace("+", "-").Replace("/", "_");
  16.         }
  17.  
  18.         public static byte[] FromUrlSafeBase64(string input)
  19.         {
  20.             return Convert.FromBase64String(input.Replace("-", "+").Replace("_", "/"));
  21.         }
  22.     }
  23.  
  24. public class AesCryptator : IAesCryptator
  25.     {
  26.         private RijndaelManaged _rijndaelObj;
  27.  
  28.         public AesCryptator()
  29.         {
  30.             _rijndaelObj = new RijndaelManaged();
  31.  
  32.             // Set encryption mode to Cipher Block Chaining
  33.             _rijndaelObj.Mode = CipherMode.CBC;
  34.             _rijndaelObj.Padding = PaddingMode.PKCS7;
  35.         }
  36.  
  37.         public void SetRijndaelObject(RijndaelManaged rijndaelObj)
  38.         {
  39.             _rijndaelObj = rijndaelObj;
  40.         }
  41.  
  42.         public RijndaelManaged GetRijndaelObject()
  43.         {
  44.             return _rijndaelObj;
  45.         }
  46.  
  47.         public AesData GetAesData()
  48.         {
  49.             string key = Encoding.ASCII.GetString(_rijndaelObj.Key);
  50.             string iv = Encoding.ASCII.GetString(_rijndaelObj.IV);
  51.             return new AesData(key, iv);
  52.         }
  53.  
  54.         public string EncryptData(string stringToEncrypt)
  55.         {
  56.             byte[] bytesToEncrypt = CryptationUtility.StringToBytes(stringToEncrypt);
  57.             string encryptedString;
  58.  
  59.             try
  60.             {
  61.                 // Generate encryptor from the existing key bytes and initialization vector.
  62.                 ICryptoTransform encryptor = _rijndaelObj.CreateEncryptor();
  63.  
  64.                 // Define memory stream which will be used to hold encrypted data.
  65.                 using (var msEncrypt = new MemoryStream())
  66.                 {
  67.  
  68.                     // Define cryptographic stream (always use Write mode for encryption).
  69.                     using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  70.                     {
  71.  
  72.                         //Define writer to write to stream
  73.                         using (var swEncrypt = new StreamWriter(csEncrypt))
  74.                         {
  75.  
  76.                             //Write to stream
  77.                             swEncrypt.Write(stringToEncrypt);
  78.  
  79.                             //Finish encrypting
  80.                             csEncrypt.FlushFinalBlock();
  81.  
  82.                             //Get encrypted data
  83.                             byte[] encryptedBytes = msEncrypt.ToArray();
  84.  
  85.                             //Convert to string
  86.                             encryptedString = CryptationUtility.ToUrlSafeBase64(encryptedBytes);
  87.                         }
  88.                     }
  89.                 }
  90.  
  91.                 return encryptedString;
  92.             }
  93.             catch (Exception e)
  94.             {
  95.                 throw new AesCryptationException("Error while encrypting", e);
  96.             }
  97.         }
  98.  
  99.         public string DecryptData(string dataToDecrypt)
  100.         {
  101.             //Convert from base64
  102.             byte[] bytesToDecrypt = CryptationUtility.FromUrlSafeBase64(dataToDecrypt);
  103.             string decryptedString;
  104.  
  105.             try
  106.             {
  107.                 // Generate decryptor from the existing key bytes and initialization vector.
  108.                 ICryptoTransform decryptor = _rijndaelObj.CreateDecryptor();
  109.  
  110.                 // Define memory stream which will be used to hold encrypted data.
  111.                 using (var msDecrypt = new MemoryStream(bytesToDecrypt))
  112.                 {
  113.                     // Define cryptographic stream (always use Write mode for encryption).
  114.                     using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  115.                     {
  116.                         //Define writer to write to stream
  117.                         using(var srDecrypt = new StreamReader(csDecrypt))
  118.                         {
  119.                             //Get encrypted data
  120.                             decryptedString = srDecrypt.ReadToEnd();
  121.                         }
  122.                     }
  123.                 }
  124.            
  125.                 return decryptedString;
  126.             }
  127.             catch (Exception e)
  128.             {
  129.  
  130.                 throw new AesCryptationException("Error while decrypting", e);
  131.             }
  132.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement