A question about encrypting file using AES

J

JTL.zheng

my code is:

-----------------------------------------------------
public static boolean enDeCrypt(String keyStr, File fin, File fout) {
try {
FileInputStream in = new FileInputStream(fin);
FileOutputStream out = new FileOutputStream(fout);
Cipher desCipher = Cipher.getInstance("AES");
desCipher.init(Cipher.ENCRYPT_MODE,
new SecretKeySpec(keyStr.getBytes("UTF-8"),
"AES"));
CipherOutputStream cos = new CipherOutputStream(out, desCipher);
byte[] enBuffer = new byte[4096];
int n;
while ( (n = in.read(enBuffer)) != -1) {
cos.write(enBuffer, 0, n);
}
cos.close();
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
-----------------------------------------------------

but it throw exception:
java.security.InvalidKeyException: Illegal key size or default
parameters
I used a 192-bit key

if I change the key's length, it will throw excepion:
java.security.InvalidKeyException: Invalid AES key length: 272
It seemed that it is not the problem about the length of the key

how can I fix it

Thank you very much in advance
 
D

Daniel Dyer

my code is:

-----------------------------------------------------
public static boolean enDeCrypt(String keyStr, File fin, File fout) {
try {
FileInputStream in = new FileInputStream(fin);
FileOutputStream out = new FileOutputStream(fout);
Cipher desCipher = Cipher.getInstance("AES");
desCipher.init(Cipher.ENCRYPT_MODE,
new SecretKeySpec(keyStr.getBytes("UTF-8"),
"AES"));
CipherOutputStream cos = new CipherOutputStream(out, desCipher);
byte[] enBuffer = new byte[4096];
int n;
while ( (n = in.read(enBuffer)) != -1) {
cos.write(enBuffer, 0, n);
}
cos.close();
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
-----------------------------------------------------

but it throw exception:
java.security.InvalidKeyException: Illegal key size or default
parameters
I used a 192-bit key
if I change the key's length, it will throw excepion:
java.security.InvalidKeyException: Invalid AES key length: 272
It seemed that it is not the problem about the length of the key
how can I fix it

By default, the longest key that you can have is 128 bits. For longer
keys you will need to download and install the unlimited strength
cryptography policies available from Sun's website. You are not allowed
them if you are a member of the Axis of Evil.

Dan.
 
G

Guest

JTL.zheng said:
Cipher desCipher = Cipher.getInstance("AES");
desCipher.init(Cipher.ENCRYPT_MODE,
new SecretKeySpec(keyStr.getBytes("UTF-8"),
"AES"));
but it throw exception:
java.security.InvalidKeyException: Illegal key size or default
parameters
I used a 192-bit key

if I change the key's length, it will throw excepion:
java.security.InvalidKeyException: Invalid AES key length: 272
It seemed that it is not the problem about the length of the key

272 != 192

I would check keyStr.getBytes("UTF-8").length because
maybe UTF-8 is giving more bytes than you thougth.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top