-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathAes.cs
123 lines (112 loc) · 4.83 KB
/
Aes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Security.Cryptography;
namespace Moserware.AesIllustrated
{
/// <summary>
/// Implements the Advanced Encryption Standard (AES) algorithm.
/// </summary>
/// <remarks>
/// See http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf for more info, or Google
/// "moserware stick figure guide advanced encryption standard".
/// </remarks>
public class Aes
{
// AES is just Rijndael with fewer options to tweak.
private readonly Rijndael _Rijndael;
public Aes()
{
_Rijndael = new Rijndael();
}
public Aes(byte[] key)
{
_Rijndael = new Rijndael(key);
}
/// <summary>
/// The cipher key in (must be 128, 192, or 256 bits)
/// </summary>
public byte[] Key
{
get { return _Rijndael.Key; }
set
{
if (!IsValidAesKey(value))
{
throw new ArgumentException("Invalid key size (must be 128, 192, or 256 bits)", "key");
}
_Rijndael.Key = value;
}
}
/// <summary>
/// Encrypts a block of data.
/// </summary>
/// <param name="input">The plaintext block.</param>
/// <returns>The resulting ciphertext.</returns>
public byte[] Encrypt(byte[] input)
{
return _Rijndael.Encrypt(input);
}
/// <summary>
/// Encrypts a block of data.
/// </summary>
/// <param name="input">The plaintext block.</param>
/// <param name="key">The key to use.</param>
/// <returns>The resulting ciphertext.</returns>
public static byte[] Encrypt(byte[] input, byte[] key)
{
return (new Aes(key)).Encrypt(input);
}
/// <summary>
/// Creates an encryption transform that can be used by the .NET cryptography APIs.
/// </summary>
/// <param name="cipherMode">The mode if the cipher to use.</param>
/// <param name="initializationVector">An initialization vector (if used by the <paramref name="cipherMode"/>, or <see langword="null"/> if not used).</param>
/// <param name="feedbackSizeInBits">Size of the feedback register in bits for feedback modes.</param>
/// <param name="paddingMode">The style of padding to apply to the last block.</param>
/// <returns>An encryption transform that can be used by the .NET cryptography APIs.</returns>
public ICryptoTransform CreateEncryptor(CipherMode cipherMode, byte[] initializationVector,
int feedbackSizeInBits, PaddingMode paddingMode)
{
return _Rijndael.CreateEncryptor(cipherMode, initializationVector, feedbackSizeInBits, paddingMode);
}
/// <summary>
/// Decrypts a block of data.
/// </summary>
/// <param name="input">The ciphertext block.</param>
/// <returns>The resulting plaintext.</returns>
public byte[] Decrypt(byte[] input)
{
return _Rijndael.Decrypt(input);
}
/// <summary>
/// Decrypts a block of data.
/// </summary>
/// <param name="input">The ciphertext block.</param>
/// <param name="key">The key to use.</param>
/// <returns>The resulting plaintext.</returns>
public static byte[] Decrypt(byte[] input, byte[] key)
{
return (new Aes(key)).Decrypt(input);
}
/// <summary>
/// Creates a decryption transform that can be used by the .NET cryptography APIs.
/// </summary>
/// <param name="cipherMode">The mode if the cipher to use.</param>
/// <param name="initializationVector">An initialization vector (if used by the <paramref name="cipherMode"/>, or <see langword="null"/> if not used).</param>
/// <param name="feedbackSizeInBits">Size of the feedback register in bits for feedback modes.</param>
/// <param name="paddingMode">The style of padding to apply to the last block.</param>
/// <returns>An decryption transform that can be used by the .NET cryptography APIs.</returns>
public ICryptoTransform CreateDecryptor(CipherMode cipherMode, byte[] initializationVector,
int feedbackSizeInBits, PaddingMode paddingMode)
{
return _Rijndael.CreateDecryptor(cipherMode, initializationVector, feedbackSizeInBits, paddingMode);
}
private static bool IsValidAesKey(byte[] key)
{
// AES only allows 3 key sizes
int keySizeInBits = key.Length*Constants.BitsPerByte;
return (keySizeInBits == 128)
|| (keySizeInBits == 192)
|| (keySizeInBits == 256);
}
}
}