附运用方法:
<%--TestDES.jsp--%> <%@ page contentType="text/html; charset=gb2312" %>
<jsp:useBean id="DES" scope="page" class="com.pechinsoft.util.WriteOffDES" />
<html>
<head><title>TestDES File</title></head> <body bgcolor="#FFFFFF">
<div align="center"><center>
<%
String Num = request.getParameter("Num");
if(Num==null || Num.equals("")) {
%>
<form name="form" action="TestDES.jsp" method="post">
<input type="text" name="Num" size="25" value=""/>
<input type="submit" name="button" value=" 确定 "/>
</form>
<%
}else{
out.println("加密前的数据:"+Num +"<br/>");
out.println("加密后的数据:"+DES.encrypt(Num) +"<br/>");
out.println("解密后的数据:"+DES.decrypt(DES.encrypt(Num)) +"<br/>");
}
%>
</center></div>
</body>
</html>
package com.pechinsoft.util; import java.security.*;
import javax.crypto.*; public class WriteOffDES {
private String Algorithm = "DES"; public WriteOffDES() {
init();
} public void init() {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
keygen = KeyGenerator.getInstance(Algorithm);
deskey = keygen.generateKey();
c = Cipher.getInstance(Algorithm);
}
catch(NoSuchAlgorithmException ex) {ex.printStackTrace();}
catch(NoSuchPaddingException ex) {ex.printStackTrace();}
} public byte[] encrypt(String e) {
try {
c.init(Cipher.ENCRYPT_MODE, deskey);
cipherByte = c.doFinal(e.getBytes());
}
catch(java.security.InvalidKeyException ex){ex.printStackTrace();}
catch(javax.crypto.BadPaddingException ex){ex.printStackTrace();}
catch(javax.crypto.IllegalBlockSizeException ex){ex.printStackTrace();} return cipherByte;
} public String decrypt(byte[] d) {
try {
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(d);
}
catch(java.security.InvalidKeyException ex){ex.printStackTrace();}
catch(javax.crypto.BadPaddingException ex){ex.printStackTrace();}
catch(javax.crypto.IllegalBlockSizeException ex){ex.printStackTrace();} return (new String(cipherByte));
} public String byteTohex(byte[] b) {
String str = "";
String stmp = "";
for(int n=0;n<b.length;n++) {
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if(stmp.length()==1) str = str + "0" + stmp;
else str = str + stmp;
if(n<b.length-1) str = str + ":";
}
return str.toUpperCase();
} private KeyGenerator keygen;
private SecretKey deskey;
private Cipher c;
private byte[] cipherByte;
}
反编译看看吧,我估计问题出在这里
Key是标准类库,实现了Seriable(好像拼错了)接口。