Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

A question about encrypting file using AES

Thread view: 
JTL.zheng - 08 Jan 2007 15:53 GMT
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
Daniel Dyer - 08 Jan 2007 16:03 GMT
> my code is:
>
[quoted text clipped - 31 lines]
> 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.

Signature

Daniel Dyer
http://www.uncommons.org

Arne Vajhøj - 09 Jan 2007 02:06 GMT
>       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
[quoted text clipped - 4 lines]
> 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
JTL.zheng - 11 Jan 2007 13:33 GMT
Thank you very much
I get the unlimited strength cryptography now
: )


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.