MD5、BASE64和DES加密

MD5算法仅能进行加密,不能进行解密;
BASE64算法即可进行加密,也可进行解密;

package com.zhbr.test;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Test {
    public static void main(String[] args) throws NoSuchAlgorithmException, IOException{
        String content = "zhbr12345";

        //用MD5加密生成秘钥串
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] md5Bytes  = md.digest(content.getBytes("UTF-8"));
        BigInteger bigInt = new BigInteger(1, md5Bytes);//1代表绝对值
        String token = bigInt.toString(16);
        System.out.println(token);

        //BASE64加密
        BASE64Encoder encoder = new BASE64Encoder();
        String token1 = encoder.encode(content.getBytes());
        System.out.println(token1);

        //BASE64解密
        BASE64Decoder decoder = new BASE64Decoder();
        String token2 = new String(decoder.decodeBuffer(token1));
        System.out.println(token2);
    }
}

DES是一种使用密钥加密的块算法,加密和解密过程中要使用同一个秘钥。

package com.zhbr.test;

import javax.crypto.Cipher;
import java.security.Key;

public class DesUtil {

    /** DES密钥Key*/
    public final static String KEY = "zhbr@1234";

    private static Cipher mEncryptCipher = null;
    private static Cipher mDecryptCipher = null;


    public DesUtil() throws Exception {
        //初始化加密和解密密码提供类
        mEncryptCipher = Cipher.getInstance("DES");
        mEncryptCipher.init(Cipher.ENCRYPT_MODE,getKey(KEY.getBytes()));
        mDecryptCipher = Cipher.getInstance("DES");
        mDecryptCipher.init(Cipher.DECRYPT_MODE,getKey(KEY.getBytes()));
    }

    /**
     * 对 字符串 加密
     * */
    public String encrypt(String strIn) throws Exception {
        return byte2HexStr(encrypt(strIn.getBytes()));
    }

    /**
     * 对 字节数组 加密
     */
    public byte[] encrypt(byte[] arrB) throws Exception {
        return mEncryptCipher.doFinal(arrB);
    }

    /**
     * 解密 字符串
     * */
    public String decrypt(String strIn) throws Exception {
        return new String(decrypt(hexStr2Byte(strIn)));
    }

    /**
     * 解密 字节数组
     */
    public byte[] decrypt(byte[] arrB) throws Exception {
        return mDecryptCipher.doFinal(arrB);
    }


    /**
     * 解密用的密钥(字节数组)长度必须为8个字节否则返回null, 不足8位时后面补0,超出8位只取前8位
     *
     * @param arrBTmp 构成该字符串的字节数组
     * @return 生成的密钥
     * @throws Exception
     */
    private Key getKey(byte[] arrBTmp) throws Exception {
        // 创建一个空的8位字节数组(默认值为0)
        byte[] arrB = new byte[8];

        // 将原始字节数组转换为8位
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }

        // 生成密钥
        Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");

        return key;
    }

    /**
     * HEX转码 String to Byte
     */
    public static byte[] hexStr2Byte(String strIn) throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;

        // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i = i + 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }

    /**
     * HEX转码 Byte to  String
     */
    public static String byte2HexStr(byte[] arrB) throws Exception {
        int iLen = arrB.length;
        // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; i++) {
            int intTmp = arrB[i];
            // 把负数转换为正数
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }
            // 小于0F的数需要在前面补0
            if (intTmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        try {

            System.out.println("加密前:");
            DesUtil des = new DesUtil();
            String pwd = des.encrypt("12345uvwxwz");
            System.out.println("加密后:" + pwd);
            pwd = des.decrypt(pwd);
            System.out.println("解密密后:" + pwd);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

参考:
https://www.jianshu.com/p/6bfd49d0d4fc
https://blog.csdn.net/qq_36154832/article/details/107020854

猜你喜欢

转载自blog.csdn.net/weixin_44455388/article/details/121637377