聊聊、AES 和 DES
AES 和 DES 都是对称加密的一种,但是 DES 的 Key 是 56 位,而 AES 的 Key 有 128,256,512 可选。
AES
加密AES
String randomKey = "12345678";
public static String ENAES() {
try {
KeyGenerator keyGene = KeyGenerator.getInstance("AES");
keyGene.init(128, new SecureRandom(randomKey.getBytes()));
SecretKey key = keyGene.generateKey();
byte[] bytes = key.getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(bytes, "AES");
Cipher ciper = Cipher.getInstance("AES");
ciper.init(Cipher.ENCRYPT_MODE, keySpec);
bytes = ciper.doFinal("hello".getBytes());
String result = org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);
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;
}
解密AES
String randomKey = "12345678";
public static String DEAES(String str) {
try {
byte[] srcBytes = org.apache.commons.codec.binary.Base64
.decodeBase64(str);
KeyGenerator keyGene = KeyGenerator.getInstance("AES");
keyGene.init(128, new SecureRandom(randomKey.getBytes()));
SecretKey key = keyGene.generateKey();
byte[] bytes = key.getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(bytes, "AES");
Cipher ciper = Cipher.getInstance("AES");
ciper.init(Cipher.DECRYPT_MODE, keySpec);
bytes = ciper.doFinal(srcBytes);
String strs = new String(bytes);
return strs;
} 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;
}
DES
加密DES
String randomKey = "12345678";
public static String ENDES() {
SecureRandom random = new SecureRandom();
try {
DESKeySpec desKey = new DESKeySpec(randomKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
byte[] bytes = cipher.doFinal("hello".getBytes());
String result = Base64.getEncoder().encodeToString(bytes);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
解密DES
String randomKey = "12345678";
public static String DEDES(String str) {
byte[] bytes = Base64.getDecoder().decode(str);
SecureRandom random = new SecureRandom();
try {
DESKeySpec desKey = new DESKeySpec(randomKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
bytes = cipher.doFinal(bytes);
String result = new String(bytes);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
mian 方法
public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println("AES ENCRYPT:" + ENAES());
System.out.println("AES DECRYPT:" + DEAES(ENAES()));
System.out.println("DES ENCRYPT:" + ENDES());
System.out.println("DES DECRYPT:" + DEDES(ENDES()));
}
运行结果
AES ENCRYPT:70IScgmG93zMpkKvsNs+TQ==
AES DECRYPT:hello
DES ENCRYPT:uhbGoCVxJa8=
DES DECRYPT:hello
聊聊、AES 和 DES的更多相关文章
- 对称加密----AES和DES加密、解密
目前主流的加密方式有:(对称加密)AES.DES (非对称加密)RSA.DSA 调用AES/DES加密算法包最精要的就是下面两句话: Cipher cipher = Cipher.get ...
- Java常用加密方案及实现——AES和DES
AES和DES都是对称加密算法,其中DES全称为Data Encryption Standard,AES全称为Advanced Encryption Standard即高级加密标准. DES现在已经不 ...
- 加密算法(对称加密)AES、DES (非对称加密)RSA、DSA
目前主流的加密方式有:(对称加密)AES.DES (非对称加密)RSA.DSA
- .Net(c#)加密解密之Aes和Des
.Net(c#)加密解密工具类: /// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelp ...
- Java http数据MD5、AES、DES加密
一,数据加密 1.提供了,md5,Hex,Sha等不可逆算法加密 2.AES加密,此加密方式瘦平台影响较重,所以只适合同类平台加密解密 3.DES自定义加密,跨平台,兼容性好 1.org.apache ...
- RAS、AES、DES加密
---------------------------------------------------------------------------------------------------- ...
- c# aes,des,md5加密等解密算法
一:可逆加密,即是能加密也能解密 对称可逆加密:加密后能解密回原文,加密key和解密key是一个 加密算法都是公开的,密钥是保密的, 即使拿到密文 你是推算不了密钥 也推算不了原文 加密解密的速度快, ...
- AES,DES加密JS源文件及其使用方法
源文件地址:https://github.com/dididi1234/crypto 进入之后直接下载CryptoJS.js,js中直接引用,小程序也一样可以使用 具体使用方法和vue中的Crypto ...
- [转] AES,SHA1,DES,RSA,MD5区别
AES:更快,兼容设备,安全级别高: SHA1:公钥后处理回传 DES:本地数据,安全级别低 RSA:非对称加密,有公钥和私钥 MD5:防篡改 相关: 公开密钥加密(英语:public-key cry ...
随机推荐
- 2017.9.18 HTMl学习总结----input标签的额type
2.1.3 HTML表单标签与表单设计 (1)表单的组成:文本框(text),密码框(password),多行文本框(Multiline text box). 单选按钮框(Single - rad ...
- phpMyAdmin提示找不到mcrypt和mbstring模块
yum install php-mcryptyum install php-mbstringphp -m 查看是否安装成功 service httpd restart 重启服务器 注: 这里可能会出现 ...
- JVM性能调优(out of memory内存溢出/泄露出来)
JVM基础知识: JVM调优工具: 1.jmap jmap常用参数 命令:jmap -heap PID >> D:\heap.log 解释: using thread-local obje ...
- jQuery 二级联动
jQuery 二级联动 ----请选择省份---- 北京 上海 江苏 ----请选择城市---- 东城 西城 崇文 宣武 朝阳 黄浦 卢湾 徐汇 长宁 静安 南京 镇江 苏州 南通 扬州 & ...
- react中内联样式的z-index不起作用.
<div style={{z-index: -100}} > hello,money. </div> 以上z-index样式如上写法是不起作用,原因是在react中内联样式的写 ...
- ES6初识-模块化
export let A=123; export function test(){ console.log('test'); } export class Hello(){ test(){ conso ...
- [MYSQL笔记0]MYSQL的安装
mysql是一种关系型数据库管理系统.以mysql5.7版本为例,安装过程如下: 首先百度出mysql的官网,进入:(以下是自己安装失败的过程,直接下拉最后看大佬的安装过程吧,就是那个红红的网址) 找 ...
- 【转载】C#批量插入数据到Sqlserver中的三种方式
引用:https://m.jb51.net/show/99543 这篇文章主要为大家详细介绍了C#批量插入数据到Sqlserver中的三种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本篇, ...
- 腾讯首页分辨手机端与pc端代码
腾讯首页分辨手机端与pc端代码 自己在做网页的时候在腾讯网首页借鉴的代码. 代码: <!-- 移动适配JS脚本 --> <script type="text/javascr ...
- python网络爬虫入门范例
python网络爬虫入门范例 Windows用户建议安装anaconda,因为有些套件难以安装. 安装使用pip install * 找出所有含有特定标签的HTML元素 找出含有特定CSS属性的元素 ...