在JSP中實現DES加密解密的例子
DES.javapackage?Lion.Security.Cryptography;
import?javax.crypto.Cipher;
import?javax.crypto.KeyGenerator;
import?javax.crypto.NoSuchPaddingException;
import?javax.crypto.SecretKey;
import?java.security.NoSuchAlgorithmException;
import?java.security.Security;
/**
?*?DES加密的,文件中共有兩個方法,加密、解密
?*?@author?Lion
?*?@author?www.lionsky.net
?*/
public?class?DES?{
????private?String?Algorithm?=?"DES";//加密算法的名稱
????private?KeyGenerator?keygen;//密鑰生成器
????private?SecretKey?deskey;//密鑰
????private?Cipher?c;//密碼器
????private?byte[]?cipherByte;
????/**
?????*?初始化?DES?實例
?????*/
????public?DES()?{
??????????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();
????????}
???????}
????/**
?????*?對?String?進行加密
?????*?@param?str?要加密的數據
?????*?@return?返回加密后的?byte?數組
?????*/
?????public?byte[]?createEncryptor(String?str)?{
????????try?{
?????????????c.init(Cipher.ENCRYPT_MODE,?deskey);//初始化密碼器,用密鑰deskey,進入加密模式
?????????????cipherByte?=?c.doFinal(str.getBytes());//加密
????????}
????????catch(java.security.InvalidKeyException?ex){
????????????ex.printStackTrace();
????????}
????????catch(javax.crypto.BadPaddingException?ex){
????????????ex.printStackTrace();
????????}
????????catch(javax.crypto.IllegalBlockSizeException?ex){
????????????ex.printStackTrace();
????????}
????????return?cipherByte;
?????}
????/**
?????*?對?Byte?數組進行解密
?????*?@param?buff?要解密的數據
?????*?@return?返回加密后的?String
?????*/
?????public?String?createDecryptor(byte[]?buff)?{
????????try?{
???????????c.init(Cipher.DECRYPT_MODE,?deskey);//初始化密碼器,用密鑰deskey,進入解密模式
???????????cipherByte?=?c.doFinal(buff);
????????}
????????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));
?????}
}
?
DES.jsp
<%@?page?contentType="text/html;?charset=gb2312"?%>
<jsp:useBean?id="DES"?scope="page"?class="Lion.Security.Cryptography.DES"?/>
<html>
<head><title>DES?File</title></head>
<body?bgcolor="#FFFFFF">
<div?align="center"><center>
<%
String?Test?=?request.getParameter("Test");
if(Test==null?||?Test.equals(""))?{
%>
????<form?name="form"?method="post">
????<input?type="text"?name="Test"?size="25"?value=""/>
????<input?type="submit"?name="button"?value="?確定?"/>
????</form>
????<%
}else{
????????????out.println("加密前的數據:"+Test?+"<br/>");
????????????out.println("加密后的數據:"+DES.createEncryptor(Test)?+"<br/>");
????????????out.println("解密后的數據:"+DES.createDecryptor(DES.createEncryptor(Test))?+"<br/>");
??????}
????%>
</center></div>
</body>
</html>
posted on 2006-04-13 17:40 都市淘沙者 閱讀(1451) 評論(2) 編輯 收藏 所屬分類: JSP/PHP