Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- using System;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Security.Cryptography;
- using System.Text;
- namespace BlahLib
- {
- public interface IBlahLib
- {
- void TestMethod();
- string Encrypt(string original, string key);
- string Decrypt(string encrypted, string key);
- string GetKey(string passphrase);
- }
- [ClassInterface(ClassInterfaceType.None)]
- public class BlahLib : IBlahLib
- {
- public BlahLib()
- {
- }
- public void TestMethod()
- {
- MessageBox.Show("Hello from the BlahLib class");
- }
- public string GetKey(string passphrase)
- {
- RijndaelManaged aesEncryption = new RijndaelManaged();
- aesEncryption.KeySize = 256;
- aesEncryption.BlockSize = 128;
- aesEncryption.Mode = CipherMode.ECB;
- aesEncryption.Padding = PaddingMode.Zeros;
- aesEncryption.GenerateIV();
- string ivStr = Convert.ToBase64String(aesEncryption.IV);
- byte[] keyText = ASCIIEncoding.UTF8.GetBytes(passphrase);
- string keyStr = Convert.ToBase64String(keyText);
- string completeKey = ivStr + "," + keyStr;
- return Convert.ToBase64String(ASCIIEncoding.UTF8.GetBytes(completeKey));
- }
- public string Encrypt(string original, string key)
- {
- RijndaelManaged aesEncryption = new RijndaelManaged();
- aesEncryption.KeySize = 256;
- aesEncryption.BlockSize = 128;
- aesEncryption.Mode = CipherMode.ECB;
- aesEncryption.Padding = PaddingMode.Zeros;
- aesEncryption.IV =
- Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(key)).Split(',')[0]);
- aesEncryption.Key =
- Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(key)).Split(',')[1]);
- byte[] plainText = ASCIIEncoding.UTF8.GetBytes(original);
- ICryptoTransform crypto = aesEncryption.CreateEncryptor();
- byte[] cipherText = crypto.TransformFinalBlock(plainText, 0, plainText.Length);
- return Convert.ToBase64String(cipherText);
- }
- public string Decrypt(string encrypted, string key)
- {
- RijndaelManaged aesEncryption = new RijndaelManaged();
- aesEncryption.KeySize = 256;
- aesEncryption.BlockSize = 128;
- aesEncryption.Mode = CipherMode.ECB;
- aesEncryption.Padding = PaddingMode.Zeros;
- aesEncryption.IV =
- Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(key)).Split(',')[0]);
- aesEncryption.Key =
- Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(key)).Split(',')[1]);
- ICryptoTransform decrypto = aesEncryption.CreateDecryptor();
- byte[] encryptedText = Convert.FromBase64CharArray(encrypted.ToCharArray(), 0, encrypted.Length);
- return ASCIIEncoding.UTF8.GetString(decrypto.TransformFinalBlock(encryptedText, 0, encryptedText.Length));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement