file encrypted by bouncycastle can not decrpted by SUN AES 256
843811Mar 9 2009 — edited Mar 10 2009Hi, I use org.bouncycastle.crypto.engines.AESLightEngine to encyrpt a file, and use javax.crypto.Cipher to decrypt the file, programs runs ok, but the decrypted file is a little difference with orignal file, can you help to dig out what's the reason? Below is the details
1. use below cmd to encrypt testfile.txt, and the result is encrypted.txt
<strong>java -cp ./bcprov-jdk15-141.jar;. BAES E testfile.txt</strong>
2. use below cmd to decrypte encrypted.txt, and the result is decrypted.txt. why it is not same as original?
<strong>java AES D</strong>
testfile.txt:
http://www.javacamp.org/scea/faq.html
https://www.certmanager.net/sun_assignment/
decrypted.txt:
??'??i?N7??%,amp.org/scea/faq.html
https://www.certmanager.net/sun_assignment/
below is related code:
<strong>BAES.java:</strong>
import org.bouncycastle.crypto.CipherKeyGenerator;
import org.bouncycastle.crypto.KeyGenerationParameters;
import org.bouncycastle.crypto.modes.CFBBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.engines.AESLightEngine;
import org.bouncycastle.crypto.StreamBlockCipher;
...
public class BAES { <br />
public static byte[] enc(byte[] k, byte[] s) throws Exception { <br />
SecretKey sk = new SecretKeySpec(k, "AES");
AESLightEngine blockCipher = new AESLightEngine();
CFBBlockCipher cfbCipher = new CFBBlockCipher(blockCipher, 8);
StreamBlockCipher streamCipher = new StreamBlockCipher(cfbCipher);
KeyParameter kpm = new KeyParameter(sk.getEncoded());
streamCipher.init(true, kpm);
byte[] out = new byte[s.length];
streamCipher.processBytes(s, 0, s.length, out, 0);
return out;
}
....
public static void main(String[] args) throws Exception {
...
if (args[0].equals("E")){ <br />
FileInputStream in = new FileInputStream("aes_key2.txt ");
byte[] k = new byte[32];
in.read(k);
in.close();
FileInputStream in2 = new FileInputStream(args[1]);
int size = in2.available();
byte[] buf=new byte[size];
in2.read(buf);
in2.close();
byte[] en = enc(k, buf);
FileOutputStream out = new FileOutputStream("encrypted.txt ");
out.write(en);
out.close();
}
====================
<strong>AES.java:</strong>
public class AES {
...
public static byte[] dec(byte[] k, byte[] s) throws Exception{
SecretKeySpec skeySpec = new SecretKeySpec(k, "AES");
String initialVectorParam = "0000000000000000";
IvParameterSpec initalVector = new IvParameterSpec(initialVectorParam.getBytes());
Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, initalVector);
return cipher.doFinal(s);
}
...
public static void main(String[] args) throws Exception { <br />
...
if (args[0].equals("D")){ <br />
FileInputStream in = new FileInputStream("aes_key2.txt ");
byte[] k = new byte[32];
in.read(k);
in.close();
FileInputStream in2 = new FileInputStream("encrypted.txt ");
int size = in2.available();
byte[] buf=new byte[size];
in2.read(buf);
in2.close();
byte[] de = dec(k, buf);
BufferedWriter out = new BufferedWriter(new FileWriter("decrypted.txt "));
out.write(new String(de));
out.close();
}
...