利用JAVA进行DES数据加密

JSP利用DEC加密解密方法案例

DES.java


package 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);
              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);
            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>

 

JAVA中进行加密解密函数

Server.UrlEncode(str)
Server.UrlDecode(str)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值