加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

利用3DES实现加密解密的Java代码

发布时间:2020-12-15 03:20:57 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 采用ECB,补位模式为:PKCS5 import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec; /** * 项目名称:

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

采用ECB,补位模式为:PKCS5
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
 
/**
 * 项目名称:****    
 * 类名称:ThreeDES    
 * 类描述: 3des 加密工具类        
 * @version  1.0   
 *
 */
public class ThreeDES {
     
   //key 根据实际情况对应的修改
   private final byte[] keybyte="123456788765432112345678".getBytes(); //keybyte为加密密钥,长度为24字节
   private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish
   private SecretKey deskey;
   ///生成密钥
   public ThreeDES(){
       deskey = new SecretKeySpec(keybyte,Algorithm);
   }
   //加密
   public byte[] encrypt(byte[] data){
        try {
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE,deskey);
            return cipher.doFinal(data);
        } catch (Exception ex) {
            //加密失败,打日志
            ex.printStackTrace();
        } 
        return null;
   }
   //解密
   public byte[] decrypt(byte[] data){
       try {
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE,deskey);
            return cipher.doFinal(data);
        } catch (Exception ex) {
            //解密失败,打日志
            ex.printStackTrace();
        } 
        return null;
   }
    
    
   public static void main(String[] args) throws Exception {
       ThreeDES des=new ThreeDES();
       String req ="135|一二|10100001|410883198501285014|||||1|2|";
//       String req1 ="<?xml version="1.0" encoding="UTF-8"?><service><head><field name="spdtrancode">22222</field><field name="spdtranserno">CM0172061308908f4dc995222321f6ecb888</field><field name="spdressys">2001</field><field name="spdresdate">20141223</field><field name="spdrestime">153606</field><field name="spdtranstate">S</field><field name="spderrocode"></field><field name="spderrormsg"></field></head><body><field name="spdcheckrslt">000000</field></body></service>";
        
       String toreq  =toHexString(req);
       System.err.println("十六进制报文===="+toreq);
       byte [] srcData=req.toString().getBytes("GBK");
       byte[] encryptData=des.encrypt(srcData);
       System.out.println("密文:");
       if(encryptData!=null){
           for(int i=0;i<encryptData.length;i++){
               String hex=Integer.toHexString(encryptData[i]);
               if(hex.length()>1)
                System.out.print(hex.substring(hex.length()-2)+" ");
               else
                System.out.print("0"+hex+" ");
           }
       }
       System.out.println("");
       System.out.println("明文:");
   }
    
   // 转化字符串为十六进制编码
   public static String toHexString(String s) {
       String str = "";
       for (int i = 0; i < s.length(); i++) {
           int ch = (int) s.charAt(i);
           String s4 = Integer.toHexString(ch);
           str = str + s4;
       }
       return str;
   }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读