Java code lib aes 加解密

Java aes 加解密

/**
 * Created by LvJianwei on 2018/2/8.
 */

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;


/**
 * @program: reflection
 * @description: AES
 * @author: LvJianwei
 * @create: 2018-02-08 15:07
 **/
public class AESDemo {
    public static void main(String[] args) {
        String key="i will always love you";
        byte[] keyBytes=initSecretKey();
        String plainText="have a nice day";
        System.out.println("plainText:"+plainText);

        byte[] encryptedBytes=encrypt(DEFAULT_CIPHER_ALGORITHM,keyBytes,KEY_ALGORITHM,plainText);
        System.out.println("encryptedText:"+Arrays.toString(encryptedBytes));
        byte[] decryptedBytes=decrypt(DEFAULT_CIPHER_ALGORITHM,keyBytes,KEY_ALGORITHM,encryptedBytes);
        System.out.println("decryptedText:"+new String(decryptedBytes));
    }

    /**
     * algorithm
     */
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    /**
     * generate key
     * @return byte[] key
     * @throws Exception
     */
    public static byte[] initSecretKey() {
        //返回生成指定算法的秘密密钥的 KeyGenerator 对象
        KeyGenerator kg = null;
        try {
            kg = KeyGenerator.getInstance(KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return new byte[0];
        }
        //初始化此密钥生成器,使其具有确定的密钥大小
        //AES 要求密钥长度为 128
        kg.init(128);
        //生成一个密钥
        SecretKey secretKey = kg.generateKey();
        return secretKey.getEncoded();
    }
    
    /**
     * AESEncrypt
     * @param cipherAlgorithm   transformation
     * @param keyBytes  key byte array
     * @param keyAlgorithm  SecretKeySpec's algorithm
     * @param plainText text to be encrypted
     * @return
     */
    public static byte[] encrypt(String cipherAlgorithm, byte[] keyBytes, String keyAlgorithm, String plainText) {
        try {
            Cipher cipher = Cipher.getInstance(cipherAlgorithm);

            String keyStr= DatatypeConverter.printBase64Binary(keyBytes);
            SecretKeySpec keySpec = new SecretKeySpec(keyBytes, keyAlgorithm);

            cipher.init(Cipher.ENCRYPT_MODE, keySpec);

            byte[] encyptedBytes = cipher.doFinal(plainText.getBytes());
            return encyptedBytes;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * decrypt
     * @param cipherAlgorithm transformation
     * @param keyBytes key
     * @param keyAlgorithm SecretKeySpec's algorithm
     * @param encyptedBytes encypted byte array
     * @return
     */
    public static byte[] decrypt(String cipherAlgorithm, byte[] keyBytes, String keyAlgorithm, byte[] encyptedBytes) {
        try {
            Cipher cipher = Cipher.getInstance(cipherAlgorithm);

            String keyStr= DatatypeConverter.printBase64Binary(keyBytes);
            SecretKeySpec keySpec = new SecretKeySpec(keyBytes, keyAlgorithm);
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            byte[] decryptedBytes = cipher.doFinal(encyptedBytes);
            return decryptedBytes;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

posted on 2018-02-08 18:10  Lv Jianwei  阅读(193)  评论(0编辑  收藏  举报