java通用加密封装类

package com.kongjs.dcssl.utils.crypto;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import javax.crypto.*;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;

@Component
public final class AlgorithmCryptoImpl {
    private final Log logger = LogFactory.getLog(getClass());
    private String algorithm;
    private String slatKey;
    private String data;
    private Key key;
    private Integer keySize;
    public Cipher cipher(String algorithm) {
        Cipher instance = null;
        try {
            instance = Cipher.getInstance(algorithm);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            logger.error("加密算法异常", e);
        }
        return instance;
    }

    public Cipher cipher() {
        return cipher(algorithm);
    }

    public KeyGenerator keyGenerator(String algorithm, Integer i, SecureRandom secureRandom, AlgorithmParameterSpec parameterSpec) {
        KeyGenerator instance = null;
        try {
            instance = KeyGenerator.getInstance(algorithm);
            if (i != null && secureRandom != null) {
                instance.init(i, secureRandom);
            } else {
                if (i != null) {
                    instance.init(i);
                } else {
                    if (secureRandom != null && parameterSpec != null) {
                        instance.init(parameterSpec, secureRandom);
                    } else {
                        if (secureRandom != null) {
                            instance.init(secureRandom);
                        } else {
                            if (parameterSpec != null) {
                                instance.init(parameterSpec);
                            }
                        }
                    }
                }
            }
        } catch (NoSuchAlgorithmException e) {
            logger.error("加密算法异常", e);
        } catch (InvalidAlgorithmParameterException e) {
            logger.error("加密算法参数异常", e);
        }
        return instance;
    }

    public KeyGenerator keyGenerator() {
        return keyGenerator(algorithm,keySize,secureRandom(),null);
    }

    public KeyPairGenerator keyPairGenerator(String algorithm,Integer i,SecureRandom secureRandom,AlgorithmParameterSpec parameterSpec) {
        KeyPairGenerator instance = null;
        try {
            instance = KeyPairGenerator.getInstance(algorithm);
            if (i!=null&&secureRandom!=null){
                instance.initialize(i,secureRandom);
            }else {
                if (i!=null){
                    instance.initialize(i);
                }else {
                    if (parameterSpec!=null&&secureRandom!=null){
                        instance.initialize(parameterSpec,secureRandom);
                    }else {
                        if (parameterSpec!=null){
                            instance.initialize(parameterSpec);
                        }
                    }
                }
            }
        } catch (NoSuchAlgorithmException e) {
            logger.error("加密算法异常", e);
        } catch (InvalidAlgorithmParameterException e) {
            logger.error("加密算法参数异常", e);
        }
        return instance;
    }

    public KeyPairGenerator keyPairGenerator() {
        return keyPairGenerator(algorithm,keySize,secureRandom(),null);
    }

    public SecureRandom secureRandom(String algorithm, byte[] slatKey) {
        SecureRandom secureRandom = null;
        try {
            secureRandom = SecureRandom.getInstance(algorithm);
            secureRandom.setSeed(slatKey);
        } catch (NoSuchAlgorithmException e) {
            logger.error("加密算法异常", e);
        }
        return secureRandom;
    }

    public SecureRandom secureRandom(String algorithm, String slatKey) {
        return secureRandom(algorithm, slatKey.getBytes(StandardCharsets.UTF_8));
    }

    public SecureRandom secureRandom(String slatKey) {
        return secureRandom(algorithm, slatKey);
    }

    public SecureRandom secureRandom() {
        return secureRandom(algorithm, slatKey);
    }

    public byte[] crypto(Cipher cipher, int mode, Key key, SecureRandom random, byte[] data) {
        AlgorithmParameters parameters;
        byte[] encrypt = null;
        try {
            parameters = AlgorithmParameters.getInstance(cipher.getAlgorithm());
            cipher.init(mode, key, parameters, random);
            encrypt = cipher.doFinal(data);
        } catch (NoSuchAlgorithmException e) {
            logger.error("加密算法异常", e);
        } catch (InvalidAlgorithmParameterException e) {
            logger.error("加密算法参数异常", e);
        } catch (IllegalBlockSizeException e) {
            logger.error("加密Cipher对象,BlockSize异常", e);
        } catch (BadPaddingException e) {
            logger.error("加密Cipher对象,Padding异常", e);
        } catch (InvalidKeyException e) {
            logger.error("加密密钥异常", e);
        }
        return encrypt;
    }

    public byte[] encrypt(Cipher cipher, Key key, SecureRandom random, byte[] data) {
        return crypto(cipher, Cipher.ENCRYPT_MODE, key, random, data);
    }

    public byte[] decrypt(Cipher cipher, Key key, SecureRandom random, byte[] data) {
        return crypto(cipher, Cipher.DECRYPT_MODE, key, random, data);
    }

    public byte[] encrypt(Key key, SecureRandom random, byte[] data) {
        return crypto(cipher(), Cipher.ENCRYPT_MODE, key, random, data);
    }

    public byte[] decrypt(Key key, SecureRandom random, byte[] data) {
        return crypto(cipher(), Cipher.DECRYPT_MODE, key, random, data);
    }

    public byte[] encrypt(Key key, byte[] data) {
        return crypto(cipher(), Cipher.ENCRYPT_MODE, key, secureRandom(), data);
    }

    public byte[] decrypt(Key key, byte[] data) {
        return crypto(cipher(), Cipher.DECRYPT_MODE, key, secureRandom(), data);
    }

    public byte[] encrypt(byte[] data) {
        return crypto(cipher(), Cipher.ENCRYPT_MODE, key, secureRandom(), data);
    }

    public byte[] decrypt(byte[] data) {
        return crypto(cipher(), Cipher.DECRYPT_MODE, key, secureRandom(), data);
    }

    public byte[] encrypt() {
        return crypto(cipher(), Cipher.ENCRYPT_MODE, key, secureRandom(), data.getBytes(StandardCharsets.UTF_8));
    }

    public byte[] decrypt() {
        return crypto(cipher(), Cipher.DECRYPT_MODE, key, secureRandom(), data.getBytes(StandardCharsets.UTF_8));
    }

    public byte[] crypto(Cipher cipher, int mode, Key key, AlgorithmParameterSpec parameterSpec, SecureRandom random, byte[] data) {
        byte[] encrypt = null;
        try {
            cipher.init(mode, key, parameterSpec, random);
            encrypt = cipher.doFinal(data);
        } catch (InvalidAlgorithmParameterException e) {
            logger.error("加密算法参数异常", e);
        } catch (IllegalBlockSizeException e) {
            logger.error("加密Cipher对象,BlockSize异常", e);
        } catch (BadPaddingException e) {
            logger.error("加密Cipher对象,Padding异常", e);
        } catch (InvalidKeyException e) {
            logger.error("加密密钥异常", e);
        }
        return encrypt;
    }

    public byte[] encrypt(Cipher cipher, Key key, AlgorithmParameterSpec parameterSpec, SecureRandom random, byte[] data) {
        return crypto(cipher, Cipher.ENCRYPT_MODE, key, parameterSpec, random, data);
    }

    public byte[] decrypt(Cipher cipher, Key key, AlgorithmParameterSpec parameterSpec, SecureRandom random, byte[] data) {
        return crypto(cipher, Cipher.DECRYPT_MODE, key, parameterSpec, random, data);
    }

    public String getAlgorithm() {
        return algorithm;
    }

    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }

    public String getSlatKey() {
        return slatKey;
    }

    public void setSlatKey(String slatKey) {
        this.slatKey = slatKey;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    public String toBase64Str(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    public byte[] toBase64ByteArr(String strings) {
        return Base64.getDecoder().decode(strings);
    }

    public String toStr(byte[] bytes) {
        return new String(bytes);
    }

    public byte[] toByteArr(String strings) {
        return strings.getBytes(StandardCharsets.UTF_8);
    }

    public Integer getKeySize() {
        return keySize;
    }

    public void setKeySize(Integer keySize) {
        this.keySize = keySize;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值