聊聊、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 ...
随机推荐
- 线程 Thread类 的四个构造方法简介
在线程中,Thread类有四个构造方法: 当我们使用 Thread类创建对象的时候,传入参数,就会用到构造方法.ThreadStart 和ParameterizedThreadStart 都是 委托类 ...
- Eclipse使用的小技巧
1.在右键new菜单栏中添加新建JSP文件 window->perspective->customize perspective->shortcuts->web->把JS ...
- CCS选择器基础
上周学习了HTML和css的一些基础,今天来夯实一下基础 选择器有: 1.标签选择器 :就是HTML 中的标签 如<p> <h1> <body>等 2.类选择器: ...
- 解决mysql远程连接失败的问题
问题描述 在我远程连接我的服务器数据库的时候,navicat给我提示了这么一个错误: ERROR : Host 'xxx' is not allowed to connect to thisMySQL ...
- 配置intellij idea中的欢迎页而不使用默认的index.jsp
在web.xml中添加 <welcome-file-list> <welcome-file>abc.jsp</welcome-file> </welcome- ...
- 【CodeBase】【转】php随机生成汉字
本方法是通过生成GB2312编码的汉字后,再转码为UTF-8编码.之所以这样做是因为UTF-8的常用汉字太过分散,随机生成会出现大量生僻字,而使用GB2312编码的好处在于其收录的大部分汉字为常用汉字 ...
- yii2 的登录注册 轮子
//利用到了yii2 框架之中的验证规则 进行判定而已 也不是很高深的东西 但是 使用框架自身的轮子 会有安全性能的隐患 1注册reg controller 中 我都以admin 为例子 publi ...
- Python学习笔记:logging(日志处理)
在一个软件中,日志是可以说必不可少的一个组成部分,通常会在定位客户问题或者记录软件使用情况等场景中会用到.logging模板块是Python的一个内置标准库,用于实现对日志的控制输出,对于平常的日志输 ...
- Python学习笔记:sqlite3(sqlite数据库操作)
对于数据库的操作,Python中可以通过下载一些对应的三方插件和对应的数据库来实现数据库的操作,但是这样不免使得Python程序变得更加复杂了.如果只是想要使用数据库,又不想下载一些不必要的插件和辅助 ...
- stdio中牛逼的写法
用空间换时间的典型 /* * NOTE! This ctype does not handle EOF like the standard C * library is required to. */ ...