RH-00
New Member
Posts: 4
|
Post by RH-00 on Jun 25, 2009 22:41:48 GMT -5
OK, I'm writing a simple encryption program, and I'm using a fixed key to make things a little bit easier. When I set the key in C# (what it was originally written in), I get no fuss. However, when I try to set the key of the RijndaelManaged instance with my own array of bytes (I'm using a 256-bit key), it tells me the key (in the class) is read-only. What gives? Any way to fix this?
|
|
|
Post by Kelly Ethridge on Jun 26, 2009 1:07:37 GMT -5
Hello,
Can you post your code demonstrating this problem?
Thank you, Kelly
|
|
RH-00
New Member
Posts: 4
|
Post by RH-00 on Jun 26, 2009 11:22:26 GMT -5
With gusto:
Function AES_Enc(file() As Byte) As Byte() Dim encryptor As New RijndaelManaged Dim key(32) As Byte key(0) = &H69 key(1) = &H42 key(2) = &H6 key(3) = &H66 key(4) = &H13 key(5) = &H37 key(6) = &HDA key(7) = &HDE key(8) = &HB0 key(9) = &HB key(10) = &HD0 key(11) = &HD key(12) = &HDE key(13) = &HAD key(14) = &HBA key(15) = &HBE key(16) = &HBA key(17) = &HDC key(18) = &HF key(19) = &HFE key(20) = &HEE key(21) = &H1E key(22) = &HAD key(23) = &HF0 key(24) = &H7 key(25) = &HBD key(26) = &HDB key(27) = &HAD key(28) = &HFF key(29) = &HFF key(30) = &HFF key(31) = &H0 encryptor.KeySize = 256 encryptor.Padding = PKCS7 Dim i As Single For i = 0 To UBound(key) Let encryptor.key(i) = key(i) Next i For i = 0 To (UBound(key) / 2) Set encryptor.IV(i) = key(i) Next i Dim transform As ICryptoTransform Set transform = encryptor.CreateEncryptor AES_Enc = transform.TransformFinalBlock(file, 0, Len(file)) End Function
Also, I've tried using "=," that didn't work, neither did using "Set."
|
|
|
Post by Kelly Ethridge on Jun 26, 2009 11:55:27 GMT -5
Hello,
Thanks for posting your code. The problem is with how VB6 handles assigned arrays to object properties. You need to declare your array differently.
Change
Dim key(32) As Byte to
Dim key() As Byte ReDim Key(32)
and it should work just fine.
Kelly
|
|
|
Post by Kelly Ethridge on Jun 26, 2009 12:30:04 GMT -5
Hello again,
I've just noticed how you assign the key and IV properties:
Dim i As Single For i = 0 To UBound(key) Let encryptor.key(i) = key(i) Next i
For i = 0 To (UBound(key) / 2) Set encryptor.IV(i) = key(i) Next i
VB6 can't do direct element assignments through a property that returns an array. You'll need to change these lines to:
Dim i As Single encryptor.key = key
encryptor.IV = key
I see you were trying to use only half of the key for your IV values. You'll have to set the last half of the key array elements to 0 and then assign it to the IV to have the same functionality as you were attempting.
Kelly
|
|
RH-00
New Member
Posts: 4
|
Post by RH-00 on Jun 26, 2009 12:46:24 GMT -5
Error: Cannot assign to array on "encryptor.key = key". I've tried that before.
|
|
|
Post by Kelly Ethridge on Jun 26, 2009 13:03:42 GMT -5
Did you change your array declaration as I had noted?
Kelly
|
|
RH-00
New Member
Posts: 4
|
Post by RH-00 on Jun 26, 2009 14:32:32 GMT -5
Got it, I just had to fiddle with my declarations a bit more. Thanks for the advice, homes!
|
|