在eclipse环境下实现简单的表单提交(servlet)
程序员文章站
2022-05-30 16:40:39
...
servlet做的。正好java老师上课讲了一点,回来复现了一下。
实现的是从html提交表单,java收到并进行RSA加密的相关计算。
重点是提醒自己的form表单里的action填写。(两个文件在同一包下)
html文件里的ajax方法是未完成的
servlet_java.java文件
package com.example.demo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Random;
public class servlet_java extends HttpServlet {
private static final long serialVersionUID = 1L;
public static String obvtext;
public static String ciphertext;
static Random ra = new Random();
public servlet_java() {
super();
// TODO Auto-generated constructor stub
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=UTF-8");
response.getWriter().append("Served at: ").append(request.getContextPath());
PrintWriter out = response.getWriter();
obvtext = request.getParameter("obvtext");
ciphertext = request.getParameter("ciphertext");
BigInteger p = new BigInteger("106697219132480173106064317148705638676529121742557567770857687729397446898790451577487723991083173010242416863238099716044775658681981821407922722052778958942891831033512463262741053961681512908218003840408526915629689432111480588966800949428079015682624591636010678691927285321708935076221951173426894836169");
BigInteger q = new BigInteger("144819424465842307806353672547344125290716753535239658417883828941232509622838692761917211806963011168822281666033695157426515864265527046213326145174398018859056439431422867957079149967592078894410082695714160599647180947207504108618794637872261572262805565517756922288320779308895819726074229154002310375209");
RSA rsa = new RSA();
// ���ɹ�Կ˽Կ'''
// pubkey, selfkey = gen_key(p, q)
//���������P,Q
System.out.println("��һ��----------------------------------------------------");
System.out.println("");
System.out.println("素数p为:" + q);
System.out.println("素数q为:" + p);
BigInteger[][] keys = rsa.genKey(p, q); //公钥对为,n,e��n,d
BigInteger[] pubkey = keys[0];
//System.out.println("Keys[0]:"+(pubkey[0]));
BigInteger[] selfkey = keys[1];
//System.out.println("Keys[1]:"+(pubkey[1]));
//System.out.println("N=(q-1)*(p-1),NΪ:"+pubkey[0]);
System.out.println();
BigInteger m = new BigInteger("1");//����
/* System.out.println("������Aliceѡȡ������M:");
String contents2=input.next(); //������ĸ���� */
//ת��Ϊm
String contents2 = obvtext;
String mid = change.stringToAscii(contents2);
m = new BigInteger("" + mid);
//m�Ѿ�ת�����˴�����
System.out.println("Alice选择的明文M为:" + m);
System.out.println();
BigInteger c = rsa.encrypt(m, pubkey); //���ܼ���õ�����C!!!!!!!!!!!!!
BigInteger r = new BigInteger("1");//��ʼ��
System.out.println("Trudy开始攻击---------------------------------");
/*
System.out.println("������Trudyѡ���rΪ:");
String contents3=input.next(); //������ĸ���� */
//ת��Ϊm
String contents3 = ciphertext;
String mid1 = change.stringToAscii(contents3);
r = new BigInteger("" + mid1);
System.out.println("Trudy选择的r为:" + c);
System.out.println();
System.out.println("Alice开始解密------------------------------");
System.out.println("C'=Cr^e mod N");
BigInteger C1 = new Exponentiation().expModemul(r, pubkey[1], c, p.multiply(q));
System.out.println(" C'=Cr^e mod N,解得c'为=" + C1);
BigInteger Mr = rsa.decrypt(C1, selfkey); //���ܼ���õ�����Mr!!!!!!!!!!!!!!!!
//System.out.println(" C'=Cr^e mod N,����C'^d=(Mr)^d,����d="+selfkey[1]);
System.out.println("C'=Cr^e mod N,又有C'^d=(Mr)^de,解得Mr为:" + Mr);
System.out.println("");
System.out.println("Alice发送Mr给Trudy---------------------------");
BigInteger M = Mr.divide(r);
String last = change.asciiToString(M.toString());
System.out.println("Trudy知道r,又知道Mr,由M=Mr/r,解得明文M为:" + last);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
public BigInteger[][] genKey(BigInteger p, BigInteger q) {
BigInteger n = p.multiply(q);
System.out.println("N=(q-1)*(p-1),NΪ:" + n);
BigInteger fy = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger e = new BigInteger("3889");//ѡȡ�˹�Կe,��֪��
// generate d
BigInteger a = e;
BigInteger b = fy;
BigInteger[] rxy = new gcd().extGcd(a, b);//����õ�d��
BigInteger r = rxy[0];
BigInteger x = rxy[1];
BigInteger y = rxy[2];
BigInteger d = x;
System.out.println("��ԿeΪ:" + e);
System.out.println("˽ԿdΪ:" + d);
// ��Կ ˽Կ
return new BigInteger[][]{{n, e}, {n, d}};
}
public BigInteger encrypt(BigInteger m, BigInteger[] pubkey) { //����
BigInteger n = pubkey[0];
BigInteger e = pubkey[1];
BigInteger c = new Exponentiation().expMode(m, e, n);
return c;
}
public BigInteger decrypt(BigInteger c, BigInteger[] selfkey) { //����
BigInteger n = selfkey[0];
BigInteger d = selfkey[1];
BigInteger m = new Exponentiation().expMode(c, d, n);
return m;
}
//��ʮ����ת���ɶ�����
public static int shier(int i) {
int num = i;
int a = 0;//����һ��int��������
int b = 0;
int c = 0;
while (num != 0) {
a = num % 2;
num = num / 2;
b++;
for (int i1 = 0; i1 < b; i1++) {
a = a * 10;
}
c += a;
}
return c;
}
@SuppressWarnings("unused")
private static BigInteger pow(int intValue) {
// TODO Auto-generated method stub
return null;
}
@SuppressWarnings("unused")
private static int multiply(BigInteger r, BigInteger r2) {
// TODO Auto-generated method stub
return 0;
}
@SuppressWarnings("unused")
private static int intValue(BigInteger bigInteger) {
// TODO Auto-generated method stub
return 0;
}
}
test.html
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<meta charset="UTF-8">
<title>RSA算法</title>
<style type="text/css">
p {
text-align: center;
font-family: 微软雅黑;
font-weight: bold;
}
span {
text-align: center;
}
div {
width: 220px;
margin-top: 260px;
margin-bottom: 100px;
margin-left: 40px;
margin-right: 40px;
}
</style>
<script type="text/javascript">
var request;
function Dom() {
var mtext = document.form1.obvtext.value;
var ctext = document.form1.ciphertext.value;
if (mtext == "") {
alert("明文不可为空");
}
if (mtext != "" && ctext == "") {
alert("密文不可为空");
}
document.getElementById("result").innerHTML = "<p>Alice选取的明文为:</p>" + mtext + "<br />" + "<p>Trudy选取的r为:</p>" + ctext + "<br />";
}
document.forms[0].target = "rfFrame";
function ajax_submit() {
$.ajax({
async: false, //表示请求是否异步处理
contentType:'application/x-www-form-urlencoded;charset=utf-8',
type: "post", //请求类型
url: "/return_text",//请求的 URL地址
dataType: "json",//返回的数据类型
data: "",
success: function (data) {
console.log(data); //在控制台打印服务器端返回的数据
},
error: function (data) {
alert("error" + data);
}
});
}
</script>
</head>
<body>
<!-- 初始按钮 -->
<!-- 信息输出框 随输出改变大小-->
<div style="width:auto; display:inline-block !important; display:inline;float:left;border:solid #4c4c4c">
<p>N,p,q等参数输出:</p>
</div>
<!-- 提交框 -->
<div style="float:left;border:solid #4c4c4c ;height:266px;margin-top: 210px ;">
<form name="form1" style="text-align: center;" target="rfFrame" method="post" action="package名字/servlet_java">
<iframe id="rfFrame" name="rfFrame" src="about:blank" style="display:none;"></iframe>
<p style="">Alice进行加密</p>
<span>请输入Alice选取的明文M:</span>
<input type="text" name="obvtext" id="otext" style="margin-top: 20px">
<p>Trudy进行攻击</p>
<span>请输入Trudy选取的r:</span>
<input type="text" name="ciphertext" id="ctext" style="margin-top: 20px;">
<input type="submit" type="button" value="提交" style="margin-top:6px;display: flex;margin-left: 86px;"
onClick="Dom();ajax_submit()">
</form>
</div>
<!-- 信息输出框 -->
<div style="height:142px;float:left;border:solid #4c4c4c;text-align: center;">
<span style="font-size: 14px" id="result"></span>
</div>
</body>
</html>