Java Encryption error

Reply

Join Date: Sep 2005
Posts: 16
Reputation: bondito is an unknown quantity at this point 
Solved Threads: 1
bondito bondito is offline Offline
Newbie Poster

Java Encryption error

 
0
  #1
Apr 26th, 2006
Hi Everyone, i am trying to use an encryption class to encrypt pasword characters, i have modified the encryption class correctly, but i am getting an unreachable statement error and a missing return statement error. Pls could any one tell me what the problem might be with the code. Thanks

  1. package datasource;
  2. import java.security.*;
  3. import javax.crypto.*;
  4. import javax.crypto.spec.*;
  5.  
  6. public class Encryption {
  7. public static String encrypt (String password){
  8.  
  9. try{
  10. //Get the key generator and create the key
  11. //
  12. System.out.println("Getting key generator ....");
  13. KeyGenerator kgen = KeyGenerator.getInstance( "Blowfish" );
  14.  
  15. System.out.println("Getting key generator ....");
  16. SecretKey secretKey = kgen.generateKey();
  17. byte[] bytes = secretKey.getEncoded();
  18. SecretKeySpec specKey = new SecretKeySpec (bytes, "Blowfish" );
  19.  
  20. // Create the cipher object //
  21.  
  22. System.out.println("Creating cipher ....");
  23. Cipher cipher = Cipher.getInstance( "Blowfish" );
  24.  
  25. System.out.println("Encrypting ...");
  26. cipher.init( Cipher.ENCRYPT_MODE, specKey );
  27. // String target = "Encrypt Michael";
  28. byte[] encrypted = cipher.doFinal( password.getBytes() );
  29.  
  30. return encrypted.toString();
  31.  
  32. //System.out.println("before: " + target );
  33. // System.out.println("after: " + new String( encrypted ) );
  34.  
  35. // Decrypt
  36.  
  37.  
  38. cipher.init ( Cipher.DECRYPT_MODE, specKey );
  39.  
  40.  
  41. byte[] decrypted = cipher.doFinal ( encrypted);
  42. return decrypted.toString();
  43. //System.out.println("\nafter decrypt: " + new String( decrypted ) );
  44. }
  45. catch (Exception e) {
  46. e.printStackTrace();
  47.  
  48. }
  49. }
  50. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Java Encryption error

 
0
  #2
Apr 27th, 2006
There's no return statement at the end of the string method. The unreachable statements are anything after your first return statement, because there's no case statement involved... It will always execute making anything below that not execute.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC