This is the java code i compiled with jdk1.5.0_15 with the unlimited strength policy files
package com.ibx.kraft.sso.crypto;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.jboss.seam.util.Base64;
/**
- Class used to decrypt and encrypt strings using a AES-256
-
-
@author mperciun
-
*/
public abstract class CryptoAES {
/**
* Method that returns an array of binar bytes from an array of hexadecimal
* bytes Used for encryption/decryption
*
* @param hexValue
* @return
*/
private static byte[] getBinaryDataFromHex(byte[] hexValue) {
int block = 0;
String HEX_STRING = "0123456789ABCDEF";
byte[] data = new byte[hexValue.length / 2];
int index = 0;
boolean next = false;
for (int i = 0; i < hexValue.length; i++) {
block <<= 4;
int pos = HEX_STRING.indexOf(Character
.toUpperCase((char) hexValue[i]));
if (pos > -1)
block += pos;
if (next) {
data[index] = (byte) (block & 0xff);
index++;
next = false;
} else
next = true;
}
return data;
}
/**
* Generate the SecretKey object from the encryption key
*
* @param value
* @return
*/
private static SecretKeySpec generateKey(String value) {
// generate the key object which we pass to the cipher
SecretKeySpec keySpec = new SecretKeySpec(getBinaryDataFromHex(value
.getBytes()), "AES");
return keySpec;
}
/**
* Encrypts a string
*
* @param clearString
* @param publicKey
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws BadPaddingException
*/
public static String encryptString(String clearString, String publicKey)
throws InvalidKeyException, IllegalBlockSizeException,
NoSuchAlgorithmException, NoSuchPaddingException,
BadPaddingException {
Cipher encryptCipher = Cipher.getInstance("AES");
SecretKeySpec key = generateKey(publicKey);
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = encryptCipher.doFinal(clearString.getBytes());
return Base64.encodeBytes(cipherText).toString();
}
/**
* Decrypts a string
*
* @param clearString
* @param publicKey
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws BadPaddingException
*/
public static String decryptString(String encryptedString, String publicKey)
throws InvalidKeyException, IllegalBlockSizeException,
NoSuchAlgorithmException, NoSuchPaddingException,
BadPaddingException {
Cipher decryptCipher = Cipher.getInstance("AES");
byte[] toBeDecrypted = Base64.decode(encryptedString);
SecretKeySpec key = generateKey(publicKey);
decryptCipher.init(Cipher.DECRYPT_MODE, key);
byte[] decipherText = decryptCipher.doFinal(toBeDecrypted);
return new String(decipherText);
}
}
If i don’t install the unlimited policy files on my computer i get a InvalidKeyException but on the WM machine i get that IllegalBlockSizeException.
This is the WM Java Service:
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
String encryptionKey = IDataUtil.getString( pipelineCursor, “encryptionKey” );
pipelineCursor.destroy();
try{
//Generate password
String generatedPassword=GeneratePass.getCompliantPassword();
//Encrypt the new password
String encryptedPassword = CryptoAES.encryptString(“test”,“7E389A0F4B319BDD59A751F9DA9A9760CE0B2158D8F0C55252D4F7D1FF8773B6”);
// pipeline
IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put( pipelineCursor_1, “generatedEBPassword”, generatedPassword);
IDataUtil.put( pipelineCursor_1, “encryptedEBPassword”, encryptedPassword);
pipelineCursor_1.destroy();
} catch (InvalidKeyException ex) {
throw new ServiceException(“InvalidKeyException:” + ex.toString());
} catch (NoSuchPaddingException ex) {
throw new ServiceException(“NoSuchPaddingException:” + ex.toString());
} catch (IllegalBlockSizeException ex) {
throw new ServiceException(“IllegalBlockSizeException:” + ex.toString());
} catch (NoSuchAlgorithmException ex) {
throw new ServiceException(“NoSuchAlgorithmException:” + ex.toString());
} catch (BadPaddingException ex) {
throw new ServiceException(“BadPaddingException:” + ex.toString());
}
The data in there is for testing. The same encryption key works fine from a static java class.