Error 15 The type or namespace name 'Aes' could not be found (are you missing a using directive or an assembly reference?)
Error 18 The name 'Aes' does not exist in the current context
I got this problem when i downgraded the application framework from 4.0 to 2.0
I saw there were missing System.Core dll which was contains definition of Aes.
My code was
using (Aes encryptor = new Aes.Create())
{
//..
}
I tried to add references but unable to do that as the same system.Core was not available for .net framework 2.0
I was in search of alternative of Aes.Create() in framework 2.0 and then i found RijndaelManaged available inside
System.Security.Cryptography.
I used them and now every thing seems fine for me with this pice of code
using (RijndaelManaged encryptor = new RijndaelManaged())
{
//...
}
Full C# code to encrypt and decrypt in framework 2.0
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Encrypt_Decrypt_in_framework_2._0
{
public class Class1
{
private static byte[] iv = new byte[] { 60, 0x73, 0x44, 0x1a, 0x45, 0xb2, 200, 0xdb };
private static byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x10, 0x11, 0x12, 0x13, 20, 0x15, 0x16, 0x17, 0x18 };
public static string DecryptString(string cipherText, string pkey)
{
try
{
cipherText = cipherText.Replace("$$$$$$", "+");
cipherText = cipherText.Replace("@@@@@@", "/");
string EncryptionKey = pkey;
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (RijndaelManaged encryptor = new RijndaelManaged())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
catch
{
return "";
}
}
public static string EncryptString(string clearText, string pkey)
{
try
{
string EncryptionKey = pkey;
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (RijndaelManaged encryptor = new RijndaelManaged())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
clearText = clearText.Replace("+", "$$$$$$");
clearText = clearText.Replace("/", "@@@@@@");
return clearText;
}
catch
{
return "";
}
}
public static void Main()
{
string plaintxt = "my name is raj";
string key = "raj";
string ciphertext = Class1.EncryptString(plaintxt, key);
Console.WriteLine(ciphertext);
Console.WriteLine(Class1.DecryptString(ciphertext, key));
Console.Read();
}
}
}
Error 18 The name 'Aes' does not exist in the current context
I got this problem when i downgraded the application framework from 4.0 to 2.0
I saw there were missing System.Core dll which was contains definition of Aes.
My code was
using (Aes encryptor = new Aes.Create())
{
//..
}
I tried to add references but unable to do that as the same system.Core was not available for .net framework 2.0
I was in search of alternative of Aes.Create() in framework 2.0 and then i found RijndaelManaged available inside
System.Security.Cryptography.
I used them and now every thing seems fine for me with this pice of code
using (RijndaelManaged encryptor = new RijndaelManaged())
{
//...
}
Full C# code to encrypt and decrypt in framework 2.0
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Encrypt_Decrypt_in_framework_2._0
{
public class Class1
{
private static byte[] iv = new byte[] { 60, 0x73, 0x44, 0x1a, 0x45, 0xb2, 200, 0xdb };
private static byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0x10, 0x11, 0x12, 0x13, 20, 0x15, 0x16, 0x17, 0x18 };
public static string DecryptString(string cipherText, string pkey)
{
try
{
cipherText = cipherText.Replace("$$$$$$", "+");
cipherText = cipherText.Replace("@@@@@@", "/");
string EncryptionKey = pkey;
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (RijndaelManaged encryptor = new RijndaelManaged())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
catch
{
return "";
}
}
public static string EncryptString(string clearText, string pkey)
{
try
{
string EncryptionKey = pkey;
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (RijndaelManaged encryptor = new RijndaelManaged())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
clearText = clearText.Replace("+", "$$$$$$");
clearText = clearText.Replace("/", "@@@@@@");
return clearText;
}
catch
{
return "";
}
}
public static void Main()
{
string plaintxt = "my name is raj";
string key = "raj";
string ciphertext = Class1.EncryptString(plaintxt, key);
Console.WriteLine(ciphertext);
Console.WriteLine(Class1.DecryptString(ciphertext, key));
Console.Read();
}
}
}