tags:

views:

1877

answers:

6
import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.io.*;


public class EncryptURL extends JApplet implements ActionListener {
  Container content;
  JTextField userName = new JTextField();
     JTextField firstName = new JTextField();
      JTextField lastName = new JTextField();
      JTextField email = new JTextField();
      JTextField phone = new JTextField();
      JTextField heartbeatID = new JTextField();
      JTextField regionCode = new JTextField();
      JTextField retRegionCode = new JTextField();
      JTextField encryptedTextField = new JTextField();

    JPanel finishPanel = new JPanel();


  public void init() {
    //setTitle("Book - E Project");
    setSize(800,600);
    content = getContentPane();
    content.setBackground(Color.yellow);
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));


    JButton submit = new JButton("Submit");

    content.add(new JLabel("User Name"));
    content.add(userName);

    content.add(new JLabel("First Name"));
    content.add(firstName);

    content.add(new JLabel("Last Name"));
    content.add(lastName);

    content.add(new JLabel("Email"));
    content.add(email);

    content.add(new JLabel("Phone"));
    content.add(phone);

    content.add(new JLabel("HeartBeatID"));
    content.add(heartbeatID);

    content.add(new JLabel("Region Code"));
    content.add(regionCode);

    content.add(new JLabel("RetRegionCode"));
    content.add(retRegionCode);

    content.add(submit);

    submit.addActionListener(this);


  }

  public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand() == "Submit"){

     String subUserName = userName.getText();
     String subFName = firstName.getText();
     String subLName = lastName.getText();
     String subEmail = email.getText();
     String subPhone = phone.getText();
     String subHeartbeatID = heartbeatID.getText();
     String subRegionCode = regionCode.getText();
     String subRetRegionCode = retRegionCode.getText();

     String concatURL = "user="+ subUserName + "&f="+ subFName + "&l=" +subLName+ "&em=" + subEmail + "&p="+subPhone+"&h="+subHeartbeatID+"&re="+subRegionCode+ "&ret=" + subRetRegionCode;
     concatURL = padString(concatURL, ' ', 16);
     byte[] encrypted = encrypt(concatURL);
     String encryptedString = bytesToHex(encrypted);
     content.removeAll();
     content.add(new JLabel("Concatenated User Input -->" + concatURL));

     content.add(encryptedTextField);
     setContentPane(content);
    }
  }

  public static byte[] encrypt(String toEncrypt) throws Exception{
    try{
      String plaintext = toEncrypt;
      String key = "01234567890abcde";
      String iv = "fedcba9876543210";

      SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
      IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

      Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
      cipher.init(Cipher.ENCRYPT_MODE,keyspec,ivspec);
      byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());

      return encrypted;
    }
    catch(Exception e){

    }

  }

  public static byte[] decrypt(byte[] toDecrypt) throws Exception{
      String key = "01234567890abcde";
      String iv = "fedcba9876543210";

      SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
      IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

      Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
      cipher.init(Cipher.DECRYPT_MODE,keyspec,ivspec);
      byte[] decrypted = cipher.doFinal(toDecrypt);

      return decrypted;
  }
  public static String bytesToHex(byte[] data) {
    if (data==null)
    {
      return null;
    }
    else
    {
      int len = data.length;
      String str = "";
      for (int i=0; i<len; i++)
      {
        if ((data[i]&0xFF)<16)
          str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);
        else
          str = str + java.lang.Integer.toHexString(data[i]&0xFF);
      }
      return str;
    }
  }
  public static String padString(String source, char paddingChar, int size)
  {
    int padLength = size-source.length()%size;
    for (int i = 0; i < padLength; i++) {
      source += paddingChar;
    }
    return source;
  }
}

I'm getting an unreported exception

java.lang.Exception; must be caught or declared to be thrown
byte[] encrypted = encrypt(concatURL);

as well as

.java:109: missing return statement

Can anyone help me with solving these problems?

+2  A: 

The first error

java.lang.Exception; must be caught or declared to be thrown byte[] encrypted = encrypt(concatURL);

means that your encrypt method throws an exception that is not being handled or declared by the actionPerformed method where you are calling it. Read all about it at the Java Exceptions Tutorial.

You have a couple of choices that you can pick from to get the code to compile.

  • You can remove throws Exception from your encrypt method and actually handle the exception inside encrypt.
  • You can remove the try/catch block from encrypt and add throws Exception and the exception handling block to your actionPerformed method.

It's generally better to handle an exception at the lowest level that you can, instead of passing it up to a higher level.

The second error just means that you need to add a return statement to whichever method contains line 109 (also encrypt, in this case). There is a return statement in the method, but if an exception is thrown it might not be reached, so you either need to return in the catch block, or remove the try/catch from encrypt, as I mentioned before.

Bill the Lizard
A: 

In actionPerformed(ActionEvent e) you call encrypt(), which is declared to throw Exception. However, actionPerformed neither catches this Exception (with try/catch around the call to encrypt()) nor declares that it throws Exception itself.

Your encrypt method, however, does not truly throw Exception. It swallows all Exceptions without even as much as logging a complaint. (Bad practice and bad style!)

Also, your encrypt method does the following:

public static byte[] encrypt(String toEncrypt) throws Exception {
  try{
    ....
    return encrypted; // HERE YOU CORRECTLY RETURN A VALUE
  } catch(Exception e) {
  }
  // YOU DO NOT RETURN ANYTHING HERE
}

That is, if you do catch any Exception, you discard it silently and then fall off the bottom of your encrypt method without actually returning anything. This won't compile (as you see), because a method that is declared to return a value must either return a value or throw an Exception for every single possible code path.

Eddie
A: 

In your 'encrypt' method, you should either get rid of the try/catch and instead add a try/catch around where you call encrypt (inside 'actionPerformed') or return null inside the catch within encrypt (that's the second error.

AgileJon
+1  A: 

All your problems derive from this

byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
return encrypted;

Which are enclosed in a try, catch block, the problem is that in case the program found an exception you are not returning anything. Put it like this (modify it as your program logic stands):

public static byte[] encrypt(String toEncrypt) throws Exception{
    try{
        String plaintext = toEncrypt;
        String key = "01234567890abcde";
        String iv = "fedcba9876543210";

        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE,keyspec,ivspec);
        byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());

        return encrypted;
    } catch(Exception e){
        return null;            // Always must return something
    }
}

For the second one you must catch the Exception from the encrypt method call, like this (also modify it as your program logic stands):

public void actionPerformed(ActionEvent e)
  .
  .
  .
    try {
        byte[] encrypted = encrypt(concatURL);
        String encryptedString = bytesToHex(encrypted);
        content.removeAll();
        content.add(new JLabel("Concatenated User Input -->" + concatURL));

        content.add(encryptedTextField);
    setContentPane(content);
    } catch (Exception exc) {
        // TODO: handle exception
    }
}

The lessons you must learn from this:

  • A method with a return-type must always return an object of that type, I mean in all possible scenarios
  • All checked exceptions (those which extends Exception class) must always be handled
victor hugo
A: 

You'll need to decide how you'd like to handle exceptions thrown by the encrypt method.

Currently, encrypt is declared with throws Exception - however, in the body of the method, exceptions are caught in a try/catch block. I recommend you either:

  • remove the throws Exception clause from encrypt and handle exceptions internally (consider writing a log message at the very least); or,
  • remove the try/catch block from the body of encrypt, and surround the call to encrypt with a try/catch instead (i.e. in actionPerformed).

Regarding the compilation error you refer to: if an exception was thrown in the try block of encrypt, nothing gets returned after the catch block finishes. You could address this by initially declaring the return value as null:

public static byte[] encrypt(String toEncrypt) throws Exception{
  byte[] encrypted = null;
  try {
    // ...
    encrypted = ...
  }
  catch(Exception e){
    // ...
  }
  return encrypted;
}

However, if you can correct the bigger issue (the exception-handling strategy), this problem will take care of itself - particularly if you choose the second option I've suggested.

harto
A: 
OscarRyz