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;
        }
    }
}

 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐