Advertisement
robsoft

simple AES code, C#

Mar 16th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. From memory, you pass the string to be encrypted/decrypted and the passphrase as a single string, separated by a comma. I think that was because this was also to be used in a DLL for use with Delphi etc, but anyway, the point is that this was the simple means of setting up and tearing down AES in C#. Apologies for lack of comments, easier to just strip than to have to sanitise too much for the web.
  2.  
  3.  
  4. using System;
  5. using System.Windows.Forms;
  6. using System.Runtime.InteropServices;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9.  
  10. namespace BlahLib
  11. {
  12.     public interface IBlahLib
  13.     {
  14.         void TestMethod();
  15.  
  16.         string Encrypt(string original, string key);
  17.         string Decrypt(string encrypted, string key);
  18.         string GetKey(string passphrase);
  19.     }
  20.  
  21.     [ClassInterface(ClassInterfaceType.None)]
  22.     public class BlahLib : IBlahLib
  23.     {
  24.         public BlahLib()
  25.         {
  26.         }
  27.        
  28.         public void TestMethod()
  29.         {
  30.             MessageBox.Show("Hello from the BlahLib class");
  31.         }
  32.  
  33.         public string GetKey(string passphrase)
  34.         {
  35.             RijndaelManaged aesEncryption = new RijndaelManaged();            
  36.             aesEncryption.KeySize = 256;            
  37.             aesEncryption.BlockSize = 128;            
  38.             aesEncryption.Mode = CipherMode.ECB;            
  39.             aesEncryption.Padding = PaddingMode.Zeros;
  40.             aesEncryption.GenerateIV();            
  41.  
  42.             string ivStr = Convert.ToBase64String(aesEncryption.IV);
  43.             byte[] keyText = ASCIIEncoding.UTF8.GetBytes(passphrase);
  44.             string keyStr = Convert.ToBase64String(keyText);  
  45.             string completeKey = ivStr + "," + keyStr;    
  46.             return Convert.ToBase64String(ASCIIEncoding.UTF8.GetBytes(completeKey));        
  47.         }
  48.  
  49.         public string Encrypt(string original, string key)
  50.         {
  51.             RijndaelManaged aesEncryption = new RijndaelManaged();
  52.             aesEncryption.KeySize = 256;
  53.             aesEncryption.BlockSize = 128;
  54.             aesEncryption.Mode = CipherMode.ECB;
  55.             aesEncryption.Padding = PaddingMode.Zeros;
  56.             aesEncryption.IV =
  57.                 Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(key)).Split(',')[0]);
  58.             aesEncryption.Key =
  59.                 Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(key)).Split(',')[1]);
  60.  
  61.             byte[] plainText = ASCIIEncoding.UTF8.GetBytes(original);
  62.             ICryptoTransform crypto = aesEncryption.CreateEncryptor();
  63.  
  64.             byte[] cipherText = crypto.TransformFinalBlock(plainText, 0, plainText.Length);
  65.             return Convert.ToBase64String(cipherText);
  66.         }
  67.  
  68.         public string Decrypt(string encrypted, string key)
  69.         {
  70.             RijndaelManaged aesEncryption = new RijndaelManaged();
  71.             aesEncryption.KeySize = 256;
  72.             aesEncryption.BlockSize = 128;
  73.             aesEncryption.Mode = CipherMode.ECB;
  74.             aesEncryption.Padding = PaddingMode.Zeros;
  75.             aesEncryption.IV =
  76.                 Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(key)).Split(',')[0]);
  77.             aesEncryption.Key =
  78.                 Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(key)).Split(',')[1]);
  79.  
  80.             ICryptoTransform decrypto = aesEncryption.CreateDecryptor();
  81.  
  82.             byte[] encryptedText = Convert.FromBase64CharArray(encrypted.ToCharArray(), 0, encrypted.Length);
  83.             return ASCIIEncoding.UTF8.GetString(decrypto.TransformFinalBlock(encryptedText, 0, encryptedText.Length));
  84.         }
  85.  
  86.        
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement