A question about encrypting file using AES

Discussion in 'Java' started by JTL.zheng, Jan 8, 2007.

  1. JTL.zheng

    JTL.zheng Guest

    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
     
    JTL.zheng, Jan 8, 2007
    #1
    1. Advertising

  2. JTL.zheng

    Daniel Dyer Guest

    On Mon, 08 Jan 2007 15:53:25 -0000, JTL.zheng <> wrote:

    > 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.

    --
    Daniel Dyer
    http://www.uncommons.org
     
    Daniel Dyer, Jan 8, 2007
    #2
    1. Advertising

  3. JTL.zheng wrote:
    > 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
     
    =?UTF-8?B?QXJuZSBWYWpow7hq?=, Jan 9, 2007
    #3
  4. JTL.zheng

    JTL.zheng Guest

    Thank you very much
    I get the unlimited strength cryptography now
    : )
     
    JTL.zheng, Jan 11, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news
    Replies:
    0
    Views:
    368
  2. JTL.zheng
    Replies:
    1
    Views:
    1,255
    =?UTF-8?B?QXJuZSBWYWpow7hq?=
    Jan 6, 2007
  3. jimgardener

    incorrect decryption using AES/CBC

    jimgardener, Jun 19, 2008, in forum: Java
    Replies:
    2
    Views:
    883
    Roedy Green
    Jun 20, 2008
  4. Phil C.
    Replies:
    2
    Views:
    443
    Phil C.
    Jul 19, 2005
  5. Nate Wiger

    New AES gem available -- fast-aes

    Nate Wiger, Jan 20, 2010, in forum: Ruby
    Replies:
    3
    Views:
    196
    Pål Bergström
    Jul 1, 2010
Loading...

Share This Page