tags:

views:

67

answers:

2

I have a test which runs great on my development MacBook Pro, but fails to run in continuous integration TeamCity server.

The error is following:

java.security.InvalidKeyException: Illegal key size
    at javax.crypto.Cipher.a(DashoA13*..)
    at javax.crypto.Cipher.init(DashoA13*..)
    at javax.crypto.Cipher.init(DashoA13*..)

Both development box and TeamCity uses Java 1.6 and I use BouncyCastle library for the need of special AES encryption.

The code is following:

private byte[] aesEncryptedInfo(String info) throws UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidParameterSpecException, InvalidAlgorithmParameterException, NoSuchProviderException {
    Security.addProvider(new BouncyCastleProvider());
    SecretKey secret = new SecretKeySpec(CUSTOMLONGSECRETKEY.substring(0, 32).getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
    cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(VECTOR_SECRET_KEY.getBytes()));
    return cipher.doFinal(info.getBytes("UTF-8"));
}

UPDATE

Looks like according to the selected answer I have to modify something on my TeamCity installation and it will possibly affect some user installations - so its not a good choice I have to switch to another crypto library to do that without limitations. So probably bouncy castle will help.

+2  A: 

You will probably need to install the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files (available at Oracle).

If you don't, the keysize is limited due to US export laws.

Mark Rotteveel
I didn't install JCE USJ specially on my development box but it works there.
Vladimir
@Mark Rotteveel, Do you think that it can be installed by default there and not installed on server?
Vladimir
@Vladimir: I am not able to find whether Apple bundles unlimited strength policy files or not, but it's not bundled in JVMs provided by Oracle (or Sun previously). If your TeamCity runs on Linux/Windows, you need to install unlimited strength policy files on your build server on your own.
Peter Štibraný
+1  A: 

In addition to installing policy files, also make sure that CUSTOMLONGSECRETKEY...getBytes() does indeed produce 32 bytes array. I would use CUSTOMLONGSECRETKEY.getBytes(some encoding) and get first 32 bytes from that. Better yet, use whole secret key to derive keys for AES with the size that you need.

Peter Štibraný
@Peter Štibraný, CUSTOMLONGSECRETKEY is constant = "3C7C6086-CF22-4972-9616-F294DAF77092" for both runs. I wonder how it can affect in TeamCity.
Vladimir
@Vladimir: I was trying to point that you should use getBytes with explicit encoding, but that doesn't seem to be the problem with your key. I'd try to install that policy files. If you don't do that, you're limited to 128-bit keys for AES.
Peter Štibraný
@Peter, Okay I got your idea. Thanks will try to install the policy files.
Vladimir