By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,045 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,045 IT Pros & Developers. It's quick & easy.

Help with "AES Encryption Algorithm for VBA and VBScript"

zmbd
Expert Mod 5K+
P: 5,397
Re: AES Encryption Algorithm for VBA and VBScript.

Hello,
Trying to step thru this code...

After reading thru the code, I tried to compile it

The following errors occur in the VBA module:
- "Set oFile1 = oFS.OpenTextFile(sFile, ForReading)" I get the error "ForReading" isn't a defined variable.
- "expandedKey = expandKey(aesKey, sbox, rcon)" as an undefined sub/function.

The first error I was able to work around using "OPEN" etc...
The second error, I'm not entirely sure about how to fix.

-z
May 14 '12 #1

✓ answered by Rabbit

Glad you got it working. I tested against a handful of NIST values but I wasn't exhaustive in my tests. Let me know if you find any errors.

Just be aware that my code was more optimized for speed than for readability. So rather than encapsulating the transformations in their own function, I expanded everything and put most of it in one function to avoid the overhead.

Share this Question
Share on Google+
6 Replies


NeoPa
Expert Mod 15k+
P: 31,419
When I look at the first block of code I see that expandKey() is defined on line #387.
Expand|Select|Wrap|Line Numbers
  1. Function expandKey(ByRef key(), ByRef box(), ByRef rcon())
Maybe you had a problem copying it all completely.

ForReading is a constant with a value of 1. ForWriting is a constant with a value of 2. See more at TechNet - Reading and Writing Text Files. If these constants aren't already defined for you then I suspect you may not have all the correct references set. Alternatively, you can set them up yourself or use the literal values.
May 14 '12 #2

zmbd
Expert Mod 5K+
P: 5,397
THANK YOU!!!

I'd never used this method to open a textfile for output.
In the past I'd used "Freefile", "Open", "Print", and "Close."
After reading up on the code I found where the references were missing from the "ForReading" "ForWriting", even found a bit of example code; however, the "ForAppend" was set to 3 not 8 which burggered me for awhile.

I'll try that cut and paste again!
I've been working from a textbook to create an AES function in VBA and here it is!!!!!!!!!

SO COOL!

-z

PS:
NeoPa:
Went back to the code... this is what I see when the the code is expanded:


Also, for some reason... all I see from the code you posted is the function header... no code.

What am I doing wrong?

-z
Attached Images
File Type: jpg AES_Code_PrtScn.jpg (22.6 KB, 2235 views)
May 14 '12 #3

Rabbit
Expert Mod 10K+
P: 12,357
zmbd, NeoPa only posted the header to show that the function was indeed defined in the code. What he was saying is that if you're getting the error, perhaps something went wrong when you copied and pasted the code from the site.
May 14 '12 #4

NeoPa
Expert Mod 15k+
P: 31,419
@zmbd
Your picture is of the code at the bottom of the second block of code. My reference was to the first block ;-)
NeoPa:
When I look at the first block of code I see that expandKey() is defined on line #387.
May 14 '12 #5

zmbd
Expert Mod 5K+
P: 5,397
Rabbit,
1ST - Thank You for the code!!

Indeed the function is in the ECM-Code-Block; however, I had originally copied the code from the following CBC-Code-Block.

Thus, to have something to work thru, I copied the code from the ECM-Code-Block, made a few tweeks:
- added "()" in the DIM sections for array variables;
- changed a "ByVal" to "ByRef" as required for array;
- added CONST for "forwrite" and "forread";
- change oWP.Value to sPass for the passphrase and added an "sPassIn" to the call (figured oWP is a textbox on a form which is what I would do))

After that the VBA compiler was happy and the code ran as expected.

So I started comparing the ECM-Mode code to the posted CBC code... some of the same tweeks as above needed to be made for the CBC code to compile and I had to C&P the expandkey() from the ECM code into the copied CBC code. It now will compile and encyphr the selected text file.

I haven't looked thru the expandkey() nor used the NIST files to verify.

Once again,
Thank You Rabbit!

I had finished creating the arrays and started trying to understand how to implement the equations... the text I am using is heavy on the theory and not so much on the practical application, and then, WOW, your code showed up! What a help in trying to follow the textbook!

-Z
May 14 '12 #6

Rabbit
Expert Mod 10K+
P: 12,357
Glad you got it working. I tested against a handful of NIST values but I wasn't exhaustive in my tests. Let me know if you find any errors.

Just be aware that my code was more optimized for speed than for readability. So rather than encapsulating the transformations in their own function, I expanded everything and put most of it in one function to avoid the overhead.
May 14 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.