Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Encrypt in java decrypting in openssl

mohitanchliaJul 23 2007 — edited Aug 15 2007
I am using below class to encrypt the data. I want to decrypt the encrypted data by this class using openssl. I am trying to run openssl enc -aes-128-ebc -d -in a.in -out a.out, but I keep getting bad magic number.

public class ByteEncrypter {
public static final String encryptionScheme = "AES";
private SecretKeySpec skeySpec;
private Cipher cipher;
private static final String UNICODE_FORMAT = "UTF8";

public ByteEncrypter( String encryptionKey )
{

if ( encryptionKey == null )
throw new IllegalArgumentException( "encryption key was null" );
try
{
byte[] keyAsBytes = encryptionKey.getBytes( UNICODE_FORMAT );
skeySpec = new SecretKeySpec(keyAsBytes, encryptionScheme);
cipher = Cipher.getInstance( encryptionScheme );
}
catch (Exception e){
if(Debug.pvshDebugLevel > 0 ) System.out.println("Failed to create ByeEncrypter");
e.printStackTrace();
}
}

public byte [] encrypt(byte [] data) throws ArchiveServiceExceptionImpl{
if (null == data || data.length <=0){
throw new IllegalArgumentException( "encryption key was null" );
}
byte[] encrypted = null;
try{
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
encrypted = cipher.doFinal(data);
}catch(Exception e){
if(Debug.pvshDebugLevel > 0 ) System.out.println("Failed to encrypt ");
e.printStackTrace();
}
return encrypted;
}

public byte [] decrypt(byte [] data) throws ArchiveServiceExceptionImpl{
if (null == data || data.length <=0){
throw new IllegalArgumentException( "encryption key was null" );
}
byte[] decrypted = null;
try{
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
decrypted = cipher.doFinal(data);
}catch(Exception e){
if(Debug.pvshDebugLevel > 0 ) System.out.println("Failed to Decrypt");
e.printStackTrace();
}
return decrypted;
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 12 2007
Added on Jul 23 2007
16 comments
3,819 views