Converting from PERL to Java
843810Feb 9 2005 — edited Feb 19 2005I was trying to convert this PERL code in Java to be able to work on the same data set without any problem.
sub encrypt
{
my $secret = shift;
my $key = shift;
my $encrypted;
my $cipher = Crypt::CBC->new
({ key => $key, cipher => 'Crypt::Rijndael' });
$cipher->start('encrypt');
$encrypted = $cipher->crypt($secret);
$encrypted .= $cipher->finish;
return encode_base64($encrypted, "");
}
sub decrypt
{
my $encrypted = decode_base64(shift);
my $key = shift;
my $secret;
my $cipher = Crypt::CBC->new
({ key => $key, cipher => 'Crypt::Rijndael' });
$cipher->start('decrypt');
$secret = $cipher->crypt($encrypted);
$secret .= $cipher->finish;
return $secret;
}
I tested this java code I've found some post before that seems to be what I was looking for:
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import sun.misc.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class AESEncryptDataString
{
public AESEncryptDataString(byte[] keyBytes, byte[] ivBytes, String characterEncoding) throws Exception
{
SecretKey key = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec iv = new IvParameterSpec(ivBytes);
this.characterEncoding = characterEncoding;
this.encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
this.encryptCipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key, iv);
this.decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
this.decryptCipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, iv);
}
synchronized public String encrypt(String dataString) throws Exception
{
byte[] dataStringBytes = dataString.getBytes(characterEncoding);
byte[] encryptedDataStringBytes = this.encryptCipher.doFinal(dataStringBytes);
String encodedEncryptedDataString = this.base64Encoder.encode(encryptedDataStringBytes);
return encodedEncryptedDataString;
}
synchronized public String decrypt(String encodedEncryptedDataString) throws Exception
{
byte[] encryptedDataStringBytes = this.base64Decoder.decodeBuffer(encodedEncryptedDataString);
byte[] dataStringBytes = this.decryptCipher.doFinal(encryptedDataStringBytes);
String dataString = new String(dataStringBytes, characterEncoding);
return dataString;
}
public static void main(String[] args)
{
try
{
// Make sure BC are a valid provider - SUN do not supply an AES
Security.addProvider(new BouncyCastleProvider());
// The AES Key - any 16,24 or 32 bytes will do though beware of weak keys.
// This could be read from a file (preferably as bytes not chars).
final byte[] keyBytes = "0123456789ABCDEF01234567".getBytes("ASCII");
// IV For CBC mode - always 16 bytes - same as block size
// Again, could be read from a file.
final byte[] IVBytes = "0123456776543210".getBytes("ASCII");
// Encrypt agent that assumes the data string is only UTF-8 characters
AESEncryptDataString dataStringEncryptAgent = new AESEncryptDataString(keyBytes, IVBytes, "UTF-8");
// Get the data string to encrypt from the command line
String dataString = (args.length == 0)? "The quick brown fox jumps over the lazy dog." : args[0];
System.out.println("Data string ....................[" + dataString + "]");
String encodedEncryptedDataString = dataStringEncryptAgent.encrypt(dataString);
System.out.println("Encoded encrypted data String ..[" + encodedEncryptedDataString + "]");
String recoveredDataString = dataStringEncryptAgent.decrypt(encodedEncryptedDataString);
System.out.println("Recovered data string ..........[" + recoveredDataString + "]");
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}
private String characterEncoding;
private Cipher encryptCipher;
private Cipher decryptCipher;
private BASE64Encoder base64Encoder = new BASE64Encoder();
private BASE64Decoder base64Decoder = new BASE64Decoder();
}
that should do the same thing (take the string, encrypt (using AES/CBC), endecode).
But as a result I have:
java.lang.SecurityException: Unsupported keysize or algorithm parameters
Someone could tell me where is the problem?
Thanks.
Stefano