常用加密手段
2024-01-03

描述

常用加解密工具类

java中使用

maven中引入:

<!--常用工具集-->
<dependency>
 <groupId>cn.hutool</groupId>
 <artifactId>hutool-all</artifactId>
 <version>5.8.16</version>
</dependency>
import cn.hutool.core.util.HexUtil;
import lombok.experimental.UtilityClass;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author traveller
 * 加密工具类
 */
@UtilityClass
public class CipherUtils {

    public static void main(String[] args)  throws Exception{
       /* String encodeStr = base64EncodeStr("你好啊");
        System.out.println(encodeStr);
        System.out.println(base64Decode(encodeStr));*/

        /* byte[] bytes = SHA256("这什么");
        System.out.println(base64EncodeStr(bytes));*/

        /*String encrypt = aesEncrypt("think you", "1111111111111111");
        System.out.println(encrypt);
        System.out.println(aesDecrypt(encrypt, "1111111111111111"));*/

        /*Map<String, byte[]> rsaKey = getRsaKey();
        byte[] privateKey = rsaKey.get("privateKey");
        byte[] publicKey = rsaKey.get("publicKey");
        String encrypt = rsaEncrypt(publicKey, "hello Rsa");
        System.out.println(encrypt);
        System.out.println(rsaDecrypt(privateKey, encrypt));*/

        String hexStr = "你好啊一";
        String encodeStr = base64EncodeStr(hexStr);
        System.out.println(encodeStr);
        char[] chars = HexUtil.encodeHex(hexStr.getBytes(StandardCharsets.UTF_8));
      /*  System.out.println(bytes);
        byte[] bytes1 = HexUtil.decodeHex(bytes);
        System.out.println(new String(bytes1));*/

        int i = Integer.parseInt(encodeStr, 16);
        System.out.println(i);
        String s = Integer.toString(i, 16);
        System.out.println(s);
    }

    /**
     *  base64 编码
     * @param context
     * @return 正常的编码byte数组
     */
    public  byte[] base64Encode(byte[] context){
        return Base64.getEncoder().encode(context);
    }

    /**
     *  base64 编码
     * @param context
     * @return 16进制的 Str
     */
    public String base64EncodeStr(String context){
        return new String(base64Encode(context.getBytes()));
    }
    /**
     *  base64 编码
     * @param context
     * @return 16进制的 Str
     */
    public String base64EncodeStr(byte[] context){
        return new String(base64Encode(context));
    }

    /**
     *  base64 解码
     * @param encodeHexStr base64编码后的 16进制 Str
     * @return 解码后的数据
     */
    public String base64Decode(String encodeHexStr){
       return new String(base64Decode(HexUtil.decodeHex(encodeHexStr)));
    }

    /**
     *  base64 解码
     * @param bytes 正常的编码byte数组
     * @return
     */
    public byte[] base64Decode(byte[] encodeBytes){
        return Base64.getDecoder().decode(encodeBytes);
    }

    /**
     *  生成 sha256格式 摘要
     * @param context
     * @return
     * @throws NoSuchAlgorithmException
     */
    public byte[] SHA256(String context) throws NoSuchAlgorithmException {
        return digest(context, "SHA-256");
    }

    /**
     * 根据 指定摘要格式, 生成对应摘要
     * @param context
     * @param digest
     * @return
     */
    public byte[] digest(String context, String digest) throws NoSuchAlgorithmException {
        MessageDigest messageDigest = MessageDigest.getInstance(digest);
        return messageDigest.digest(context.getBytes(StandardCharsets.UTF_8));
    }

    /**
     *
     * @param context
     * @param selectKey
     * @return
     */
    public String aesEncrypt(String context, String secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] bytes = cipher.doFinal(context.getBytes());
        return base64EncodeStr(bytes);
    }

    /**
     *
     * @param context
     * @param selectKey
     * @return
     */
    public String aesDecrypt(String context, String secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] bytes = cipher.doFinal(base64Decode(context.getBytes(StandardCharsets.UTF_8)));
        return new String(bytes);
    }

    public Map<String, byte[]> getRsaKey() throws NoSuchAlgorithmException {
        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        KeyPair keyPair = generator.generateKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();
        Map map = new HashMap(2);
        map.put("privateKey", privateKey.getEncoded());
        map.put("publicKey", publicKey.getEncoded());
        return map;
    }

    public String rsaEncrypt(byte[] publicKey, String context) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey1 = keyFactory.generatePublic(keySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey1);
       return base64EncodeStr(cipher.doFinal(context.getBytes()));
    }

    public String rsaDecrypt(byte[] privateKey, String enContext) throws InvalidKeySpecException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey1 = keyFactory.generatePrivate(keySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey1);
        return new String(cipher.doFinal(base64Decode(enContext.getBytes())));
    }
}