Search code examples
javajspencryptionxor

Using Xor to encode and decode


I am attempting to make a JSP that takes text from the text area and depending on whether you select encode or decode, will encode your text or decode the encoded text. The encode part works, but the decode option throws

org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "".

Here is my code:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Xorcoder</h1>
    <form method="post">
        <label>Key</label><input type="text" name="key" />
        <p>Encode<input type="radio" name="option" value="encode" /></p>
        <p>Decode<input type="radio" name="option" value="decode" /></p>
        <p><textarea name="txt" style="width:400px;height:200px" >
            <%String txt = "";
                int key;
                String option = "";

                if(request.getParameter("txt") != null) {
                    txt = request.getParameter("txt");
                }

                if(request.getParameter("key") != null) {
               key = Integer.parseInt(request.getParameter("key"));
                }
                else {
                    key = 0;
                }

                  if(request.getParameter("option") != null) {  
                option = request.getParameter("option");
                  }

                   char temp;

                   int[] array = new int[(txt.length())];

                      if(option.equals("encode")) {
                      for(int i = 0; i < array.length; i++) {
                       temp = txt.charAt(i);
                       array[i] = temp^key;
                      }


                      for(int i = 0; i < array.length; i++) 
                        out.print(array[i] + " ");
                      }

                      else if(option.equals("decode")){
                      String[] array2 = txt.split(" ");
                      int temp2;

                      for(int i = 0; i < array2.length; i++) {
                          temp2 = Integer.parseInt(array2[i]);
                          temp2 = temp2^key;
                          out.print((char)temp2);                              
                       }                                               
                   }

                      %></textarea></p>
        <p><input type="submit" value="Press" /></p>
        <p><input type="reset" value="Clear" /></p>

    </form>

</body>
</html>

Solution

  • The problem starts here:

      for (int i = 0; i < array.length; i++) 
        out.print(array[i] + " ");
      }
    

    This outputs the array with a space after each number. Not between each number.

    You then split that string like this:

      String[] array2 = txt.split(" ");
    

    and (unsurprisingly) the last element of the array is going to be an empty string.

    Solutions:

    1. Don't output the final space in the first place.

    2. Trim the string before splitting

    3. Check that the strings are non-empty before calling parseInt.

    (You don't need to check for null. The spec for split guarantees that there will be no nulls in the array ...)