java安全架构____AES加密原理

来源:互联网 发布:unity3d 5 动画系统 编辑:程序博客网 时间:2024/05/28 23:10
//使用安全数可以增加
SECURITY_STR 安全系数 
package demo.security;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;/** * @author God * AES加密解密工具类 */public class AESUtil {//加密模式public static final String ALGORITHM_AES="AES";//安全数 public static final String SECURITY_STR="zhangsan";/** * 加密 * @param security_str * @param clearText * @return */public static byte[] AESEncode(String security_str,String clearText){byte[] cipherBytes=null;try {// 1.构造密钥生成器,指KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_AES);// 2.初始化秘钥生成器 加安全数keyGenerator.init(128, new SecureRandom(security_str.getBytes()));//获取安全秘钥SecretKey secretKey = keyGenerator.generateKey();///生成秘钥自己数组byte[] secretKeyBytes = secretKey.getEncoded();//根据字节数组生成AES秘钥SecretKeySpec secretKeySpec = new SecretKeySpec(secretKeyBytes, ALGORITHM_AES);// 根据AES生成密码器Cipher cipher = Cipher.getInstance(ALGORITHM_AES);//初始化密码器 加密模式,secretKeySpeccipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);//将明文进行自己数组加密处理cipherBytes=cipher.doFinal(clearText.getBytes());} catch (Exception e) {e.printStackTrace();}return cipherBytes;}/** * 解密 安全数   加密的数组 * @return */public static byte[] AESDecode(String security_str,byte [] cipherBytes){byte[] clearTextBytes=null;try {KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_AES);keyGenerator.init(128, new SecureRandom(security_str.getBytes()));SecretKey secretKey = keyGenerator.generateKey();byte[] secretKeyBytes = secretKey.getEncoded();SecretKeySpec secretKeySpec = new SecretKeySpec(secretKeyBytes,ALGORITHM_AES);Cipher cipher = Cipher.getInstance(ALGORITHM_AES);// 创建密码器cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);// 初始化clearTextBytes = cipher.doFinal(cipherBytes);} catch (Exception e) {e.printStackTrace();}return clearTextBytes;}/** * 测试AES加密解密 * @param args */public static void main(String[] args) {String clearText="老司机开车了";System.out.println("明文是_____:"+clearText);//加密System.out.println("机密后的密文是___________:"+AESEncode(SECURITY_STR, "老司机开车了"));byte[]bytes=AESDecode(SECURITY_STR, AESEncode(SECURITY_STR, "老司机开车了"));System.out.println("解析的明文是___:"+new String(bytes));}}

//运行结果


1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 假的租房合同怎么办 代注册公司被骗怎么办 公司跨区迁址怎么办 公司搬走注册地怎么办 注册公司没有房产证怎么办 公司不运营了怎么办 公司注销了账户怎么办 租户不变更地址怎么办 营业执照忘审了怎么办 工商营业执照年检过期怎么办 个体营业执照没有年报怎么办? 个体工商户一年没有申报怎么办 个体工商户逾期未申报怎么办 个体户没报税过怎么办 农业银行证书过期了怎么办 ca证书丢了怎么办 ca证书被锁怎么办 上个月忘记清卡怎么办 财务人员进入税务黑名单怎么办 社保本丢了怎么办 贷款车辆登记证书怎么办 发票薄丢了怎么办? 汽车发票丢了怎么办 税票弄丢了怎么办 交强险正本丢了怎么办 个体营业执照正本丢失怎么办 简易注销后税务怎么办 拒绝了日历邀请怎么办 老人走丢了怎么办 老人走丢找不到怎么办 没人给介绍对象怎么办 bate365账号被锁怎么办 qq号疑似被盗怎么办 不知道音乐名字怎么办 忘记支付宝登录怎么办 微信被老婆拉黑怎么办 微信群昵称改不了怎么办 微信号设置不了怎么办 修改微信号点不开怎么办 多屏设置失败怎么办 icould密码忘了怎么办