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){
            //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) {

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.

This article has been dead for over six months. Start a new discussion instead.