944,161 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3625
  • Java RSS
Apr 26th, 2006
0

Java Encryption error

Expand Post »
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

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bondito is offline Offline
17 posts
since Sep 2005
Apr 27th, 2006
0

Re: Java Encryption error

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Swing Button question
Next Thread in Java Forum Timeline: Hangman classes not working at home.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC