Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello all,

 

I'm trying to automatically generate a key value for the RijndaelManaged encryption object. The idea is to generate this key based off of an algorithm that processes a string (i.e. password) that is entered by the user. This is to ensure that the key does not reside somewhere on the machine but is instead generated furing runtime. I have successfully done it for the IV but get the error (Specified key is not a valid size for this algorithm.) when I run this code:

 

Private Function GenerateBTKey(ByVal thestring As String) As String

 

Dim MyByte(15) As Byte

MyByte = System.Text.Encoding.ASCII.GetBytes(thestring.Length)

Dim sb As New System.Text.StringBuilder(25)

Dim i As Integer = 0

crpSym.Key = MyByte

While i < crpSym.IV.Length

sb.Append(crpSym.Key(i))

sb.Append(",")

i = i + 1

End While

 

Return sb.ToString()

End Function

 

The stringbuilder routine is to view the key after it is created via a console. The string I am passing is "this is a test".

 

I'm pretty sure it has to do with the fact that the key value is not of the right size (i.e. 64 bytes I think). The question is how do I modify the aforementioned code to get it to that size?

 

thanx in advance,

 

inzo

he who forgets will be destined to remember... (E.Vedder)
  • Administrators
Posted (edited)

Not tried this but possibly you could pad the string out to the required size and then use it's bytes as the key. Also do you mean to use thestring.length as a parameter to GetBytes - wouldn't thestring be a better choice of parameter.

 

At the top of the file if you add the line

option explicit

does it generate any errors? If so they probably need fixing first. Also you may want to try modifying the start of your routine to something like

       Dim MyByte(15) As Byte
       thestring = thestring.PadRight(64)
       MyByte = System.Text.Encoding.ASCII.GetBytes(thestring)

but like I said above I haven't had the chance to test this and see if it works...

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Hey Pdamp...

 

Thanx for the response. I tried the padding of the string before and again now - still not working. I do have option explicit on and now errors are occuring.

 

Good note on the thestring.length - I changed it before and forgot to change it back.

 

still searching.

 

thanx,

 

inzo

he who forgets will be destined to remember... (E.Vedder)
Posted

Got It!

 

Basically I'm too fatigued!

 

There was a little line in there during the loop that was causing the error - I was refencing the length of the IV from the function I copied rather than the Key value of my crypto stream.

 

That worked and it fixed everything.

 

thanx in advance.

 

inzo

he who forgets will be destined to remember... (E.Vedder)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...