Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to set secret key

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to set secret key

    Hi all,

    Below is a des encryption/decryption algorithm i grab from online.

    Questions:
    1) How can i set my own secret key instead of auto set key
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    2)How to i print the sboxes because my assignment require me to analysis sboxes ?

    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
     
    class DesEncrypter {
      Cipher ecipher;
     
      Cipher dcipher;
     
      DesEncrypter(SecretKey key) throws Exception {
        ecipher = Cipher.getInstance("DES");
        dcipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        dcipher.init(Cipher.DECRYPT_MODE, key);
      }
     
      public String encrypt(String str) throws Exception {
        // Encode the string into bytes using utf-8
        byte[] utf8 = str.getBytes("UTF8");
     
        // Encrypt
        byte[] enc = ecipher.doFinal(utf8);
     
        // Encode bytes to base64 to get a string
        return new sun.misc.BASE64Encoder().encode(enc);
      }
     
      public String decrypt(String str) throws Exception {
        // Decode base64 to get bytes
        byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
     
        byte[] utf8 = dcipher.doFinal(dec);
     
        // Decode using utf-8
        return new String(utf8, "UTF8");
      }
    }
     
    public class Main {
      public static void main(String[] argv) throws Exception {
        SecretKey key = KeyGenerator.getInstance("DES").generateKey();
        DesEncrypter encrypter = new DesEncrypter(key);
        String encrypted = encrypter.encrypt("Don't tell anybody!");
        String decrypted = encrypter.decrypt(encrypted);
      }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: How to set secret key

    Hopefully this link will help you - Secret Key Cryptography Tutorial
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to set secret key

    Quote Originally Posted by JavaPF View Post
    Hopefully this link will help you - Secret Key Cryptography Tutorial
    Thanks !! The link was useful. I found the answer i looking for @ the comment section.

    Regarding my second question about the sbox. It is possible to have access to the sboxes?