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的更多相关文章

  1. 对称加密----AES和DES加密、解密

    目前主流的加密方式有:(对称加密)AES.DES        (非对称加密)RSA.DSA 调用AES/DES加密算法包最精要的就是下面两句话: Cipher cipher = Cipher.get ...

  2. Java常用加密方案及实现——AES和DES

    AES和DES都是对称加密算法,其中DES全称为Data Encryption Standard,AES全称为Advanced Encryption Standard即高级加密标准. DES现在已经不 ...

  3. 加密算法(对称加密)AES、DES (非对称加密)RSA、DSA

    目前主流的加密方式有:(对称加密)AES.DES        (非对称加密)RSA.DSA

  4. .Net(c#)加密解密之Aes和Des

    .Net(c#)加密解密工具类: /// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelp ...

  5. Java http数据MD5、AES、DES加密

    一,数据加密 1.提供了,md5,Hex,Sha等不可逆算法加密 2.AES加密,此加密方式瘦平台影响较重,所以只适合同类平台加密解密 3.DES自定义加密,跨平台,兼容性好 1.org.apache ...

  6. RAS、AES、DES加密

    ---------------------------------------------------------------------------------------------------- ...

  7. c# aes,des,md5加密等解密算法

    一:可逆加密,即是能加密也能解密 对称可逆加密:加密后能解密回原文,加密key和解密key是一个 加密算法都是公开的,密钥是保密的, 即使拿到密文 你是推算不了密钥 也推算不了原文 加密解密的速度快, ...

  8. AES,DES加密JS源文件及其使用方法

    源文件地址:https://github.com/dididi1234/crypto 进入之后直接下载CryptoJS.js,js中直接引用,小程序也一样可以使用 具体使用方法和vue中的Crypto ...

  9. [转] AES,SHA1,DES,RSA,MD5区别

    AES:更快,兼容设备,安全级别高: SHA1:公钥后处理回传 DES:本地数据,安全级别低 RSA:非对称加密,有公钥和私钥 MD5:防篡改 相关: 公开密钥加密(英语:public-key cry ...

随机推荐

  1. 【转】Android UI开发第二十四篇——Action Bar

    Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...

  2. 等待唤醒机制,UDP通信和TCP通信

    等待唤醒机制 通过等待唤醒机制使各个线程能有效的利用资源. 等待唤醒机制所涉及到的方法: wait() :等待,将正在执行的线程释放其执行资格 和 执行权,并存储到线程池中. notify():唤醒, ...

  3. C# return语句

    一.C# return语句 return语句用于终止它出现在其中的方法的执行,并将控制返回给调用方法. 语法格式如下: return ...;return语句还可以返回一个可选值.如果方法为void类 ...

  4. JQuery 过滤选择器 与属性修改的方法演示比较

    文本匹配 在表单输入项里面输入值,根据输入值,点击判断按钮,让对应的复选框选中 <html> <head> <meta http-equiv="Content- ...

  5. 树梅派3B kali2.0 启用SSH进行远程登录

    工具/原料 kali 2.0 ssh SSH连接工具(XShell)or PUTTY vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉, ...

  6. 【shopex】真正可用的app开发机制

    shopex的app开发机制详解   网上流传的shopex4.8.5的app开发教程,不仅说得不明不白,而且由于版本问题,照着做根本是做不成的. 知其然,亦要知其所以然. shopex提供了的一个干 ...

  7. win7同时安装python2和python3

    1.下载python2和python3版本. 2.安装python3   1>选择添加PATH路径到系统.   2>为所有用户安装python. 3.安装python2   1>为所 ...

  8. IDEA 工具项目的配置及如何打war包

    1. Project Structure 1.1 首先点击File-ProjectStructure,进入项目配置: 2.Project Settings配置 2.1 Project  2.1.1 f ...

  9. TCP/IP协议之http和https协议

    一.TCP/IP协议 TCP/IP 是不同的通信协议的大集合. 1.TCP - 传输控制协议 TCP 用于从应用程序到网络的数据传输控制. TCP 负责在数据传送之前将它们分割为 IP 包,然后在它们 ...

  10. 笔记-DB-mongodb-常用操作-1

    笔记-DB-mongodb-常用操作-1 1.  启动及连接 1.1.  启动 启动mongod windows下: 1.   如已添加服务 net start <service name> ...