java秘钥工具类

 
package security.impl;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import security.IEncryptionAlgorithm;
import security.IEncryptionAlgorithm.Parameter;

public class EncryptionAlgorithmImpl implements IEncryptionAlgorithm {
    public EncryptionAlgorithmImpl() {
    }

    public SecretKey getKeygen(Parameter encryptionParameter, int keysize) throws NoSuchAlgorithmException {
        KeyGenerator instance = KeyGenerator.getInstance(encryptionParameter.getEncryptionType());
        instance.init(keysize);
        SecretKey secretKey = instance.generateKey();
        return secretKey;
    }

    public SecretKey getKeygen(Parameter encryptionParameter, int keysize, String password) throws NoSuchAlgorithmException {
        KeyGenerator instance = KeyGenerator.getInstance(encryptionParameter.getEncryptionType());
        instance.init(keysize, new SecureRandom(password.getBytes()));
        SecretKey secretKey = instance.generateKey();
        return secretKey;
    }

    public String getEncryptMessage(SecretKey deskey, String message) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException {
        String algorithm = deskey.getAlgorithm();
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(1, deskey);
        return this.parseByte2HexStr(cipher.doFinal(message.getBytes()));
    }

    public String getEncryptMessage(String message) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException {
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed("******".getBytes());
        generator.init(128, secureRandom);
        SecretKey keygen = generator.generateKey();
        Cipher cipher = Cipher.getInstance(keygen.getAlgorithm());
        cipher.init(1, keygen);
        return this.parseByte2HexStr(cipher.doFinal(message.getBytes()));
    }

    public String getDecryptMessage(SecretKey deskey, String message) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException {
        String algorithm = deskey.getAlgorithm();
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(2, deskey);
        return new String(cipher.doFinal(this.parseHexStr2Byte(message)));
    }

    public String getDecryptMessage(String message) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException {
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed("******".getBytes());
        generator.init(128, secureRandom);
        SecretKey keygen = generator.generateKey();
        Cipher cipher = Cipher.getInstance(keygen.getAlgorithm());
        cipher.init(2, keygen);
        return new String(cipher.doFinal(this.parseHexStr2Byte(message)));
    }

    public String parseByte2HexStr(byte[] buf) {
        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < buf.length; ++i) {
            String hex = Integer.toHexString(buf[i] & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            sb.append(hex.toUpperCase());
        }

        return sb.toString();
    }

    public byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hexStr.length() / 2];

            for(int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte)(high * 16 + low);
            }

            return result;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小钻风巡山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值