今天给各位分享aes加密算法java实现的知识,如果能碰巧解决你现在面临的问题,别忘了关注本站。
JDK对DESede算法的支持
密钥长度:128位
工作模式:ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128/OFB/OBF8 to OFB128
填充方式:Nopadding/PKCS5Padding/ISO10126Padding/
aes加密解密的java实现:
package com.kongxincai.encanddec;import java.security.Key;import java.security.NoSuchAlgorithmException;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;public class aesCoder { private static final String KEY_ALGORITHM = "aes"; private static final String DEFAULT_CIPHER_ALGORITHM = "aes/ECB/PKCS5Padding";//默认的加密算法
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();
} private static Key toKey(byte[] key){ //生成密钥
return new SecretKeySpec(key, KEY_ALGORITHM);
} public static byte[] encrypt(byte[] data,Key key) throws Exception{ return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} public static byte[] encrypt(byte[] data,byte[] key) throws Exception{ return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} public static byte[] encrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{ //还原密钥
Key k = toKey(key); return encrypt(data, k, cipherAlgorithm);
} public static byte[] encrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{ //实例化
Cipher cipher = Cipher.getInstance(cipherAlgorithm); //使用密钥初始化,设置为加密模式 cipher.init(Cipher.ENCRYPT_MODE, key); //执行操作
return cipher.doFinal(data);
} public static byte[] decrypt(byte[] data,byte[] key) throws Exception{ return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} public static byte[] decrypt(byte[] data,Key key) throws Exception{ return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} public static byte[] decrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{ //还原密钥
Key k = toKey(key); return decrypt(data, k, cipherAlgorithm);
} public static byte[] decrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{ //实例化
Cipher cipher = Cipher.getInstance(cipherAlgorithm); //使用密钥初始化,设置为解密模式 cipher.init(Cipher.DECRYPT_MODE, key); //执行操作
return cipher.doFinal(data);
} private static String showByteArray(byte[] data){ if(null == data){ return null;
}
StringBuilder sb = new StringBuilder("{"); for(byte b:data){
sb.append(b).append(",");
}
sb.deleteCharAt(sb.length()-1);
sb.append("}"); return sb.toString();
} public static void main(String[] args) throws Exception { byte[] key = initSecretKey();
System.out.println("key:"+showByteArray(key));
Key k = toKey(key); //生成秘钥
String data ="aes数据";
System.out.println("加密前数据: string:"+data);
System.out.println("加密前数据: byte[]:"+showByteArray(data.getBytes()));
System.out.println(); byte[] encryptData = encrypt(data.getBytes(), k);//数据加密
System.out.println("加密后数据: byte[]:"+showByteArray(encryptData));// System.out.println("加密后数据: hexStr:"+Hex.encodeHexStr(encryptData)); System.out.println(); byte[] decryptData = decrypt(encryptData, k);//数据解密
System.out.println("解密后数据: byte[]:"+showByteArray(decryptData));
System.out.println("解密后数据: string:"+new String(decryptData));
}
}
需要依赖Java加密扩展的支持才能实现。
用Java实现aes需要依赖Java加密扩展TheJavaCryptographyExtension,简称JCE的支持主要是在javax下面的一些包。根据描述需要使用的算法为aesCBCNoPadding。
Java语言的语法与C语言和C加加语言很接近,使得大多数程序员很容易学习和使用。另一方面,Java丢弃了C加加中很少使用的、很难理解的、令人迷惑的那些特性,如操作符重载、多继承、自动的强制类型转换。特别地,Java语言不使用指针,而是引用。并提供了自动分配和回收内存空间,使得程序员不必为内存管理而担忧。
你解密的key必须是加密的key啊
你看看,你解密的时候又KeyGenerator.getInstance("aes").generateKey();这是重新搞了一个key啊,当然解不出来了
我估计你这代码人家原先是写在一起的吧,加密完了再直接解密给你看,人家只generateKey一次,自然很顺利,你分成了两个例子,居然分别generateKey,自然失败
1. aes加密字符串
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("aes");// 创建aes的Key生产者
kgen.init(128, new SecureRandom(password.getBytes()));// 利用用户密码作为随机数初始化出
// 128位的key生产者
//加密没关系,SecureRandom是生成安全随机数序列,password.getBytes()是种子,只要种子相同,序列就一样,所以解密只要有password就行
SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回
// null。
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "aes");// 转换为aes专用密钥
Cipher cipher = Cipher.getInstance("aes");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化为加密模式的密码器
byte[] result = cipher.doFinal(byteContent);// 加密
return result;
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
2. aes解密
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("aes");// 创建aes的Key生产者
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "aes");// 转换为aes专用密钥
Cipher cipher = Cipher.getInstance("aes");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器
byte[] result = cipher.doFinal(content);
return result; // 明文
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
*
* @author wchun
*
* aes128 算法,加密模式为ECB,填充模式为 pkcs7(实际就是pkcs5)
*
*
*/
public class aes {
static final String algorithmStr="aes/ECB/PKCS5Padding";
static private KeyGenerator keyGen;
static private Cipher cipher;
static boolean isInited=false;
//初始化
static private void init()
{
//初始化keyGen
try {
keyGen=KeyGenerator.getInstance("aes");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
keyGen.init(128);
//初始化cipher
try {
cipher=Cipher.getInstance(algorithmStr);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isInited=true;
}
public static byte[] GenKey()
{
if(!isInited)//如果没有初始化过,则初始化
{
init();
}
return keyGen.generateKey().getEncoded();
}
public static byte[] Encrypt(byte[] content,byte[] keyBytes)
{
byte[] encryptedText=null;
if(!isInited)//为初始化
{
init();
}
Key key=new SecretKeySpec(keyBytes,"aes");
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
encryptedText=cipher.doFinal(content);
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encryptedText;
}
//解密为byte[]
public static byte[] DecryptToBytes(byte[] content,byte[] keyBytes)
{
byte[] originBytes=null;
if(!isInited)
{
init();
}
Key key=new SecretKeySpec(keyBytes,"aes");
try {
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//解密
try {
originBytes=cipher.doFinal(content);
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return originBytes;
}
}
这个算法java SDK自带的额 参考代码如下:
/**解密
* @param content 待解密内容
* @param password 解密密钥
* @return
*/
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("aes");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "aes");
Cipher cipher = Cipher.getInstance("aes");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* 加密
*
* @param content 需要加密的内容
* @param password 加密密码
* @return
*/
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("aes");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "aes");
Cipher cipher = Cipher.getInstance("aes");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
图像界面的话就不说了
智能化时代的到来涉及了各种核心算法,保护算法就能保障开发者权益,杜绝市面上各种山寨品,加密芯片恰好能起到很好的保护作用,如何选择加密芯片呢?KEROS加密芯片专注于加密领域十余年,行业首选。
1.安全性:采用国际通用aes256算法加密并同时通过KAS传送,除基本认证之外,利用2K安全EEPROM,用户可以自己管理密钥和数据,实现双重保护。
2.唯一性:以定制的方式为每一位用户单独定制“专属型号CID”,多用户之间算法不兼容,并且采用固化的方法直接将算法固化到晶圆上而无需烧入。
3.序列号:每颗芯片制造生产时具有5字节全球唯一SN序列号,每颗芯片SN都不会重复。
4.防抄特性:每颗芯片都有自己独特的密钥系统,破解单颗芯片只对这颗芯片对应的产品有效,对整个同类型的产品是无效的,依旧无法通过验证。而且KEROS采用ASIC方法设计,芯片内为纯逻辑电路,封装内有40多层逻辑电路整合了10万多个逻辑门,爆力刨片破解难度可想而知。
5.安全存储:用户可以将保密数据加密之后安全的存放到EEPROM中。aes加密算法java实现的介绍就聊到这里吧,感谢你花时间阅读本站内容。
本文标签:aes加密算法java实现