DES加密算法工具类
博客专区 > Curry_M 的博客 > 博客详情
DES加密算法工具类
Curry_M 发表于8个月前
DES加密算法工具类
  • 发表于 8个月前
  • 阅读 14
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

package com.pingan.common.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * DES加密算法工具类
 */
@SuppressWarnings("restriction")
public class DESUtils {
    private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish

    //keybyte为加密密钥,长度为24字节
    //src为被加密的数据缓冲区(源)
    public static String encryptMode(byte[] keybyte, byte[] src) {
        BASE64Encoder base64encoder = new BASE64Encoder();
        try {
            //生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

            //加密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            c1.doFinal(src);
            return base64encoder.encode(c1.doFinal(src));
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }

    //keybyte为加密密钥,长度为24字节
    //src为加密后的缓冲区
    public static String decryptMode(byte[] keybyte, String src) {
        try {
            BASE64Decoder base64decoder = new BASE64Decoder();
            //生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

            //解密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.DECRYPT_MODE, deskey);
            return new String(c1.doFinal(base64decoder.decodeBuffer(src)));
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }

    //转换成十六进制字符串
    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";

        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
            if (n < b.length - 1)
                hs = hs + ":";
        }
        return hs.toUpperCase();
    }

    public static void main(String[] args) {
        final byte[] keyBytes = { 0x21, 0x36, 0x4F, 0x58, (byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x55, 0x79, 0x51,
                (byte) 0xCB, (byte) 0xDD, 0x55, 0x66, 0x77, 0x19, 0x74, (byte) 0x98, 0x33, 0x55, 0x36, (byte) 0xE2 }; //24字节的密钥
        String szSrc = "12345678~!@#$%^&";

        System.out.println("加密前的字符串:" + szSrc);

        String encoded = encryptMode(keyBytes, szSrc.getBytes());
        System.out.println("加密后的字符串:" + encoded);

        String srcBytes = decryptMode(keyBytes, encoded);
        System.out.println("解密后的字符串:" + srcBytes);

        String srcBytes2 = decryptMode(keyBytes, "X+9ZZqXkvNZo+RpsuMEzVQ==");
        System.out.println("解密后的字符串2:" + srcBytes2);
    }
}

 

标签: DES加密算法
共有 人打赏支持
粉丝 2
博文 38
码字总数 40117
×
Curry_M
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: