Can I use the Aes Encryption technique to encrypt/decrypt a user defined type. In this link the encryption is done for a simple string. But Suppose I want to encrypt my below user defined type.
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
How it can be done.
Or is there any other alternative way to do it.
P.S - I have tried following after Jon's answer but while I am getting an exception "Length of the data to decrypt is invalid" while decrypting the data. I am using following code to encrypt and decrypt the data.
[Serializable]
public class Person:ISerializable
{
public string FirstName { get; set; }
public string LastName { get; set; }
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("FirstName", FirstName);
info.AddValue("LastName", LastName);
}
public Person()
{
}
public Person(SerializationInfo info, StreamingContext ctxt)
{
FirstName = (string)info.GetValue("FirstName", typeof(string));
LastName = (string)info.GetValue("LastName", typeof(string));
}
}
static void Main(string[] args)
{
Person per = new Person() { FirstName = "Vikram", LastName = "Chaudhary" };
using (AesManaged aes = new AesManaged())
{
byte[] encrypted = EncryptStringToBytes_Aes(per, aes.Key, aes.IV);
byte[] decrypted = DecryptStringFromBytes_Aes(encrypted, aes.Key, aes.IV);
}
}
static byte[] EncryptStringToBytes_Aes(Person plainText, byte[] Key, byte[] IV)
{
MemoryStream stream = new MemoryStream();
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(stream, plainText);
byte[] encrypted;
// Create an AesManaged object
// with the specified key and IV.
using (AesManaged aesAlg = new AesManaged())
{
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))
{
csEncrypt.Write(stream.ToArray(), 0, stream.ToArray().Length);
csEncrypt.Close();
}
}
}
// Return the encrypted bytes from the memory stream.
byte[] encryptedData = stream.ToArray();
return encryptedData;
}
static byte[] DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
byte[] decryptedData;
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
Person decryptedObject = null;
// Create an AesManaged object
// with the specified key and IV.
using (AesManaged aesAlg = new AesManaged())
{
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())
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
{
csDecrypt.Write(cipherText, 0, cipherText.Length);
}
decryptedData = msDecrypt.ToArray();
}
}
return decryptedData;
}
Please let me know what I am doing wrong.
You're currently combining two separable concepts:
Person
and a stream of binary data (or byte[]
)?byte[]
)?Those two concepts should be separated as far as they possibly can be in your code and in your mind.
Work out how you want to serialize the object - native .NET binary serialization? Some text format (JSON, XML, something else)? A 3rd party binary serialization format (e.g. Protocol Buffers, Thrift)? You can build and test this without any encryption being involved.
Work out how you want to encrypt "some data". You can build and test this without any serialization being involved, just using hard-coded data.
See more on this question at Stackoverflow