0

I am trying to use AES encryption with encryption in one program and decryption in another. I have working code that encrypts the file but I need to extract the key to use in the other program to decrypt.

When I do this:

string key = String.Empty;
byte[] aesKey;
using (var aes = Aes.Create())
{
    aesKey = aes.Key;
    key = Convert.ToBase64String(aes.Key);
}

and use aesKey in my encrypt it works. When I try to convert from Base64 string to Byte[] it doesn't seem to work, It doesn't end up with the same bytes. How do you convert?

string sKey = "LvtZELDrB394hbSOi3SurLWAvC8adNpZiJmQDJHdfJU=";
aesKey = System.Text.Encoding.UTF8.GetBytes(sKey);
1
  • 1
    You use Convert.FromBase64String... always look for an inverse operation which actually sounds like it's the inverse. You're not wanting the UTF-8-encoded form of a string, so don't use Encoding.UTF8...
    – Jon Skeet
    Commented Apr 8, 2014 at 15:54

1 Answer 1

2

There is a corresponding From method that you should use.

byte[] aesKey = Convert.FromBase64String(sKey);

Not the answer you're looking for? Browse other questions tagged or ask your own question.