Results 1 to 4 of 4
  1. #1
    Akirien is offline Member
    Join Date
    Feb 2012
    Posts
    18
    Rep Power
    0

    Default Can someone explain this code?

    Hello there.. This code was developed to implement the EL Gamal algorithm. Can someone explain to me why the input is "beef"?


    Java Code:
    package chapter4;
    
    import java.security.Key;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.SecureRandom;
    
    import javax.crypto.Cipher;
    
    /**
     * El Gamal example with random key generation.
     */
    public class RandomKeyElGamalExample
    {
        public static void main(String[] args) throws Exception
        {
            byte[]           input = new byte[] { (byte)0xbe, (byte)0xef };
            Cipher           cipher = Cipher.getInstance(
                                                       "ElGamal/None/NoPadding", "BC");
            KeyPairGenerator generator = KeyPairGenerator.getInstance("ElGamal", "BC");
            SecureRandom     random = Utils.createFixedRandom();
    
            // create the keys
    
            generator.initialize(256, random);
    
            KeyPair          pair = generator.generateKeyPair();
            Key              pubKey = pair.getPublic();
            Key              privKey = pair.getPrivate();
    
            System.out.println("input : " + Utils.toHex(input));
    
            // encryption step
    
            cipher.init(Cipher.ENCRYPT_MODE, pubKey, random);
    
            byte[] cipherText = cipher.doFinal(input);
    
            System.out.println("cipher: " + Utils.toHex(cipherText));
    
            // decryption step
    
            cipher.init(Cipher.DECRYPT_MODE, privKey);
    
            byte[] plainText = cipher.doFinal(cipherText);
    
            System.out.println("plain : " + Utils.toHex(plainText));
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can someone explain this code?

    Quote Originally Posted by Akirien View Post
    Hello there.. This code was developed to implement the EL Gamal algorithm. Can someone explain to me why the input is "beef"?
    The entire algorithm runs as follows: first some mumbo jumbo, next it creates the keys, followed by the encryption step and finally the program does the decryption step. Its output is (probably) 0xbeef again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Akirien is offline Member
    Join Date
    Feb 2012
    Posts
    18
    Rep Power
    0

    Default Re: Can someone explain this code?

    Quote Originally Posted by JosAH View Post
    The entire algorithm runs as follows: first some mumbo jumbo, next it creates the keys, followed by the encryption step and finally the program does the decryption step. Its output is (probably) 0xbeef again.

    kind regards,

    Jos

    Thx Jos for the explanation.. yeah.. its output is beef. :D

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Can someone explain this code?

    This may be related: jargon, node: DEADBEEF

    Also, Java class files are identified by their first four bytes, which are 0xCAFEBABE. :-P
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Anyone could explain the Code.....
    By seongchog in forum New To Java
    Replies: 2
    Last Post: 09-22-2011, 07:38 PM
  2. Any one help me fix JAVA code and explain ?
    By batista11b5 in forum New To Java
    Replies: 4
    Last Post: 10-23-2010, 05:19 PM
  3. Please explain how this bit of code works.
    By Allspark in forum New To Java
    Replies: 4
    Last Post: 09-03-2010, 03:56 AM
  4. Please explain these 2 lines of code to me..
    By murphaph in forum New To Java
    Replies: 10
    Last Post: 01-19-2010, 02:11 PM
  5. need to explain this code
    By reached in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •