a little question about DES

Discussion in 'Java' started by JTL.zheng, Dec 26, 2006.

  1. JTL.zheng

    JTL.zheng Guest

    my codes is:
    -----------------------
    static public void encrypt(String keyStr, File fin, File fout) {
    try {
    SecretKey key = new SecretKeySpec(keyStr.getBytes(), "DES");
    FileInputStream in = new FileInputStream(fin);
    FileOutputStream out = new FileOutputStream(fout);
    Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    desCipher.init(Cipher.ENCRYPT_MODE, key);
    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();
    }
    }
    --------------------

    as we know, the DES's key is 56bits

    but when I used:
    encrypt("1234567", new File("E:/1.txt"), new File("E:/2.txt"));
    or
    encrypt("123456789", new File("E:/1.txt"), new File("E:/2.txt"));

    it throw Exception:
    Invalid key length: 7 bytes
    or
    Invalid key length: 9 bytes

    but
    encrypt("12345678", new File("E:/1.txt"), new File("E:/2.txt"));
    is OK

    it means that the key must be 64bits,but DES's key is 56bits,isn't it?
    why I used 56bits key it throw Exception?
    JTL.zheng, Dec 26, 2006
    #1
    1. Advertising

  2. JTL.zheng

    Alan Krueger Guest

    Alan Krueger, Dec 26, 2006
    #2
    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. Replies:
    0
    Views:
    369
  2. Alex Nitulescu

    DES Crypto not working ???

    Alex Nitulescu, Feb 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    370
    Kevin
    Feb 13, 2005
  3. jim

    question about DES API

    jim, Sep 14, 2005, in forum: Java
    Replies:
    8
    Views:
    553
    jan V
    Sep 15, 2005
  4. ThaDoctor
    Replies:
    3
    Views:
    372
    Alan Woodland
    Sep 28, 2007
  5. Daniel
    Replies:
    1
    Views:
    201
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page