Talk:AES implementations
![]() | Cryptography: Computer science List‑class Low‑importance | ||||||||||||
|
GnuTLS
I'm opening discussion for the inclusion of GnuTLS as a C implementation of AES. It performs more or less as a drop in for OpenSSL. Quoted from the GnuTLS page: "GnuTLS is a secure communications library. It provides a C language application programming interface (API) to access security protocols such as IETF's transport layer security (TLS) protocols." I'm going to add the library, anyone feel free to provide counterpoint to it's inclusion though.
Tului (talk) 04:09, 11 June 2011 (UTC)
no pycrypt
there is no longer a functioning pycrypt module being posted on the link given. — Preceding unsigned comment added by 213.105.62.2 (talk) 09:04, 30 August 2011 (UTC)
Removing wrappers
I suggest removing all the libraries that merely wrap other implementations. These are not AES implementations in and of themselves, though they can be very useful for development. This includes M2Crypto, NCrypt, pycryptopp and probably more. — Preceding unsigned comment added by 79.171.96.132 (talk) 16:52, 19 October 2011 (UTC)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Security.Cryptography; using System.IO; namespace Crypto_Test {
public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void btnEncrypt_Click(object sender, EventArgs e) { this.txtResult.Text=AESEncrypter.Encrypt(this.txtSource.Text.Trim()); }
private void btnDecrypt_Click(object sender, EventArgs e) { this.txtResult.Text = AESEncrypter.Decrypt(this.txtSource.Text.Trim()); }
private void button1_Click(object sender, EventArgs e) { AESEncrypter.TestIt(); }
}
public class AESEncrypter { public static void TestIt() { try {
string original = "Here is some data to encrypt!";
// Create a new instance of the AesCryptoServiceProvider // class. This generates a new key and initialization // vector (IV). using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider()) {
// Encrypt the string to an array of bytes. byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
String strCipher = System.Text.Encoding.Unicode.GetString(encrypted); //kvr code
// Decrypt the bytes to a string. string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
byte[] byteArr = System.Text.Encoding.Unicode.GetBytes(strCipher); string strDeciphered = DecryptStringFromBytes_Aes(byteArr, myAes.Key, myAes.IV); //Display the original data and the decrypted data. Console.WriteLine("Original: {0}", original); Console.WriteLine("cipher as string: {0}", strCipher);//kvr Console.WriteLine("deciphered from cipher : {0}", strDeciphered);//kvr Console.WriteLine("Round Trip: {0}", roundtrip); }
} catch (Exception e) { Console.WriteLine("Error: {0}", e.Message); } }
public static string Encrypt(string plainText) { using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider()) { string strCipher=""; try { // Encrypt the string to an array of bytes and then convert it to string. byte[] key = GetKey("1234pass", "salt012345678", 32); byte[] IV = GetIV("1234pass", "salt012345678", 32); byte[] encrypted = EncryptStringToBytes_Aes(plainText, key, IV); strCipher = System.Text.Encoding.Unicode.GetString(encrypted); //kvr code } catch (Exception e) {
Console.WriteLine("Error: {0}", e.Message); } return strCipher;
} }
public static string Decrypt(string cipher) { using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider()) { string strPlain = ""; try { byte[] byteArr = System.Text.Encoding.Unicode.GetBytes(cipher); byte[] key = GetKey("1234pass", "salt012345678", myAes.KeySize); byte[] IV = GetIV("1234pass", "salt012345678", myAes.KeySize); strPlain = DecryptStringFromBytes_Aes(byteArr, key, IV); Aes aes= new AesManaged();
} catch (Exception e) {
Console.WriteLine("Error: {0}", e.Message); } return strPlain;
}
}
public static byte[] GetKey(string password, string saltvalue,int keySize) { //http://stackoverflow.com/questions/2503433/how-to-create-encryption-key-for-encryption-algorithms
byte[] salt = Encoding.ASCII.GetBytes(saltvalue); Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, salt); byte[] key=keyGenerator.GetBytes(keySize); return key; }
public static byte[] GetIV(string password, string saltvalue, int keySize) { //http://stackoverflow.com/questions/2503433/how-to-create-encryption-key-for-encryption-algorithms
byte[] salt = Encoding.ASCII.GetBytes(saltvalue); Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, salt); byte[] IV = keyGenerator.GetBytes(keySize); return IV; }
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV) { // Check arguments. if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("Key"); byte[] encrypted; // Create an AesCryptoServiceProvider object // with the specified key and IV. using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) { aesAlg.Key = Key; aesAlg.IV = IV;
// Create a decrytor to perform the stream transform. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
//Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray();
} } }
// Return the encrypted bytes from the memory stream. return encrypted;
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV) { // Check arguments. if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("Key");
// Declare the string used to hold // the decrypted text. string plaintext = null;
// Create an AesCryptoServiceProvider object // with the specified key and IV. using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider()) { aesAlg.Key = Key; aesAlg.IV = IV;
// Create a decrytor to perform the stream transform. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) {
// Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } }
}
return plaintext;
} }
}