private AlgorithmParameterSpec paramSpec;
private SecretKey secretKey;
public void afterPropertiesSet() throws ObjectFactoryException {
if ( ( saltString == null ) || ( algorithm == null ) || ( encryptionKey == null ) ) {
throw new ObjectFactoryException( "Required properties not set - need Salt, algorithm and encryption key" );
}
if ( saltString.length() != this.saltLength ) {
// Make sure that the salt length is 8 bytes - the PBEParameterSpec doesn't anything but
if ( saltString.length() < saltLength ) {
saltString = ( saltString + "!@#$%^&*" ).substring( 0, saltLength ); // postfix bytes to pad it out
} else if ( saltString.length() > saltLength ) {
saltString = saltString.substring( 0, saltLength ); // Trim off longer than 8-bytes
}
}
byte[] saltBytes = saltString.getBytes();
paramSpec = new PBEParameterSpec( saltBytes, getIterations() );
PBEKeySpec skeySpec = new PBEKeySpec( getEncryptionKey().toCharArray(), saltBytes, getIterations() );
try {
secretKey = SecretKeyFactory.getInstance( getAlgorithm() ).generateSecret( skeySpec );
} catch ( Exception ex ) {
ex.printStackTrace();
throw new ObjectFactoryException( "Encryption requested not available" );
}
}