zoukankan      html  css  js  c++  java
  • AES算法加密java实现

    package cn.itcast.coderUtils;

    import java.security.Key;
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;



    public class AESCoder {

    public static final String KEY_ALGORITHM = "AES";

    /**
    * 加密、解密/ 工作模式/ 填充方式
    */
    public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
    * 转换秘钥
    * @param key 二进制秘钥
    * @return Key 秘钥
    * @throws Exception
    */
    private static Key toKey(byte[] key) throws Exception {
    //实例化AES秘钥材料
    SecretKey  secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
    return secretKey;
    }


    /**
    * @param data 带解密数据
    * @param key 秘钥
    * @return byte[] 解密数据
    * @throws Exception
    */
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    //还原秘钥
    Key k = toKey(key);
    //实例化
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    //初始化,设置解密模式
    cipher.init(Cipher.DECRYPT_MODE, k);
    //运行操作
    return cipher.doFinal(data);
    }

    /**
    * 加密
    * @param data  带加密数据
    * @param key 秘钥
    * @return byte[] 加密数据
    * @throws Exception
    */
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
    //还原秘钥
    Key k = toKey(key);
    //实例化
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    //初始化,设置加密模式
    cipher.init(Cipher.ENCRYPT_MODE, k);
    //运行操作
    return cipher.doFinal(data);
    }

    /**
    * 生成秘密秘钥
    * java7 仅仅支持56位密钥
    * Bouncy Castle 支持64位秘密
    * @return  二进制秘钥
    * @throws Exception
    */
    public static byte[] initKey() throws Exception {
    /**
    * 实例化秘钥生成器
    *如要使用64位秘钥须要替换为 KeyGenerator.getInstance(CIPHER__ALGORITHM, "BC")。
    *"BC"是Bouncy Castle安全提供者的缩写。
    */
    KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
    //初始化秘钥生成器
    kg.init(128);
    //生成秘密秘钥
    SecretKey secretKey = kg.generateKey();
    //获得秘钥的二进制编码形式
    return secretKey.getEncoded();

    }


    上述代码实现相对通用,可用于DES,DESede(3DES),RC2。RC4等算法都能够參照上述代码实现,仅仅需对算法名称稍作调整就可以。


    測试用例代码:

    package cn.itcast.testUtils;

    import org.apache.commons.codec.binary.Base64;
    import org.junit.Test;
    import com.sun.enterprise.security.auth.login.AssertedCredentials;
    import cn.itcast.coderUtils.DESCoder;
    public class AESCoderTest {


    @Test
    public void testAES() throws Exception {
    String inputStr = "AES";
    byte[] inputData = inputStr.getBytes();
    System.out.println("原文: "+ inputStr);
    byte[] key = DESCoder.initKey(); 
    System.out.println("秘钥: " + Base64.encodeBase64String(key));
    //加密
    inputData = DESCoder.encrypt(inputData, key);
    System.out.println("加密后: " + Base64.encodeBase64String(inputData));
    byte[] outputDtat = DESCoder.decrypt(inputData, key);
    String outputStr = new String(outputDtat);
    System.out.println("解密后: " + outputStr);
    Boolean bool = inputStr.equals(outputStr);
    System.out.println(bool);

    }
    }

  • 相关阅读:
    使用jmeter进行性能测试-Jmeter教程及技巧汇总 (转)
    Linux防火墙(Iptables)的开启与关闭
    解决Unable to load R3 module ...VBoxDD.dll (VBoxDD):GetLastError=1790
    Linux如何修改文件/文件夹内所有文件的权限
    php抽象类的简单应用
    php接口和多态的概念以及简单应用
    关于php中数据访问的几点补充
    php中重写和final关键字的使用
    php中static静态关键字的使用
    php对象引用和析构函数的关系
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6902973.html
Copyright © 2011-2022 走看看