侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

DES加密及解密类

2024-05-12 星期日 / 0 评论 / 0 点赞 / 95 阅读 / 8537 字

package com.ai.des; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.cryp

package com.ai.des;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * DES加密及解密类,用于对密文进行加密解密操作
 * @author zhugl
 *
 */
public class DesHelper {
    
    private String key;
    private DESKeySpec desKey;
    private SecretKey secretKey;
    private SecureRandom secureRandom;
    private static final String DEFAULT_KEY = "123qwe!@#QWE";
    
    public DesHelper(String key){
        this.key = key;
        try {
            this.init(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public DesHelper(){
        this.key = DesHelper.DEFAULT_KEY;
        try {
            this.init(key);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 初始化密钥信息
     * @param key
     * @throws Exception
     */
    private void init(String key) throws Exception{
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        this.desKey = new DESKeySpec(key.getBytes());
        this.secretKey = keyFactory.generateSecret(desKey);
        this.secureRandom = new SecureRandom();
    }
    
    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
    
    /**
     * 加密明文,返回字节数组
     * @param text
     * @return
     * @throws Exception
     */
    public byte[] enc(String text) throws Exception{
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, this.secretKey, this.secureRandom);
        return cipher.doFinal(text.getBytes());
    }
    
    /**
     * 加密密文,返回16进制字符串
     * @param text
     * @return
     * @throws Exception
     */
    public String encString(String text) throws Exception{
        return this.byte2String(this.enc(text));
    }
    
    /**
     * 解密密文,返回字节数组
     * @param data
     * @return
     * @throws Exception
     */
    public byte[] dec(byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, this.secretKey, this.secureRandom);
        return cipher.doFinal(data);
    }
    
    /**
     * 解密16进制字符串密文,返回字符串
     * @param text
     * @return
     * @throws Exception
     */
    public String decString(String text) throws Exception{
        return new String(this.dec(this.string2Byte(text)));
    }
    
    /**
     * 将字节转换为16进制字符串
     * @param data
     * @return
     */
    public String byte2String(byte[] data){
        StringBuffer buffer = new StringBuffer();
        for(byte b:data){
            int ch = b&0xff;
            String x = Integer.toHexString(ch);
            if(x.length()==1){
                buffer.append("0");
            }
            buffer.append(x);
        }
        return buffer.toString();
    }
    
    /**
     * 将16进制字符串转换为字节数组
     * @param text
     * @return
     */
    public byte[] string2Byte(String text){
        int size = text.length()/2;
        byte[] data = new byte[size];
        for(int i=0;i<size;i++){
            int start = i*2;
            int end = start+2;
            String x = text.substring(start, end);
            int ch = Integer.parseInt(x,16);
            data[i] = (byte)ch;
        }
        return data;
    }
    
    public static void main(String[] args){
        DesHelper desHelper = new DesHelper();
        try {
//            byte[] data = desHelper.enc("123qwe!@#");
//            String xx = desHelper.byte2String(data);
//            System.out.println(xx);
//            String yy = new String(desHelper.dec(desHelper.string2Byte(xx)));
//            System.out.println(yy);
            String text = "中华人民共和123qwe!@#QWE";
            String xx = desHelper.encString(text);
            System.out.println(xx);
            String yy = desHelper.decString(xx);
            System.out.println(yy);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
 

广告 广告

评论区