Java DES

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xtingy
    New Member
    • Aug 2008
    • 1

    Java DES

    Hellos.

    Am having difficulty solving my problem as follows:

    Im trying to write a program to perform DES encryption. I do not wish to generate a DES key using the SecretKey method as i have my own set of keys. I have the following:

    plainText (8 bytes) in hex representation.
    key(7 bytes) in hex representation.
    Both are stored in the form of byte arrays.
    Then i do a hex2Byte conversion for both plainText and key.
    and i passed these 2 variables into my try block as shown below. Is it correct?

    Nothing is displayed after i execute the program. Pls help! Thanks :)

    Code:
    try 		
    {  			 			
    byte[] cipherText = null; 			 			
    //init cipher 			
    KeySpec ks = new DESKeySpec(key); 			
    SecretKeyFactory kf =SecretKeyFactory.getInstance("DES"); 
    SecretKey ky = kf.generateSecret(ks); 			
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 			cipher.init(Cipher.ENCRYPT_MODE, ky); 	
    
    //encrypt 			
    cipherText = cipher.doFinal(plainText); 
    System.out.println("Key: " +key); 
    System.out.println("PlainText:"+plainText); 		
    	 			 			
    //display cipherText 			
    System.out.println("CipherText: " + byte2Hex(cipherText)); 			 		
    } 	
    	 		
    catch (Exception e) 		{}
  • cordeo
    New Member
    • Jul 2008
    • 16

    #2
    Originally posted by xtingy
    Hellos.

    Nothing is displayed after i execute the program. Pls help! Thanks :)

    Code:
    try 		
    {  			 			
    byte[] cipherText = null; 			 			
    //init cipher 			
    KeySpec ks = new DESKeySpec(key); 			
    SecretKeyFactory kf =SecretKeyFactory.getInstance("DES"); 
    SecretKey ky = kf.generateSecret(ks); 			
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 			cipher.init(Cipher.ENCRYPT_MODE, ky); 	
    
    //encrypt 			
    cipherText = cipher.doFinal(plainText); 
    System.out.println("Key: " +key); 
    System.out.println("PlainText:"+plainText); 		
    	 			 			
    //display cipherText 			
    System.out.println("CipherText: " + byte2Hex(cipherText)); 			 		
    } 	
    	 		
    catch (Exception e) 		{}
    I don't have time to put up a full answer at this very moment but as a programming rule: NEVER ignore exceptions by using an empty catch-block.

    It's an often seen beginners mistake, but problems that otherwise would be reported might silently disappear, which is undesired of course. So write:
    Code:
    catch (Exception e) { e.printStackTrace(); }
    and have a look at your console. Not being sure whether this is enhough help, it is an important step in the right direction.

    Comment

    • cordeo
      New Member
      • Jul 2008
      • 16

      #3
      as a second rule,
      2. Catch specific exceptions
      By catching (java.lang.)Exc eption, you also catch things like IndexOutOfBound sException, NegativeArraySi zeException, NoSuchElementEx ception, IOException and others that have nothing to do (at least, not directly) with the code in the try-block. Especially in the case of an empty catch-block, lots of problems that are neatly reported by java are silently ignored, causing
      Nothing is displayed after i execute the program.
      For example, you might have a wrong key size causing a InvalidKeyExcep tion:
      java.security.I nvalidKeyExcept ion: Wrong key size
      at javax.crypto.sp ec.DESKeySpec.< init>(DashoA13* ..)
      at javax.crypto.sp ec.DESKeySpec.< init>(DashoA13* ..)
      at etc.

      If plainText is null, you get
      Exception in thread "main" java.lang.Illeg alArgumentExcep tion: Null input buffer
      at javax.crypto.Ci pher.doFinal(Da shoA13*..)
      at etc.

      Code:
              try {
                  // making this null causes an IllegalArgumentException
                  byte[] plainText = new byte[]{0x38,0x39};
                  // making this shorter causes an InvalidKeyException
                  byte[] key = new byte[]{0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37};
                  byte[] cipherText = null;
                  //init cipher      
                  KeySpec ks = new DESKeySpec(key);
                  SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
                  SecretKey ky = kf.generateSecret(ks);
                  Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
                  cipher.init(Cipher.ENCRYPT_MODE, ky);
                  //encrypt      
                  cipherText = cipher.doFinal(plainText);
                  System.out.println("Key: " + new String(key));
                  System.out.println("PlainText:" + new String(plainText));
                  //display cipherText          
                  System.out.println("CipherText: " + byte2Hex(cipherText));
              } catch (InvalidKeyException e) {
                  e.printStackTrace();
              } catch (NoSuchAlgorithmException e) {
                  e.printStackTrace();
              } catch (InvalidKeySpecException e) {
                  e.printStackTrace();
              } catch (NoSuchPaddingException e) {
                  e.printStackTrace();
              } catch (IllegalBlockSizeException e) {
                  e.printStackTrace();
              } catch (BadPaddingException e) {
                  e.printStackTrace();
              }
      3. System.out.prin tln for byte[]
      This actually prints a java object pointer on the console:
      System.out.prin tln("Key: " + key);
      for example might print
      Key: [B@c7e553
      Using a special routine like byte2Hex is a solution, otherwise, you might use
      Code:
      System.out.println("Key: " + new String(key));
      as a quick workaround.

      Comment

      Working...