aes encryption and openssl descryption (interoperable)
843811Aug 18 2006 — edited Mar 2 2007hi all,
I am a new to encryption using AES, actually it is an adhoc task.
also new to the jce library.
I need to encrypt a text file from java program using bouncycastle jce as provider.
i create both encrypt and decrypt program. They work fine(the decrypt can store the original text from encrypted on). but it got corrupt when use openssl (0.98) under command prompt on win32 platform. i am seeking help why openssl can't decrypt the file correct
after encrypted the text using java program and try using the openssl command to restore the original text.
D:\fts\aes>openssl enc -d -aes-128-cbc -k 1234567890123456 -in encrypt.aes -out
test.des -nosalt
it shows the folllowing error
bad decrypt
3860:error:06065064:digital envelope routines:EVP_DecryptFinal:bad decrypt:.\cry
pto\evp\evp_enc.c:450:
what happen to my java code. do i provide the wrong key to openssl
or i miss any components in openssl.??
I understand there it need IV, but i just ignore it, it works fine in decrypted code. does openssl to provide iv explicitly??
I also taked alook of the post threadid=739626. but its code seems not compatiable in my version. it will be appreciated if the complete code is provided. i just a beginner in java ce with some theory/concept from books.
thx
here is my encrypt code.
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.security.cert.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
public class EncryptFile3 {
public static void main(String args[]) {
if (args.length < 1) {
System.out.println("Usage: java EncryptFile <file name>");
System.exit(-1);
}
try {
File desFile = new File("encrypt.aes");
FileInputStream fis = null;
FileOutputStream fos;
CipherInputStream cis;
//String xform = "AES/CBC/PKCS5Padding";
String xform = "AES/CBC/NoPadding";
// Creation of Secret key
byte[] keyBytes = "1234567890123456".getBytes(); //AES 128bit
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
System.out.println("Generate key for AES at "+keyBytes.length +" bits");
Key key = new SecretKeySpec(keyBytes, "AES");
// Creation of Cipher objects
Cipher encrypt = Cipher.getInstance(xform);
System.out.println("Get Provider Info."+encrypt.getProvider().getInfo());
encrypt.init(Cipher.ENCRYPT_MODE, key);
// Open the Plaintext file
try {
fis = new FileInputStream(args[0]);
} catch(IOException err) {
System.out.println("Cannot open file!");
System.exit(-1);
}
cis = new CipherInputStream(fis, encrypt);
// Write to the Encrypted file
fos = new FileOutputStream(desFile);
byte[] b = new byte[8];
// fos.write("Salted__".getBytes());
int i = cis.read(b);
while (i != -1) {
fos.write(b, 0, i);
i = cis.read(b);
}
fos.flush();
fos.close();
cis.close();
fis.close();
} catch(Exception e){
e.printStackTrace();
}
}
}