1. 简单介绍

3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。由于计算机运算能力的增强,原版DES密码的密钥长度变得容易被暴力破解;3DES即是设计用来提供一种相对简单的方法,即通过增加DES的密钥长度来避免类似的攻击,而不是设计一种全新的块密码算法。

2. 对称加密

2.1 介绍

对称密码算法是当今应用范围最广,使用频率最高的加密算法。它不仅应用于软件行业,在硬件行业同样流行。各种基础设施凡是涉及到安全需求,都会优先考虑对称加密算法。对称密码算法的加密密钥和解密密钥相同,对于大多数对称密码算法,加解密过程互逆。

  • 特点:算法公开、计算量小、加密速度快、加密效率高。

  • 弱点:双方都使用同样密钥,安全性得不到保证。

对称密码有流密码和分组密码两种,但是现在普遍使用的是分组密码:

2.2 分组密码工作模式

  • ECB:电子密码本(最常用的,每次加密均产生独立的密文分组,并且对其他的密文分组不会产生影响,也就是相同的明文加密后产生相同的密文)
  • CBC:密文链接(常用的,明文加密前需要先和前面的密文进行异或运算,也就是相同的明文加密后产生不同的密文)
  • CFB:密文反馈
  • OFB:输出反馈
  • CTR:计数器

2.3 常用对称密码:

  • DES(Data Encryption Standard,数据加密标准)
  • 3DES(Triple DES、DESede,进行了三重DES加密的算法)
  • AES(Advanced Encryption Standard,高级数据加密标准,AES算法可以有效抵制针对DES的攻击算法

3. DES / 3DES / AES 三种算法实现

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import com.newland.csf.common.business.IBusinessComponent; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets; /**
*
*
*
*/
public class TripleDes { //指定要使用的算法 DES / 3DES / AES 分别对应的 值为: DES / DESede / AES
public static final String ALGORITHM_3DES = "DESede"; /**
* 解密算法
* @param hexString 密文手机号
* @param skString 密钥
* @return
* @throws Exception
*/
public static String tripleDesDecrypt(String skString, String hexString) throws Exception {
SecretKey secretKey = new SecretKeySpec(fromHexString(skString), ALGORITHM_3DES);
byte[] input = fromHexString(hexString);
byte[] output = tripleDesDecryptBytes(secretKey, input);
return new String(output, StandardCharsets.UTF_8);
} /**
* 加密算法
* @param hexString 明文手机号
* @param skString 密钥
* @return
* @throws Exception
*/
public static String tripleDesEncrypt(String skString, String hexString) throws Exception {
SecretKey secretKey = new SecretKeySpec(fromHexString(skString), ALGORITHM_3DES);
byte[] output = tripleDesEncryptBytes(secretKey, hexString.getBytes(StandardCharsets.UTF_8));
return bytes2Hex(output, false);
} public static String bytes2Hex(byte[] bytes, boolean upperCase) {
if (bytes == null || bytes.length <= 0) {
return "";
}
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return upperCase ? sb.toString().toUpperCase() : sb.toString();
} public static byte[] fromHexString(final String hexString) {
if ((hexString.length() % 2) != 0) {
throw new IllegalArgumentException(
"hexString.length not is an even number");
} final byte[] result = new byte[hexString.length() / 2];
final char[] enc = hexString.toCharArray();
StringBuilder sb = new StringBuilder(2);
for (int i = 0; i < enc.length; i += 2) {
sb.delete(0, sb.length());
sb.append(enc[i]).append(enc[i + 1]);
result[i / 2] = (byte) Integer.parseInt(sb.toString(), 16);
}
return result;
} public static byte[] tripleDesEncryptBytes(SecretKey secretKey, byte[] src) throws Exception {
Cipher c1 = Cipher.getInstance(ALGORITHM_3DES);
c1.init(Cipher.ENCRYPT_MODE, secretKey);
return c1.doFinal(src);
} public static byte[] tripleDesDecryptBytes(SecretKey secretKey, byte[] src) throws Exception {
Cipher c1 = Cipher.getInstance(ALGORITHM_3DES);
c1.init(Cipher.DECRYPT_MODE, secretKey);
return c1.doFinal(src);
} /**
* 加密文件
* @param skString
* @param srcFilePath
* @param desFilePath
* @throws Exception
*/
public static void tripleDesEncryptFile(String skString,String srcFilePath,String desFilePath) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM_3DES);
SecretKey secretKey = new SecretKeySpec(fromHexString(skString), ALGORITHM_3DES);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
writeFile(cipher,srcFilePath,desFilePath);
} /**
* 解密文件
* @param skString
* @param srcFilePath
* @param desFilePath
* @throws Exception
*/
public static void tripleDesDecryptFile(String skString,String srcFilePath,String desFilePath) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM_3DES);
SecretKey secretKey = new SecretKeySpec(fromHexString(skString), ALGORITHM_3DES);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
writeFile(cipher,srcFilePath,desFilePath);
} private static void writeFile(Cipher cipher,String srcFilePath,String desFilePath) throws Exception{
byte[] buff = new byte[512];
byte[] temp = null;
int len = 0;
try (FileInputStream fis = new FileInputStream(new File(srcFilePath));
FileOutputStream fos = new FileOutputStream(new File(desFilePath))) {
while ((len = fis.read(buff)) > 0) {
temp = cipher.update(buff, 0, len);
fos.write(temp);
}
temp = cipher.doFinal();
if (temp != null) {
fos.write(temp);
}
}
}
}

本文由AnonyStar 发布,可转载但需声明原文出处。

仰慕「优雅编码的艺术」 坚信熟能生巧,努力改变人生

欢迎关注微信公账号 :云栖简码 获取更多优质文章

更多文章关注笔者博客 :云栖简码

DES/3DES/AES 三种对称加密算法实现的更多相关文章

  1. Java利用DES/3DES/AES这三种算法分别实现对称加密

    转载地址:http://blog.csdn.net/smartbetter/article/details/54017759 有两句话是这么说的: 1)算法和数据结构就是编程的一个重要部分,你若失掉了 ...

  2. DES、3DES、AES、PBE对称加密算法实现及应用

    1.对称加密算法概述 对称加密算法是应用较早的加密算法,技术成熟.在对称加密算法中,数据发信方将明文和加密密钥一起经过特殊加密算法处理后,使其变成复杂的加密密文发送出去.收信方收到密文后,若想解读原文 ...

  3. DES,3DES,AES这三种对称密钥的区别与联系

    DES:Data Encryption Standard(数据加密标准,又美国国密局,选中的IBM的方案,密钥长度为56,标准提出是要使用64位长的密钥,但是实际中DES算法只用了64位中的56位密钥 ...

  4. java-信息安全(二)-对称加密算法DES,3DES,AES,Blowfish,RC2,RC4

    概述 信息安全基本概念: DES(Data Encryption Standard,数据加密标准) 3DES(Triple DES,三重数据加密算法(TDEA,Triple Data Encrypti ...

  5. 浅析DES与AES、RSA三种典型加密算法的比较

    DES与AES的比较 自DES 算法公诸于世以来,学术界围绕它的安全性等方面进行了研究并展开了激烈的争论.在技术上,对DES的批评主要集中在以下几个方面: 1.作为分组密码,DES 的加密单位仅有64 ...

  6. DES/3DES/AES区别

    公元前400年,古希腊人发明了置换密码.1881年世界上的第一个电话保密专利出现.在第二次世界大战期间,德国军方启用“恩尼格玛”密码机,密码学在战争中起着非常重要的作用. DES 1977年1月,美国 ...

  7. [android]DES/3DES/AES加密方式

    DES 支持8位加密解密,3Des支持24位,Aes支持32位.3Des是Des算法做三次.位数的单位是字节byte.不是bits. 3Des是把24位分成3组.第一组八位用来加密,第二组8位用于解密 ...

  8. 对加密的了解(DES/3DES/AES区别 )

    DES 1977年1月,美国政府颁布:采纳IBM公司设计的方案作为非机密数据的正式. 目前在国内,随着三金工程尤其是金卡工程的启动,DES算法在POS.ATM.磁卡及智能卡(IC卡).加油站.高速公路 ...

  9. 常用加密算法的Java实现总结(二) ——对称加密算法DES、3DES和AES

    1.对称加密算法 1.1 定义 对称加密算法是应用较早的加密算法,技术成熟.在对称加密算法中,数据发信方将明文(原始数据)和加密密钥(mi yue)一起经过特殊加密算法处理后,使其变成复杂的加密密文发 ...

随机推荐

  1. HTML标签和属性一

    一.web基础知识 html,专门指网页技术 HTML5,大前端技术(网页,app,桌面程序,数据可视化,VR....) 网页(pc,pad,phone) app  wx  服务器 数据库 HTML5 ...

  2. 【python----发轫之始】【基础知识总结】

    python基础知识总结 一.自学感受 学完之后,,,感觉脑子里全是乱的,单词这么多,都要分不清什么时候该用什么,他到底属于哪一个数据类型里的函数,,,,,, 所以,我想着把笔记整理一下,方便自己和需 ...

  3. How To Mitigate Slow HTTP DoS Attacks in Apache HTTP Server

    http://www.acunetix.com/blog/web-security-zone/articles/slow-http-dos-attacks-mitigate-apache-http-s ...

  4. Deno会在短期内取代Node吗?

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://blog.bitsrc.io/what-is-deno-and-will-it-r ...

  5. PAT-1135 Is It A Red-Black Tree(二叉查找树的创建和遍历)

    There is a kind of balanced binary search tree named red-black tree in the data structure. It has th ...

  6. HDU1588

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1588 题目大意:g(i)= k * i + b. 给定 k 和 b,求0 <= i < n ...

  7. 【Java】Scanner类nextInt后使用nextLine无法读取输入

    首先,我们先介绍一下,在学习Java语言,对于字符串的输入,由于Scanner.next()函数无法输入空格及回车,此时,我们就必须要用Scanner.nextLine()解决这类问题, 在使用过程中 ...

  8. 使用vue2.0创建的项目的步骤

    1.由于vue项目依赖 node.js npm 需要先安装.   若没有请先安装,请百度 //检查是否有node.js  npm vue win+r   输入cmd  输入node -v  回车 会出 ...

  9. Java——变量自增(++)自减(--)

    //运算符在操作数之后,称为“后增量”.i变量自增,返回自增之前的值;//运算符在操作数之前,称为“前增量”.i变量自增,返回自增之后的值.//自减同理 public static void test ...

  10. 要小心 JavaScript 的事件代理

    我们知道,如果给 form 里面的 button 元素绑定事件,需要考虑它是否会触发 form 的 submit 行为.除此之外,其它场合给 button 元素绑定事件,你几乎不用担心这个事件会有什么 ...