使用 Xor 进行编解码
Using Xor to encode and decode
我正在尝试制作一个 JSP 从文本区域获取文本,并根据您 select 编码或解码,对您的文本进行编码或对编码文本进行解码。编码部分有效,但解码选项抛出
org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "".
这是我的代码:
<%@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>
java.lang.NumberFormatException
告诉你 ""
不是数字。问题出在您对 Integer.parseInt
的使用上。在使用 Integer.parseInt(...)
之前请检查该元素的输入是否不为空(并且不为空,仅当参数可以为空时才检查此条件,否则不需要)。如果它为空,Integer.parseInt
将抛出该错误。
问题从这里开始:
for (int i = 0; i < array.length; i++)
out.print(array[i] + " ");
}
这会输出每个数字后带有 space 的数组。不是每个数字之间。
然后你像这样分割那个字符串:
String[] array2 = txt.split(" ");
并且(毫不奇怪)数组的最后一个元素将是一个空字符串。
解决方案:
不要一开始就输出最后的space
Trim拆分前的字符串
调用parseInt
前检查字符串是否为空。
(您不需要检查 null
。split
的规范保证数组中不会有空值...)
希望您解决难题所需要做的就是在代码的最后一个循环中添加 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);
}
另外我想告诉大家,在 <%、%> 等标签之间没有任何 space和其他 html 标签使您的文本区域更干净一些。我的意思是,尝试使用
<textarea name="txt" style="width:400px;height:200px" ><%
而不是
<textarea name="txt" style="width:400px;height:200px" >
<%
这样您的文本区域中就不会出现任何无用的 space 个字符。
祝你好运:)
我正在尝试制作一个 JSP 从文本区域获取文本,并根据您 select 编码或解码,对您的文本进行编码或对编码文本进行解码。编码部分有效,但解码选项抛出
org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "".
这是我的代码:
<%@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>
java.lang.NumberFormatException
告诉你 ""
不是数字。问题出在您对 Integer.parseInt
的使用上。在使用 Integer.parseInt(...)
之前请检查该元素的输入是否不为空(并且不为空,仅当参数可以为空时才检查此条件,否则不需要)。如果它为空,Integer.parseInt
将抛出该错误。
问题从这里开始:
for (int i = 0; i < array.length; i++)
out.print(array[i] + " ");
}
这会输出每个数字后带有 space 的数组。不是每个数字之间。
然后你像这样分割那个字符串:
String[] array2 = txt.split(" ");
并且(毫不奇怪)数组的最后一个元素将是一个空字符串。
解决方案:
不要一开始就输出最后的space
Trim拆分前的字符串
调用
parseInt
前检查字符串是否为空。
(您不需要检查 null
。split
的规范保证数组中不会有空值...)
希望您解决难题所需要做的就是在代码的最后一个循环中添加 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);
}
另外我想告诉大家,在 <%、%> 等标签之间没有任何 space和其他 html 标签使您的文本区域更干净一些。我的意思是,尝试使用
<textarea name="txt" style="width:400px;height:200px" ><%
而不是
<textarea name="txt" style="width:400px;height:200px" >
<%
这样您的文本区域中就不会出现任何无用的 space 个字符。
祝你好运:)