Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, March 16, 2020 3:51 AM
I have the following code
byte[] key = { }; //Encryption Key
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray;
try
{
key = Convert.FromBase64String("12345678");
DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
inputByteArray = Encoding.UTF8.GetBytes("testwatermnessage88888");
MemoryStream Objmst = new MemoryStream();
CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateEncryptor(key, IV), CryptoStreamMode.Write);
Objcs.Write(inputByteArray, 0, inputByteArray.Length);
Objcs.FlushFinalBlock();
var encryptedstr = Convert.ToBase64String(Objmst.ToArray());//encrypted string
}
catch (Exception ex)
{
throw ex;
}
.I want to increase the length of the key to 128 or 256 but it gives an error string is not the correct length .
How can i give a key of length 256 characters in the string ?
thanks
All replies (4)
Monday, March 16, 2020 11:54 AM âś…Answered
There are a couple of considerations when using AES:
- Key size in bits must be either 128, 192 or 256 (which gives you either 16,24 or 32 bytes) - all other key array sizes will result in the exception you describe.
- IV size in bits must be equal to cipher block size - by default this value is 128 bit, therefore your IV array must be 16 bytes long
I will not argue validity of converting arbitrary strings to Base64 representation for use as AES keys because I don't know enough of your use case, but given you've got long enough string, you can ensure you don't overrun the key size with the following technique:
byte[] key = Convert.FromBase64String("your-super-long-string-1234567890123456789012345678901234567890123456789012345678901234").Take(16).ToArray();// assume 128 bit key size
byte[] IV = Convert.FromBase64String("your-other-super-long-string-1234567890123456789012").Take(16).ToArray(); // assume 128 bit block size
However just want to point out, the when you create an instance of AesCryptoServiceProvider a random Key and IV are already generated for you, so you don't necessarily have to come up with these yourself:
byte[] inputByteArray;
try
{
AesCryptoServiceProvider ObjAES = new AesCryptoServiceProvider(); // key and IV will be generated for you
inputByteArray = Encoding.UTF8.GetBytes("testwatermnessage88888");
MemoryStream Objmst = new MemoryStream();
Console.WriteLine(Convert.ToBase64String(ObjAES.Key)); // store the value somewhere for decryption use
Console.WriteLine(Convert.ToBase64String(ObjAES.IV)); // store the value somewhere for decryption use
CryptoStream Objcs = new CryptoStream(Objmst, ObjAES.CreateEncryptor(), CryptoStreamMode.Write); // notice, no parameters to .CreateEncryptor()
Objcs.Write(inputByteArray, 0, inputByteArray.Length);
Objcs.FlushFinalBlock();
return Convert.ToBase64String(Objmst.ToArray());//encrypted string
}
catch (Exception ex)
{
throw ex;
}
this way you will guarantee correct lengths
Monday, March 16, 2020 5:44 AM
DESCryptoServiceProvider supports 64 bit keys and is considered obsolete encryption. If you want longer keys (128-256 bits) - you can opt for AesCryptoServiceProvider instead.
The code should be mostly unchanged, just replace the provider and supply longer key.
Monday, March 16, 2020 8:50 AM
I have done that gives same error , even followed the example in the link.
it errors on the following
key = "1234567890123456789012345678901234567890123456789012345678901234"
Encoding.UTF32.GetBytes(key) this gives an error key is not a valid length
Monday, March 16, 2020 9:51 AM
Hi, robby32,
About Base64:
Base64 uses 4 ASCII characters to encode 24-bits (3 bytes) of data.
That means 64 chars in base string will be 48 bytes while "AesCryptoServiceProvider" requires bytes of 32 length.
For 256 bits (32 bytes), you need 256/6 = 42.66 chars. That's rounded to 43 char.
However, base64 requires it divisible by 4 so that you should add one "=".
Solution:
string keyString = "1234567890123456789012345678901234567890123=";
byte[] key = Convert.FromBase64String(keyString); // the length will be 32
Hope this can help you.
Best regards,
Sean