简体   繁体   English

使用Xor进行编码和解码

[英]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. 我正在尝试创建一个JSP,它从文本区域获取文本,并根据您是选择编码还是解码,将编码您的文本或解码编码的文本。 The encode part works, but the decode option throws 编码部分工作,但解码选项抛出

org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "". org.apache.jasper.JasperException:java.lang.NumberFormatException:对于输入字符串:“”。

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>

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 . 在调用parseInt之前检查字符串是否为空。

(You don't need to check for null . The spec for split guarantees that there will be no nulls in the array ...) (您不需要检查nullsplit规范保证数组中没有空值...)

java.lang.NumberFormatException tells you that "" is not a number. java.lang.NumberFormatException告诉你""不是数字。 Problem is with you usage of Integer.parseInt . 问题在于您使用Integer.parseInt Before using Integer.parseInt(...) please check if the input of that element is not empty (and not null, check this condition only if the parameter can be null, otherwise not required). 在使用Integer.parseInt(...)之前,请检查该元素的输入是否为空(并且不为null,仅在参数可以为null时检查此条件,否则不需要)。 If it is empty, Integer.parseInt will throw that error. 如果为空,则Integer.parseInt将抛出该错误。

Hopefully, all you need to do to solve your conundrum is to add an if statement into the last loop in your code: 希望您解决难题所需要做的就是在代码的最后一个循环中添加一个if语句:

for(int i = 0; i < array2.length; i++) {
   if (array2[i].isEmpty()) //checks whether the length of string is 0
      continue; //skips current iteration and moves further
   temp2 = Integer.parseInt(array2[i]);
   temp2 = temp2^key;
   out.print((char)temp2);                              
} 

Also I'd like to tell that not having any space between such tags as <% , %> and other html tags makes your textarea somewhat cleaner. 另外我想告诉我们, <%%>和其他html标签之类的标签之间没有任何空格可以让你的textarea更清洁。 I mean, try to use 我的意思是,尝试使用

   <textarea name="txt" style="width:400px;height:200px" ><%

Instead of 代替

   <textarea name="txt" style="width:400px;height:200px" >
      <%

so that you will not have any useless space characters in your textarea. 这样你的textarea就不会有任何无用的空格字符了。

Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM