AES加解密源码(直接可调用)
16lz
2021-01-26
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 ;
}
}
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 ;
}
}
更多相关文章
- android三种监听方法
- 在已有的Android(安卓)签名基础上添加地图key的方式
- Xamarin.Android-捕获未处理异常(全局异常)
- Android:Service生命周期方法与Service启动方式bindService与Star
- Android(安卓)刘海屏适配方案---NotchTools,适配国内四大厂商(华
- Android(安卓)手机端与服务器端通过http交换数据 Json
- Android进阶:十二、最简单的方式实现自定义阴影效果
- 关于android创建快捷方式会启动两个应用的问题(二)
- Android开发实战《手机安全卫士》——11.“进程管理”模块拓展 &