Help with "AES Encryption Algorithm for VBA and VBScript"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    Help with "AES Encryption Algorithm for VBA and VBScript"

    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.OpenTextFil e(sFile, ForReading)" I get the error "ForReading " isn't a defined variable.
    - "expandedKe y = expandKey(aesKe y, 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
    Last edited by NeoPa; May 14 '12, 11:16 AM. Reason: Fixed for moving to the Questions Forum.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32663

    #2
    When I look at the first block of code I see that expandKey() is defined on line #387.
    Code:
    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.

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      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:
      [IMGnothumb]http://bytes.com/attachments/attachment/6380d1337001281/aes_code_prtscn .jpg[/IMGnothumb]
      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 Files
      Last edited by zmbd; May 14 '12, 01:18 PM. Reason: Added PS

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        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.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32663

          #5
          @zmbd
          Your picture is of the code at the bottom of the second block of code. My reference was to the first block ;-)
          Originally posted by NeoPa
          NeoPa:
          When I look at the first block of code I see that expandKey() is defined on line #387.

          Comment

          • zmbd
            Recognized Expert Moderator Expert
            • Mar 2012
            • 5501

            #6
            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

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              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.

              Comment

              Working...