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

}
}

原文地址:https://www.cnblogs.com/yxysuanfa/p/6902973.html