954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Encryption error

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 :sad:

package datasource;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
 
public class Encryption {
    public static String encrypt (String password){
 
        try{
            //Get the key generator and create the key
            //
            System.out.println("Getting key generator ....");
            KeyGenerator kgen = KeyGenerator.getInstance( "Blowfish" );
 
            System.out.println("Getting key generator ....");
            SecretKey secretKey = kgen.generateKey();
            byte[] bytes = secretKey.getEncoded();
            SecretKeySpec specKey = new SecretKeySpec (bytes, "Blowfish" );
 
            // Create the cipher object //
 
            System.out.println("Creating cipher ....");
            Cipher cipher = Cipher.getInstance( "Blowfish" );
 
            System.out.println("Encrypting ...");
            cipher.init( Cipher.ENCRYPT_MODE, specKey );
        //    String target = "Encrypt Michael";
            byte[] encrypted = cipher.doFinal( password.getBytes() );
 
            return encrypted.toString();
 
            //System.out.println("before: " + target );
        //    System.out.println("after: " + new String( encrypted ) );
 
            // Decrypt
 
 
            cipher.init ( Cipher.DECRYPT_MODE, specKey );
 
 
            byte[] decrypted = cipher.doFinal ( encrypted);
            return decrypted.toString();
            //System.out.println("\nafter decrypt: " + new String( decrypted ) );
            }
            catch (Exception e) {
                e.printStackTrace();
 
            }
                }
    }
bondito
Newbie Poster
17 posts since Sep 2005
Reputation Points: 10
Solved Threads: 1
 

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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You