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 750223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:31:35+00:00 2026-05-14T14:31:35+00:00

*** sweet – thanks to Edward Smith for the CF Technote that indicated the

  • 0

*** sweet – thanks to Edward Smith for the CF Technote that indicated the key from ColdFusion was Base64 encoded. See generateKey() for the ‘fix’

My task is to use Java 1.4.2 to match the results a given ColdFusion code sample for encryption.

Known/given values:

  • A 24-byte key
  • A 16-byte salt (IVorSalt)
  • Encoding is Hex
  • Encryption algorithm is AES/CBC/PKCS5Padding
  • A sample clear-text value
  • The encrypted value of the sample clear-text after going through the ColdFusion code

Assumptions:

  • Number of iterations not specified in the ColdFusion code so I assume only one iteration
  • 24-byte key so I assume 192-bit encryption

Given/working ColdFusion encryption code sample:

<cfset ThisSalt = "16byte-salt-here">
<cfset ThisAlgorithm = "AES/CBC/PKCS5Padding">
<cfset ThisKey = "a-24byte-key-string-here">
<cfset thisAdjustedNow = now()>
<cfset ThisDateTimeVar = DateFormat( thisAdjustedNow , "yyyymmdd" )>
<cfset ThisDateTimeVar = ThisDateTimeVar & TimeFormat( thisAdjustedNow , "HHmmss" )>
<cfset ThisTAID = ThisDateTimeVar & "|" & someOtherData>
<cfset ThisTAIDEnc = Encrypt( ThisTAID , ThisKey , ThisAlgorithm , "Hex" , ThisSalt)>

My Java 1.4.2 encryption/decryption code swag:

package so.example;

import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.*;

public class SO_AES192 {

private static final String _AES = "AES";
private static final String _AES_CBC_PKCS5Padding = "AES/CBC/PKCS5Padding";
private static final String KEY_VALUE = "a-24byte-key-string-here";
private static final String SALT_VALUE = "16byte-salt-here";
private static final int ITERATIONS = 1;

private static IvParameterSpec ivParameterSpec;

public static String encryptHex(String value) throws Exception {
    Key key = generateKey();

    Cipher c = Cipher.getInstance(_AES_CBC_PKCS5Padding);
    ivParameterSpec = new IvParameterSpec(SALT_VALUE.getBytes());
    c.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);

    String valueToEncrypt = null;
    String eValue = value;
    for (int i = 0; i < ITERATIONS; i++) {
//            valueToEncrypt = SALT_VALUE + eValue; // pre-pend salt - Length > sample length
        valueToEncrypt =  eValue;     // don't pre-pend salt  Length = sample length
        byte[] encValue = c.doFinal(valueToEncrypt.getBytes());
        eValue =  Hex.encodeHexString(encValue);
    }
    return eValue;
}

public static String decryptHex(String value) throws Exception {
    Key key = generateKey();

    Cipher c = Cipher.getInstance(_AES_CBC_PKCS5Padding);
    ivParameterSpec = new IvParameterSpec(SALT_VALUE.getBytes());
    c.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);

    String dValue = null;
    char[] valueToDecrypt = value.toCharArray();
    for (int i = 0; i < ITERATIONS; i++) {
        byte[] decordedValue = Hex.decodeHex(valueToDecrypt);
        byte[] decValue = c.doFinal(decordedValue);
//            dValue = new String(decValue).substring(SALT_VALUE.length()); // when salt is pre-pended
        dValue = new String(decValue);   // when salt is not pre-pended
        valueToDecrypt = dValue.toCharArray();
    }
    return dValue;
}

private static Key generateKey() throws Exception {
    // Key key = new SecretKeySpec(KEY_VALUE.getBytes(), _AES); // this was wrong
    Key key = new SecretKeySpec(new BASE64Decoder().decodeBuffer(keyValueString), _AES); // had to un-Base64 the 'known' 24-byte key.
    return key;
}

}

I cannot create a matching encrypted value nor decrypt a given encrypted value. My guess is it’s something to do with how I’m handling the initial vector/salt.

I’m not very crypto-savvy but I’m thinking I should be able to take the sample clear-text and produce the same encrypted value in Java as ColdFusion produced. I am able to encrypt/decrypt my own data with my Java code (so I’m consistent) but I cannot match nor decrypt the ColdFusion sample encrypted value.

I have access to a local webservice that can test the encrypted output. The given ColdFusion output sample passes/decrypts fine (of course). If I try to decrypt the same sample with my Java code (using the actual key and salt) I get a “Given final block not properly padded” error. I get the same net result when I pass my attempt at encryption (using the actual key and salt) to the test webservice.

Any Ideas?

  • 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-05-14T14:31:35+00:00Added an answer on May 14, 2026 at 2:31 pm

    Is the value in the Coldfusion ThisKey:

    <cfset ThisKey = "a-24byte-key-string-here">
    

    The exact same string that is returned from the java generateKey() function? I believe they need to be to be the same string for the generated encrypted text to be the same.

    To use a fixed key like that in CF, you may need to follow this from the CF technote on strong encryption:

    You may want to generate your own key for two reasons:

    1. You want to match the details of other encryption software.
    2. You want to increase the resistance to cracking of your
      encrypted data by pattern-oriented
      cryptanalysis techniques.

    For example, to create a 32-byte key
    to use with the AES algorithm with the
    hex value:

    8738fed68e7677d374e0946c8f7bd3bb4f50f23717f9f3667b2419483959039c

    you would use the ColdFusion functions
    BinaryDecode and ToBase64 to create
    the key:

    <cfset myKey =
    ToBase64(BinaryDecode("8738fed68e7677d374e0946c8f7bd3bb4f50f23717f9f3667b2419483959039c","Hex")>
    <cfset encrypted =Encrypt(myString, myKey, "AES")>
    

    EDIT: Just realized that the key (as you mentioned in your comment) is base64, so if the “generateKey” method in Java looks like:

    private static Key generateKey() throws Exception {
    final byte[] decodedKey = new BASE64Decoder().decodeBuffer(KEY_VALUE);
    final Key key = new SecretKeySpec(decodedKey, _AES);
    return key;
    }
    

    You should be golden.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep reading about C99 and C++11 and all these totally sweet things that
I long for those sweet optional arguments from the days when I programmed in
Short and sweet version: Is there a single web service method that would return
I have a sheet with a custom button on it from where I control
I have an excel sheet full of times. They are formatted so that they
Python has syntactically sweet list comprehensions: S = [x**2 for x in range(10)] print
Gmail has this sweet thing going on to get an atom feed: def gmail_url(user,
Short and sweet: How can I tell Interface Builder to center a window on
Short and sweet, Is it ok to use (current version) Ruby 1.9.1 with Rails
Short and sweet version: On one machine out of around a hundred test machines

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.