C#

AES Encryption-Decryption Class in C#

Let’s see a simple sealed class(used singleton pattern) to encrypt and decrypt Strings in C#:

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
/*Simple AES Encryption - Decryption Class
Copyright (C) 2012  George Karpouzas
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
 
namespace Encryption.AES
{
    /// <summary>
    /// prevent the inheritance of this class
    /// </summary>
    public sealed class libAES
    {
        private static readonly libAES _instance = new libAES();
 
        /// <summary>
        /// prevent instantiation from other classes
        /// </summary>
        private libAES() { }
 
        /// <summary>
        /// get instance of the class
        /// </summary>
        /// <returns></returns>
        public static libAES Instance
        {
            get
            {
                return _instance;
            }
        }
 
        /// <summary>
        /// Decrypt string using AES 128
        /// </summary>
        /// <param name="cipheredtext"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public String Decrypt(String cipheredtext, String key)
        {
            byte[] keybytes = Encoding.UTF8.GetBytes(key);
            byte[] cipheredData = Convert.FromBase64String(cipheredtext);
 
            RijndaelManaged aes = new RijndaelManaged();
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.None;
 
            //16 ascii characters for IV
            byte[] IVbytes = Encoding.ASCII.GetBytes("myVerySecretIVKe");
 
            ICryptoTransform decryptor = aes.CreateDecryptor(keybytes, IVbytes);
            System.IO.MemoryStream ms = new System.IO.MemoryStream(cipheredData);
            CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
            StreamReader creader = new StreamReader(cs, Encoding.UTF8);
 
            String Base64 = creader.ReadToEnd();
 
            ms.Close();
            cs.Close();
 
            return Encoding.UTF8.GetString(Convert.FromBase64String(Base64));
        }
 
        /// <summary>
        /// Encrypt string using AES 128
        /// </summary>
        /// <param name="plaintext"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public String Encrypt(String plaintext, String key)
        {
            byte[] keybytes = Encoding.UTF8.GetBytes(key);
 
            RijndaelManaged aes = new RijndaelManaged();
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.None;
            byte[] IVbytes = Encoding.ASCII.GetBytes("myVerySecretIVKe");
 
            ICryptoTransform encryptor = aes.CreateEncryptor(keybytes, IVbytes);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
 
            byte[] plainBytes = Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes(plaintext)));
 
            cs.Write(plainBytes, 0, plainBytes.Length);
 
            cs.FlushFinalBlock();
 
            byte[] cipherBytes = ms.ToArray();
 
            ms.Close();
            cs.Close();
 
            return Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
        }
    }
}

To use it:

//decryption
String data = libEncryption.Instance.Decrypt("mycipheredtextdata", "my_key");
 
//encryption
String data = libEncryption.Instance.Encrypt("myplaintextdata", "my_key");