0

I am currently working on a Encryption/Decryption exercise. Main functionality is done however it was done in console. Now I will create the application in win form.

My Encryption/Decryption methods take byte arrays as arguments. I used DES as per program specifications. I was population the byte arrays like this

//were mk is a string
byte[] mkBytes = System.Text.UTF8Encoding.UTF8.GetBytes(mk); 

However CreateDecryptor method was giving "Specified key is not a valid size for this algorithm" error. Therefore to go around the problem I was forced to do this, hard code the whole bit array and worked fine.

byte[] masterKey_bytearr = { 0x12, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x23 };

But now I face the problem again since the user will be inputting the string in a textbox and cannot hard code them myself. How can I get the string from textbox and assign it to a byte[] suitable for DES CreateDecryptor method?

villecoder
  • 13,323
  • 2
  • 33
  • 52
user2307236
  • 665
  • 4
  • 19
  • 44
  • Not a duplicate - the question is about how to use `GetBytes` to get a byte array of a valid size that will be accepted by `CreateDecryptor` – stuartd Dec 27 '13 at 19:40
  • There is an [overload to `Encoding.GetBytes`](http://msdn.microsoft.com/en-us/library/ydktsz9z(v=vs.110).aspx) that allows you to specify how many bytes you want - so `byte[] mkBytes = UTF8Encoding.UTF8.GetBytes(mk.ToCharArray(), 0, 8);` – stuartd Dec 27 '13 at 19:41

0 Answers0