Encrypting a String with AES?

M

mfunkmann

Hi there,

I want to save an object in a file, at first I have toserialize it,
then I have a string. This string should be encrypted before it is
saved on the hdd.

Are there any usable encryption methods? Preferable AES but anything
lower would do it..

kind regards

Matthias
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

I want to save an object in a file, at first I have toserialize it,
then I have a string. This string should be encrypted before it is
saved on the hdd.

Are there any usable encryption methods? Preferable AES but anything
lower would do it..

System.SecurityCryptography namespace in the framework has
excellent encryption support including AES.

Demo code:

using System;
using System.Text;
using System.Security.Cryptography;

class MainClass
{
public static void Main(string[] args)
{
Encoding utf = new UTF8Encoding();
Rijndael aes = new RijndaelManaged();
byte[] key = utf.GetBytes("hemmeligabcdefgh12345678");
byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16 };
ICryptoTransform encrypt = aes.CreateEncryptor(key, iv);
String plain1 = "Dette er en lille test";
byte[] cipher =
encrypt.TransformFinalBlock(utf.GetBytes(plain1), 0,
utf.GetByteCount(plain1));
for(int i = 0; i < cipher.Length; i++)
{
Console.WriteLine(cipher);
}
ICryptoTransform decrypt = aes.CreateDecryptor(key, iv);
String plain2 =
utf.GetString(decrypt.TransformFinalBlock(cipher, 0, cipher.Length));
Console.WriteLine(plain2);
}
}

Don't worry about the text - it is in my native language not english.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top