473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Java DES

1
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 :)

Expand|Select|Wrap|Line Numbers
  1. try         
  2. {                           
  3. byte[] cipherText = null;                          
  4. //init cipher             
  5. KeySpec ks = new DESKeySpec(key);             
  6. SecretKeyFactory kf =SecretKeyFactory.getInstance("DES"); 
  7. SecretKey ky = kf.generateSecret(ks);             
  8. Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");             cipher.init(Cipher.ENCRYPT_MODE, ky);     
  9.  
  10. //encrypt             
  11. cipherText = cipher.doFinal(plainText); 
  12. System.out.println("Key: " +key); 
  13. System.out.println("PlainText:"+plainText);         
  14.  
  15. //display cipherText             
  16. System.out.println("CipherText: " + byte2Hex(cipherText));                      
  17. }     
  18.  
  19. catch (Exception e)         {}
  20.  
Aug 22 '08 #1
2 2727
cordeo
16
Hellos.

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

Expand|Select|Wrap|Line Numbers
  1. try         
  2. {                           
  3. byte[] cipherText = null;                          
  4. //init cipher             
  5. KeySpec ks = new DESKeySpec(key);             
  6. SecretKeyFactory kf =SecretKeyFactory.getInstance("DES"); 
  7. SecretKey ky = kf.generateSecret(ks);             
  8. Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");             cipher.init(Cipher.ENCRYPT_MODE, ky);     
  9.  
  10. //encrypt             
  11. cipherText = cipher.doFinal(plainText); 
  12. System.out.println("Key: " +key); 
  13. System.out.println("PlainText:"+plainText);         
  14.  
  15. //display cipherText             
  16. System.out.println("CipherText: " + byte2Hex(cipherText));                      
  17. }     
  18.  
  19. catch (Exception e)         {}
  20.  
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:
Expand|Select|Wrap|Line Numbers
  1. catch (Exception e) { e.printStackTrace(); }
  2.  
and have a look at your console. Not being sure whether this is enhough help, it is an important step in the right direction.
Aug 29 '08 #2
cordeo
16
as a second rule,
2. Catch specific exceptions
By catching (java.lang.)Exception, you also catch things like IndexOutOfBoundsException, NegativeArraySizeException, NoSuchElementException, 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 InvalidKeyException:
java.security.InvalidKeyException: Wrong key size
at javax.crypto.spec.DESKeySpec.<init>(DashoA13*..)
at javax.crypto.spec.DESKeySpec.<init>(DashoA13*..)
at etc.

If plainText is null, you get
Exception in thread "main" java.lang.IllegalArgumentException: Null input buffer
at javax.crypto.Cipher.doFinal(DashoA13*..)
at etc.

Expand|Select|Wrap|Line Numbers
  1.         try {
  2.             // making this null causes an IllegalArgumentException
  3.             byte[] plainText = new byte[]{0x38,0x39};
  4.             // making this shorter causes an InvalidKeyException
  5.             byte[] key = new byte[]{0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37};
  6.             byte[] cipherText = null;
  7.             //init cipher      
  8.             KeySpec ks = new DESKeySpec(key);
  9.             SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
  10.             SecretKey ky = kf.generateSecret(ks);
  11.             Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
  12.             cipher.init(Cipher.ENCRYPT_MODE, ky);
  13.             //encrypt      
  14.             cipherText = cipher.doFinal(plainText);
  15.             System.out.println("Key: " + new String(key));
  16.             System.out.println("PlainText:" + new String(plainText));
  17.             //display cipherText          
  18.             System.out.println("CipherText: " + byte2Hex(cipherText));
  19.         } catch (InvalidKeyException e) {
  20.             e.printStackTrace();
  21.         } catch (NoSuchAlgorithmException e) {
  22.             e.printStackTrace();
  23.         } catch (InvalidKeySpecException e) {
  24.             e.printStackTrace();
  25.         } catch (NoSuchPaddingException e) {
  26.             e.printStackTrace();
  27.         } catch (IllegalBlockSizeException e) {
  28.             e.printStackTrace();
  29.         } catch (BadPaddingException e) {
  30.             e.printStackTrace();
  31.         }
  32.  
3. System.out.println for byte[]
This actually prints a java object pointer on the console:
System.out.println("Key: " + key);
for example might print
Key: [B@c7e553
Using a special routine like byte2Hex is a solution, otherwise, you might use
Expand|Select|Wrap|Line Numbers
  1. System.out.println("Key: " + new String(key));
as a quick workaround.
Aug 29 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
1
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
0
by: mailkhurana | last post by:
Hii , I am trying to use a type 2 driver to connect to DB2 0n AIX 5 I have a small java test to class to establish a conneciton with the db .. I am NOT using WAS or any appserver When I try to...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
0
by: jaywak | last post by:
Just tried running some code on Linux (2.4.21-32.0.1.EL and Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)) and Windows XPSP2 (with Java HotSpot(TM) Client VM (build...
1
by: jaimemartin | last post by:
hello, I want to validate an xml by means of a schema (xsd). To do that first of all I´m using a SchemaFactory. The problem is that if I run the code in Windows all works fine, but If I run it in...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.