3DES
3DES是继DESeasy被破解后的DES加密升级版,它属于对称加密。
可指定24位长度的密钥。在java API中也有事实上现,代码例如以下:
/**
* 3DES 的Java SDK API 实现
* @author dxd
* 201406917
*/
public class DES3 {
private static final String Algorithm = "DESede";// 定义 加密算法,可用 //keybyte为加密密钥。长度为24字节
//src为被加密的数据缓冲区(源)
public static byte[] encryptMode(byte[] keybyte, byte[] src) {
try {
//生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); //加密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
} //keybyte为加密密钥,长度为24字节
//src为加密后的缓冲区
public static byte[] decryptMode(byte[] keybyte, byte[] src) {
try {
//生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); //解密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
} //转换成十六进制字符串
public static String byte2hex(byte[] b) {
String hs="";
String stmp=""; for (int n=0;n<b.length;n++) {
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}
}
调用时能够:
byte[] keydata = {
(byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
(byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b, (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f,
(byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17, };
String src = "3DES加密算法" ;
byte[] encoded = DES3.encryptMode(keydata, src.getBytes());
System.out.println("DES3-->Encoded = " + new String(encoded));
则能够计算出原文src加密后的密文。
3DES的更多相关文章
- Des与3Des加密解密
/// <summary> /// Des和3Des算法 /// </summary> public class Des { /// <summary> /// D ...
- [转] 对称加密算法DES、3DES
转自:http://www.blogjava.net/amigoxie/archive/2014/07/06/415503.html 1.对称加密算法 1.1 定义 对称加密算法是应用较早的加密算法, ...
- DES & 3DES 加密算法
JAVA坑 跟其他公司java的对接口,一个细节对到吐血,具体: DesUtil.java(别人的反例) //package base_class; import java.io.IOExceptio ...
- C# 3DES加密
最近一个项目中,因为服务端是用的java开发的,客户端是用的C#,由于通信部分采用到了3DES加密,所以做个记录,以备以后需要的时候直接用. 这是对方(java)的加密算法,和网上流传的代码也差不多( ...
- iOS 3DES加密解密(一行代码搞定)
3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...
- C#的3DES加密解密算法
C#类如下: using System; using System.Collections.Generic; using System.Text; using System.Security.Cryp ...
- 对称密码-DES和3DES
最近在看信息安全的知识,就总结了一下自己所学到知识. 先说一下什么是对称密码算法,什么是对称密码算法呢?对称密码算法是指有了加密密钥就可以推算出解密密钥,有了解密密钥就可以推算出加密密钥的的算法. 那 ...
- 与POS机通信时的3DES(双倍长)加密解密
项目中有个SocketServer要和移动便携POS机通信,POS开发商就告诉我们他们用的3DES(双倍长)加密,给了个Key.数据和结果,让我们实现. c#用TripleDESCryptoServi ...
- PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决方法
我也是PHP新手,通过w3cschool了解了一下php基本原理之后就开写了.但仍是菜鸟. 先不管3DES加密的方法对不对,方法都是网上的,在运行的时候报了个错,把小弟整死了.找来找去终于自己摸出了方 ...
- 简进祥==iOS 3DES加密解密
3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...
随机推荐
- HDU 5289 Assignment(二分+RMQ-ST)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- TJOI2015题解
(转载前请标明出处,谢谢) 打算来做一波TJOI2015,来写题解啦! Day1: T1:[bzoj3996] 题目链接:http://www.lydsy.com/JudgeOnline/proble ...
- JQuery基础 学习的一些例子以及手册
原文发布时间为:2009-12-23 -- 来源于本人的百度文章 [由搬家工具导入] 不多说,直接下载。。。 下载:http://www.xmaspx.com/Services/FileAttachm ...
- 长沙理工校赛I题题解-连续区间的最大公约数
题目来源https://www.nowcoder.com/acm/contest/96/I 解题前们需要先知道几个结论: 首先,gcd是有区单调性的: gcd(L,R)>=gcd(L,R+d) ...
- [LeetCode] Remove Duplicates from Sorted List 链表
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 牛客网 牛客小白月赛2 C.真真假假-String遍历比较
C.真真假假 链接:https://www.nowcoder.com/acm/contest/86/C 这个题真的是无敌的水,但是自己写前面的string数组的时候,里面的这些头文件要用双引号(&qu ...
- Codeforces Gym101473 E.Dangerous Dive (2013-2014 ACM-ICPC Brazil Subregional Programming Contest)
代码: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmat ...
- (6)ASP.NET HttpServerUtility 类
HttpServerUtility 类 提供用于处理 Web 请求的 Helper 方法 https://msdn.microsoft.com/zh-cn/library/system.web.htt ...
- ansible 2.7.1 常见错误总结
1.RequestsDependencyWarning (refer to http://blog.51cto.com/mjunetwslinux/2177727?source=dra) python ...
- [Python Cookbook] Numpy Array Slicing and Indexing
1-D Array Indexing Use bracket notation [ ] to get the value at a specific index. Remember that inde ...