patterncsharpMinor
AES256 Implementation
Viewed 0 times
implementationaes256stackoverflow
Problem
I coded a little implementation to encrypt and decrypt bytes with AES256 (ECB) in C#. I wanted to know if I can optimize/cleanup my code a bit more, or if it i fine like this:
```
using System;
using System.Security.Cryptography;
using System.Text;
namespace AES256
{
class Program
{
private static string getString(byte[] b)
{
return Encoding.UTF8.GetString(b);
}
static void Main(string[] args)
{
byte[] data = Encoding.UTF8.GetBytes("This is just a little message to test AES.");
byte[] key = { 7, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] enc = Encrypt(data, key);
byte[] dec = Decrypt(enc, key);
Console.WriteLine("Input : {0}", getString(data));
Console.WriteLine("Key : {0}", getString(key));
Console.WriteLine("KeySize : {0}", 256);
Console.WriteLine("Encrypted : {0}", getString(enc));
Console.WriteLine("Decrypted : {0}", getString(dec));
Console.ReadKey();
}
public static byte[] Encrypt(byte[] data, byte[] key)
{
using (AesCryptoServiceProvider csp = new AesCryptoServiceProvider())
{
csp.KeySize = 256;
csp.BlockSize = 128;
csp.Key = key;
csp.Padding = PaddingMode.PKCS7;
csp.Mode = CipherMode.ECB;
ICryptoTransform encrypter = csp.CreateEncryptor();
return encrypter.TransformFinalBlock(data, 0, data.Length);
}
}
private static byte[] Decrypt(byte[] data, byte[] key)
{
using (AesCryptoServiceProvider csp = new AesCryptoServiceProvider())
{
csp.KeySize = 256;
csp.BlockSize = 128;
csp.Key = key;
csp.Padding = PaddingMod
```
using System;
using System.Security.Cryptography;
using System.Text;
namespace AES256
{
class Program
{
private static string getString(byte[] b)
{
return Encoding.UTF8.GetString(b);
}
static void Main(string[] args)
{
byte[] data = Encoding.UTF8.GetBytes("This is just a little message to test AES.");
byte[] key = { 7, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] enc = Encrypt(data, key);
byte[] dec = Decrypt(enc, key);
Console.WriteLine("Input : {0}", getString(data));
Console.WriteLine("Key : {0}", getString(key));
Console.WriteLine("KeySize : {0}", 256);
Console.WriteLine("Encrypted : {0}", getString(enc));
Console.WriteLine("Decrypted : {0}", getString(dec));
Console.ReadKey();
}
public static byte[] Encrypt(byte[] data, byte[] key)
{
using (AesCryptoServiceProvider csp = new AesCryptoServiceProvider())
{
csp.KeySize = 256;
csp.BlockSize = 128;
csp.Key = key;
csp.Padding = PaddingMode.PKCS7;
csp.Mode = CipherMode.ECB;
ICryptoTransform encrypter = csp.CreateEncryptor();
return encrypter.TransformFinalBlock(data, 0, data.Length);
}
}
private static byte[] Decrypt(byte[] data, byte[] key)
{
using (AesCryptoServiceProvider csp = new AesCryptoServiceProvider())
{
csp.KeySize = 256;
csp.BlockSize = 128;
csp.Key = key;
csp.Padding = PaddingMod
Solution
The code is simple to understand and does what it needs to do. Just some remarks:
-
Your code contains duplicates. You could extract that code into a method:
-
You made the
- Whenever you use the ECB cipher mode, you must justify in written form why you use it. This is because this cipher mode can reveal patterns in your plain text.
- Converting the encrypted bytes to an UTF-8 string doesn't make sense. You should better print them in hex or base64.
-
Your code contains duplicates. You could extract that code into a method:
private AesCryptoServiceProvider CreateProvider(byte[] key) {
return new AesCryptoServiceProvider {
KeySize = 256,
BlockSize = 128,
Key = key,
Padding = PaddingMode.PKCS7,
Mode = CipherMode.ECB
};
}-
You made the
Encrypt method public, but Decode is private. I don't see a reason for this asymmetry.Code Snippets
private AesCryptoServiceProvider CreateProvider(byte[] key) {
return new AesCryptoServiceProvider {
KeySize = 256,
BlockSize = 128,
Key = key,
Padding = PaddingMode.PKCS7,
Mode = CipherMode.ECB
};
}Context
StackExchange Code Review Q#154017, answer score: 8
Revisions (0)
No revisions yet.