Security and DES keys


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Security and DES keys

Hybrid View

  1. #1
    Jason Bock Guest

    Security and DES keys

    I'm trying to generate a DES key, but the key length I get back is 8 and I
    thought it should be 56. Here's my code:

    import java.io.*;
    import java.util.*;
    import java.security.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;

    // assume pwd is equal to something like "javapassword"
    MessageDigest messagedigest = MessageDigest.getInstance("SHA");
    System.out.println("MessageDigest algorithm made via SHA algorithm...");
    messagedigest.update(pwd.getBytes());
    String msgDigest = new String(messagedigest.digest());
    System.out.println("Message digest complete, length is " + (new
    Integer(msgDigest.getBytes().length)) );

    DESKeySpec dks = new DESKeySpec(msgDigest.getBytes());
    System.out.println("DESKeySpec complete...");
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    System.out.println("SecretKeyFactory complete...");
    SecretKey key = skf.generateSecret(dks);
    System.out.println("Key generation is complete, length is " + (new
    Integer(key.getEncoded().length)) );

    What happens is that the SHA hash works (I have to duplicate this in Windows
    via the CryptoAPI and I've verified that the two hash values are equal).
    However, the key length always ends up being 8, when the documentation
    states that key lengths should be 56. What ends up happening is that the
    first 8 bytes match between my Windows and Java code, but after that nothing
    matches.

    How do I remedy this in my Java code?

    Thanks,

    Jason Bock



  2. #2
    Jason Bock Guest

    Re: Security and DES keys

    Shoot me. 8 bytes = 56 bits. Argh!

    Jason

    "Jason Bock" <jrbock@execpc.com> wrote in message
    news:38cd59d6$1@news.devx.com...
    > I'm trying to generate a DES key, but the key length I get back is 8 and I
    > thought it should be 56. Here's my code:
    >
    > import java.io.*;
    > import java.util.*;
    > import java.security.*;
    > import javax.crypto.*;
    > import javax.crypto.spec.*;
    >
    > // assume pwd is equal to something like "javapassword"
    > MessageDigest messagedigest = MessageDigest.getInstance("SHA");
    > System.out.println("MessageDigest algorithm made via SHA algorithm...");
    > messagedigest.update(pwd.getBytes());
    > String msgDigest = new String(messagedigest.digest());
    > System.out.println("Message digest complete, length is " + (new
    > Integer(msgDigest.getBytes().length)) );
    >
    > DESKeySpec dks = new DESKeySpec(msgDigest.getBytes());
    > System.out.println("DESKeySpec complete...");
    > SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    > System.out.println("SecretKeyFactory complete...");
    > SecretKey key = skf.generateSecret(dks);
    > System.out.println("Key generation is complete, length is " + (new
    > Integer(key.getEncoded().length)) );
    >
    > What happens is that the SHA hash works (I have to duplicate this in

    Windows
    > via the CryptoAPI and I've verified that the two hash values are equal).
    > However, the key length always ends up being 8, when the documentation
    > states that key lengths should be 56. What ends up happening is that the
    > first 8 bytes match between my Windows and Java code, but after that

    nothing
    > matches.
    >
    > How do I remedy this in my Java code?
    >
    > Thanks,
    >
    > Jason Bock
    >
    >




  3. #3
    Jason Bock Guest

    Re: Security and DES keys


    Jason Bock <jrbock@execpc.com> wrote in message
    news:38cd5e00@news.devx.com...
    > Shoot me. 8 bytes = 56 bits. Argh!
    >
    > Jason


    Shoot me again! I meant that the 8 bytes in the key = 64 bits, but the key
    is the 56 bit key.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center