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();
 
            }
                }
    }

Dani AI

Generated

— the compile errors come from two simple control-flow problems and a couple of practical mistakes; correctly pointed out the immediate cause.

The technical problems

  • There is a return inside the try so any code after that line is unreachable.
  • If an exception is thrown you fall into the catch and never return anything, so the compiler reports a missing return.
  • encrypted.toString() does not produce the encrypted bytes as text (it prints an object id).
  • Generating a new random key every time means you cannot decrypt later — the key must be stable (or stored) if you expect reversible encryption.

How to fix (practical pattern)

  • Split encryption and decryption into separate methods, or return the encrypted value at the end of the method (not in the middle).
  • Return a printable encoding of the byte[] (Base64 or hex) so the result is a usable String.
  • Either declare the method throws the security exceptions and let the caller handle them, or ensure every control path returns a value (avoid swallow-only catch blocks).

Example encryption/decryption pattern (use a stable SecretKey and Base64):

import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;

public static String encrypt(String plaintext, SecretKey key) throws GeneralSecurityException {
    Cipher c = Cipher.getInstance("Blowfish");
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] out = c.doFinal(plaintext.getBytes(java.nio.charset.StandardCharsets.UTF_8));
    return Base64.getEncoder().encodeToString(out);
}

public static String decrypt(String base64, SecretKey key) throws GeneralSecurityException {
    byte[] enc = Base64.getDecoder().decode(base64);
    Cipher c = Cipher.getInstance("Blowfish");
    c.init(Cipher.DECRYPT_MODE, key);
    return new String(c.doFinal(enc), java.nio.charset.StandardCharsets.UTF_8);
}

Security note
If the goal is password storage, do not use reversible encryption — use a salted, slow hash (PBKDF2WithHmacSHA256, bcrypt or Argon2) and store salt+hash. If reversible encryption is needed, persist the key securely (Java KeyStore) and prefer an authenticated cipher (AES/GCM) over older ciphers.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.