Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi,

I haven't been using encryption much so this is probably a basic question. I'm writing a small program to encrypt a file using username and password information from the user.

Currently I've created an instance of AesManaged which is always instantiated the same way, regardless if I'm encrypting or decrypting the data.

The code is as follows:
C#
private static System.Security.Cryptography.Aes InitAes(string username, string password) {
   System.Security.Cryptography.Aes aes = new System.Security.Cryptography.AesManaged();

   aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
   aes.KeySize = 128;
   aes.Key = System.Text.Encoding.Unicode.GetBytes(password);
   aes.IV = System.Text.Encoding.Unicode.GetBytes(username);

   return aes;
}

As you can see I've simply used the password as key and the username as IV.

Now, I've understood that in this situation the preferred way is to generate both key and IV with Rfc2898DeriveBytes.

The question I'm asking: Is that correct? And if it is, how should I use Rfc2898DeriveBytes? If I use random salt in encryption, how should I decrypt the file?
Posted
Comments
Kenneth Haugland 4-Sep-12 16:23pm    
Just out of curiosity why did you choose AesManaged? seems to be a lot of encryption algorithms to choose from:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.aspx
Not saying its wriong, just wonded :)
Wendelius 4-Sep-12 16:38pm    
Good question, I was looking for quite strong but easy to implement encryption. First I thought about Rijndael, but then I read somewhere from MSDN that Aes should be used instead. At that point Aes seemed sufficient, but I'm definitely open to suggestions :)
Kenneth Haugland 4-Sep-12 16:49pm    
Seems that this guy agrees with your choise in any case:
http://msdn.microsoft.com/en-us/magazine/cc164055.aspx
Wendelius 4-Sep-12 16:53pm    
That's a good read, thanks!
Kenneth Haugland 4-Sep-12 17:01pm    
It also seems that the Aes uses Rijndael algorithm, at least to judge by the reference in the bottom. I just got interested and had to find out, this stuff would be good to know in any case :)

1 solution

Not sure if this is the best approach for the question, but I came up with the following solution.

  • Define a new Rfc2898DeriveBytes
  • Define the password as the password for Rfc2898DeriveBytes (well this was surpising)
  • Define the username as salt
  • Use the first 16 bytes (128 bit) as the Key for Aes
  • Use the next 16 bytes as the initialization vector


So the code looks currently like this:
C#
private static System.Security.Cryptography.Aes InitAes(string username, string password) {
   System.Security.Cryptography.Aes aes = new System.Security.Cryptography.AesManaged();
   System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 
   = new System.Security.Cryptography.Rfc2898DeriveBytes(password, 
                                                         System.Text.Encoding.Unicode.GetBytes(username));

   aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
   aes.KeySize = 128;
   aes.Key = rfc2898.GetBytes(16);
   aes.IV = rfc2898.GetBytes(16);

   return aes;
}


If anybody has comments or enhancement ideas, feel free to add them as comments or solutions.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900