您的位置:首页 > 其它

AES加解密源码(直接可调用)

2017-07-31 15:18 155 查看
package com.example.rog_pc.myapplication;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by a12 on 2016/7/31.
 */
public class amAES {

    /**
     * 密钥算法
     */
   
public  final String KEY_ALGORITHM = "AES";

    /**
     * 加密/解密算法 / 工作模式 / 填充方式
     * Java 6支持PKCS5Padding填充方式
     * Bouncy Castle支持PKCS7Padding填充方式
     */
   
public  final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * 生成密钥的数�?
     *
     * @return byte[] 二进制key
     * @throws Exception
     */
   
public  byte[] generateSecretKey()  {

        // 实例
        KeyGenerator kg = null;
        try {

            kg = KeyGenerator.getInstance(KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {

            e.printStackTrace();
        }

        // AES 要求密钥长度   128位 192位或 256
        kg.init(128, new SecureRandom());
        // 生成秘密密钥
        SecretKey secretKey = kg.generateKey();
        // 获得密钥的二进制编码形式
        return secretKey.getEncoded();
    }

    /**
     * 转换密钥
     *
     * @param key 二进制密�?
     * @return Key 密钥
     * @throws Exception
     */
   
private  SecretKey toKey(byte[] key) throws Exception {

        // 实例化AES密钥材料
        SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
        return secretKey;
    }

    /**
     * 加密
     *
     * @param data 待加密数�?
     * @param key 密钥
     * @return byte[] 加密数据
     * @throws Exception
     */
   
public  byte[] encrypt(byte[] data, byte[] key)  {

        // 还原密钥
        Key aeskey = null;
        byte[] bytesRet = null;
        try {

            aeskey = toKey(key);
           
/*
       * 实例�?
       * 使用PKCS7Padding填充方式,按如下方式实现
       * Cipher.getInstance(CIPHER_ALGORITHM, "BC");
       */
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

           
// 初始化,设置为加密模�?
            cipher.init(Cipher.ENCRYPT_MODE, aeskey);

           
// 执行操作
            bytesRet = cipher.doFinal(data);
        } catch (Exception e) {

            e.printStackTrace();
        }

        return  bytesRet;
    }

    /**
     * 解密
     *
     * @param data 待解密数�?
     * @param key 密钥
     * @return byte[] 解密数据
     * @throws Exception
     */
   
public  byte[] decrypt(byte[] data, byte[] key)  {

        Key aeskey = null;
        byte[] bytesRet = null;
        try {

            aeskey = toKey(key);
           
/*
       * 实例�?
       * 使用PKCS7Padding填充方式,按如下方式实现
       * Cipher.getInstance(CIPHER_ALGORITHM, "BC");
       */
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

           
// 初始化,设置为解密模�?
            cipher.init(Cipher.DECRYPT_MODE, aeskey);

           
// 执行操作

            bytesRet = cipher.doFinal(data);
        } catch (Exception e) {

            e.printStackTrace();
        }

        return  bytesRet;
    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: