Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:36:37+00:00 2026-06-05T12:36:37+00:00

This has become more of an exercise in what am I doing wrong than

  • 0

This has become more of an exercise in what am I doing wrong than mission critical, but I’d still like to see what (simple probably) mistake I’m making.

I’m using mysql (5.1.x) AES_ENCRYPT to encrypt a string. I’m using CF’s generateSecretKey(‘AES’) to make a key (I’ve tried it at defaul and 128 and 256 bit lengths).

So let’s say my code looks like this:

    <cfset key = 'qLHVTZL9zF81kiTnNnK0Vg=='/>
    <cfset strToEncrypt = '4111111111111111'/>
    <cfquery name="i" datasource="#dsn#">
        INSERT INTO table(str) 
            VALUES AES_ENCRYPT(strToEncrypt,'#key#');
    </cfquery>

That works fine as expected and I can select it using SELECT AES_DECRYPT(str,’#key#’) AS… with no problems at all.

What I can’t seem to do though is get CF to decrypt it using something like:

    <cfquery name="s" datasource="#dsn#">
        SELECT str
          FROM table
    </cfquery>
    <cfoutput>#Decrypt(s.str,key,'AES')#</cfoutput>

or

    <cfoutput>#Decrypt(toString(s.str),key,'AES')#</cfoutput>

I keep getting “The input and output encodings are not same” (including the toString() – without that I get a binary data error). The field type for the encrypted string in the db is blob.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:36:40+00:00Added an answer on June 5, 2026 at 12:36 pm

    This entry explains that mySQL handles AES-128 keys a bit differently than you might expect:

    .. the MySQL algorithm just or’s the bytes of a given passphrase
    against the previous bytes if the password is longer than 16 chars and
    just leaves them 0 when the password is shorter than 16 chars.

    Not highly tested, but this seems to yield the same results (in hex).

    <cfscript>
        function getMySQLAES128Key( key ) {
            var keyBytes   = charsetDecode( arguments.key, "utf-8" );
            var finalBytes = listToArray( repeatString("0,", 16) );
    
            for (var i = 1; i <= arrayLen(keyBytes); i++) {
                // adjust for base 0 vs 1 index
                var pos = ((i-1) % 16) + 1;
                finalBytes[ pos ] = bitXOR(finalBytes[ pos ], keyBytes[ i ]);
            }
    
            return binaryEncode( javacast("byte[]", finalBytes ), "base64" );
        }
    
        key     = "qLHVTZL9zF81kiTnNnK0Vg==";
        input   = "4111111111111111";
    
        encrypted = encrypt(input, getMySQLAES128Key(key), "AES", "hex");
        WriteDump("encrypted="& encrypted);
    
        // note: assumes input is in "hex". either convert the bytes 
        // to hex in mySQL first or use binaryEncode
        decrypted = decrypt(encrypted, getMySQLAES128Key(key), "AES", "hex");
        WriteDump("decrypted="& decrypted);
    </cfscript>
    

    Note: If you are using mySQL for encryption be sure to see its documentation which mentions the plain text may end up in various logs (replication, history, etectera) and “may be read by anyone having read access to that information”.


    Update: Things may have changed, but according to this 2004 bug report the .mysql_history file is only on Unix. (Keep in mind there may be other log files) Detailed instructions for clearing .mysql_history can be found in the manual, but in summary:

    • Set the MYSQL_HISTFILE variable to /dev/null (on each log in)
    • Create .mysql_history as a symbolic link to /dev/null (only once)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As stated in this question , single quotes in html has either become more
This has probably something to do with my transformations, but right now I can't
This has bothered me for a while. I use `hi-lock' or more specifically highlight-symbol
This has to be a frequent question, but not on SO yet, so it
This has happened before to me, but I can't remember how I fixed it.
I want to produce a simple, static HTML file, that has one or more
Preface: This has become a quite a long post. While I'm not new to
I have an ASP.NET website and over time it has become more and more
I've been using SQLObject for a long while, but noticed that SQLAlchemy has become
This seems like an extremely easy problem but alas I cannot figure it out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.