加密工具(md5、SHA1、SHA256、RSA、AES、DES)

自己写的加密util

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

@Slf4j
public class EncryptUtil {
   
   

    public static final String RSA = "RSA";
    public static final String AES = "AES";
    public static final String DES = "DES";
    public static final String BASE_64 = "base64";
    public static final String HEX = "hex";

    private EncryptUtil() {
   
   
        throw new IllegalStateException("Utility class");
    }

    public static String base64Encode(String data) {
   
   
        return Base64.getEncoder().encodeToString(data.getBytes());
    }

    public static String base64Decode(String data) {
   
   
        return new String(Base64.getDecoder().decode(data.getBytes()));
    }

    public static String md5(String data) {
   
   
        return DigestUtils.md5DigestAsHex(data.getBytes());
    }

    public static String sha1(String data) {
   
   
        try {
   
   
            MessageDigest messageDigest = MessageDigest.getInstance("SHA");
            byte[] cipherBytes = messageDigest.digest(data.getBytes(StandardCharsets.UTF_8));
            return parseByte2HexStr(cipherBytes);
        } catch (Exception e) {
   
   
            e.printStackTrace();
        }
        return null;
    }

    public static String sha256(String data) {
   
   
        try {
   
   
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(data.getBytes(StandardCharsets.UTF_8));
            byte[] bytes = messageDigest.digest();
            return parseByte2HexStr(bytes);
        } catch (NoSuchAlgorithmException e) {
   
   
            e.printStackTrace();
        }
        return null;
    }


    public static String rsaSHA1(String data, String privateKey, String outputEncoding) {
   
   
        try {
   
   
            byte[] keyBytes = Base64.getMimeDecoder().decode(privateKey.getBytes());
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey priKey = KeyFactory.getInstance(RSA).generatePrivate(keySpec);
            Signature signature = Signature.getInstance("SHA1withRSA");
            signature.initSign(priKey);
            signature.update(data.getBytes(StandardCharsets.UTF_8));
            byte[] signs = signature.sign();
            if (BASE_64.equals(outputEncoding)) {
   
   
                return Base64.getMimeEncoder().encodeToString(signs);
            }
            return parseByte2HexStr(signs);
        } catch (Exception e) {
   
   
            e.printStackTrace();
        }
        return null;
    }


    public static String rsaSHA256(String data, String privateKey, String outputEncoding) {
   
   
        try {
   
   
            byte[] keyBytes = Base64.getMimeDecoder().decode(privateKey.getBytes());
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey priKey = KeyFactory.getInstance(RSA).generatePrivate(keySpec);
            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initSign(priKey);
            signature.update(data.ge
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值