excelSecurity.jsp
3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="weaver.general.Util" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="org.bouncycastle.crypto.BufferedBlockCipher" %>
<%@ page import="org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher" %>
<%@ page import="org.bouncycastle.crypto.modes.CBCBlockCipher" %>
<%@ page import="org.bouncycastle.crypto.engines.AESFastEngine" %>
<%@ page import="org.bouncycastle.crypto.params.ParametersWithIV" %>
<%@ page import="org.bouncycastle.util.encoders.Hex" %>
<%@ page import="org.bouncycastle.crypto.params.KeyParameter" %>
<%@ page import="weaver.file.WeaverCrypto" %>
<jsp:useBean id="RecordSet" class="weaver.conn.RecordSet" scope="page" />
<%!
static byte[] keybytes = "WEAVER E-DESIGN.".getBytes();
static byte[] iv = "weaver e-design.".getBytes();
/**
*为字符串加密
*
* @param content
* @return
*/
public String encode(String content) {
try {
BufferedBlockCipher engine = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new AESFastEngine()));
engine.init(true, new ParametersWithIV(new KeyParameter(keybytes),
iv));
byte[] enc = new byte[engine
.getOutputSize(content.getBytes().length)];
int size1 = engine.processBytes(content.getBytes(), 0, content
.getBytes().length, enc, 0);
int size2 = engine.doFinal(enc, size1);
byte[] encryptedContent = new byte[size1 + size2];
System.arraycopy(enc, 0, encryptedContent, 0,
encryptedContent.length);
return new String(Hex.encode(encryptedContent));
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
/**
* 为字符串解密
*
* @param content
* @return
*/
public String decode(String content) {
try {
BufferedBlockCipher engine = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new AESFastEngine()));
engine.init(true, new ParametersWithIV(new KeyParameter(keybytes),
iv));
byte[] deByte = Hex.decode(content);
engine.init(false, new ParametersWithIV(new KeyParameter(keybytes),
iv));
byte[] dec = new byte[engine.getOutputSize(deByte.length)];
int size1 = engine.processBytes(deByte, 0, deByte.length, dec, 0);
int size2 = engine.doFinal(dec, size1);
byte[] decryptedContent = new byte[size1 + size2];
System.arraycopy(dec, 0, decryptedContent, 0,
decryptedContent.length);
return new String(decryptedContent);
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
%>
<%
//设置response
response.setContentType("text/html;charset=utf-8");
String str1 = Util.null2String(request.getParameter("str1"));
String type = Util.null2String(request.getParameter("securitype"));
if(type.equals("encode")) {
String encryptKey = request.getParameter("encryptKey");
try{
encryptKey = WeaverCrypto.decryptRSAKey(encryptKey);
}catch(Exception e){
e.printStackTrace();
}
str1 = WeaverCrypto.decryptAES(str1.trim(), encryptKey.trim());//解密
str1 = encode(str1);
}else
str1 = decode(str1);
out.clear();
out.println(str1);
%>