A. 3des加密原理
使用3Des加密算法前,我们需要了解一下当前主流的加密模式:单向加密和双向加密,两者最大的区别在于加密的密文是否具有可逆性。
单向加密:将需要加密的数据进行加密,并且密文不可进行解密,像我们常用的加密算法MD5就属于这种。
双向加密:和单向加密不同的是可以通过某些方式进行加解密的操作,其中分为对称加密和非对称加密。
对称加密:指数据使用者必须拥有相同的密钥才可以进行加密解密,就像彼此约定的一串暗号,本文介绍的3Des加密就属于这种。
非对称加密:通过一组包含公钥和私钥的密码来加密解密,用公钥加密,私钥解密,首推的就是RSA加密
---------------------------------------------------------------------------------------------------------------------------------------
3Des加密算法,由于可以逆推原文,所以主要通过本地的唯一密钥来保证数据的安全性,我这边通过生成随机的256位加密字符串存储在本地,代码读取时将其通过md5加密成32位的字符串(由于本地有原始密钥,不必担心md5加密不可逆),最后以这32位加密字符串作为密钥进行加解密的操作。
B. java 3des双倍长 有参考的代码给提供下吗
虽然不知道你说的双倍长是什么意思,这是我自己用的3des工具类
密文是把byte数组转化成16进制字符串,一个byte对应两个数字,不知道是不是你说的双倍
/**
*DESede(3DES)加解密整理--pangjs2013.12.04
*/
publicclassDESede{
/**定义加密算法。可用DES,DESede,Blowfish*/
="DESede";
privatestaticfinalchar[]hex="0123456789abcdef".toCharArray();
publicstaticfinalStringkeyStr=StringUtil.DeviceKey;
/**必须是24位*/
privatestaticbyte[]key=strToBytes(keyStr);
/**
*加密,参数有问题返回null
*@paramsrc明文
*@return16进制字符串密文
*/
publicstaticStringencrypt(Stringsrc){
if(src==null){
returnnull;
}
try{
byte[]origin=src.getBytes("utf-8");
returnbytesToStr(des3Init(Cipher.ENCRYPT_MODE,origin));
}catch(Exceptione){
e.printStackTrace();
returnnull;
}
}
/**
*解密,参数有问题返回null
*@paramcipher(16进制字符串密文)
*@return明文
*/
publicstaticStringdecrypt(Stringcipher){
if(cipher==null){
returnnull;
}
try{
byte[]origin=des3Init(Cipher.DECRYPT_MODE,strToBytes(cipher));
returnnewString(origin,"utf-8");
}catch(Exceptione){
e.printStackTrace();
returnnull;
}
}
/**
*3des加解密
*@parammodeCipher.ENCRYPT_MODE|Cipher.DECRYPT_MODE
*@paraminputbyte[]
*@returnbyte[]
*@,InvalidKey,NoSuchPadding,BadPadding,IllegalBlockSize
*/
privatestaticbyte[]des3Init(intmode,byte[]input)throwsException{
//根据【给定的字节数组key】和【指定算法DESede(3des)】构造一个密钥
SecretKeysecretKey=newSecretKeySpec(key,Algorithm);
//加解密
Ciphercipher=Cipher.getInstance(Algorithm);
cipher.init(mode,secretKey);
returncipher.doFinal(input);
}
/**
*byte数组转换为16进制字符串
*@paramb[1,33,79,88,-120]
*@return"01214f5888"
*/
privatestaticStringbytesToStr(byte[]b){
StringBuilders=newStringBuilder();
for(inti=0;i<b.length;i++){
s.append(hex[(b[i]>>>4)&0xf]);
s.append(hex[b[i]&0xf]);
}
returns.toString();
}
/**
*16进制字符串转换为相应的byte数组
*@paramsrc"81214f5888"
*@return[-127,33,79,88,-120]
*/
privatestaticbyte[]strToBytes(Stringsrc){
char[]c=src.toCharArray();
byte[]b=newbyte[c.length/2];
for(inti=0;i<b.length;i++){
b[i]=(byte)((Character.digit((int)c[2*i],16)<<4)
|Character.digit((int)c[2*i+1],16));
}
returnb;
}
}
C. 3DES的3DES算法
3DES算法是指使用双长度(16字节)密钥K=(KL||KR)将8字节明文数据块进行3次DES加密/解密。如下所示:
Y = DES( KL[DES-1( KR[DES( KL[X] )] )] )
解密方式为:
X = DES-1( KL[DES( KR[DES-1( KL[Y] )] )] )
其中,DES( KL[X] )表示用密钥K对数据X进行DES加密,DES-1( KR[Y] )表示用密钥K对数据Y进行解密。
SessionKey的计算采用3DES算法,计算出单倍长度的密钥。表示法为:SK = Session(DK,DATA)
3DES加密算法为:
VOID 3DES(BYTE DoubleKeyStr[16], BYTE Data[8], BYTE Out[8])
{
BYTE Buf1[8], Buf2[8];
DES (&DoubleKeyStr[0], Data, Buf1);
UDES(&DoubleKeyStr[8], Buf1, Buf2);
DES (&DoubleKeyStr[0], Buf2, Out);
}
D. 如何用C实现3DES算法..
//功能:实现DES及3DES加解密的算法
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "des.h"
//函数声明
int Do_DES(char* strSrc, char* strKey, char* strDest, char flag);
int Do_3DES(char* strSrc, char* strKey, char* strDest, char flag);
//主函数
int main(int argc, char** argv)
{
char src16[16+1],key16[16+1],key48[48+1],dest16[16+1];
if(argc != 3)
{
fprintf(stderr,"Usage: [%s -e|-d s|3]\n",argv[0]);
exit(1);
}
if(strcmp(argv[2],"-s") == 0)
{
if(strcmp(argv[1],"-e") == 0)
{
fprintf(stderr,"Please input the string that you want to encrypt(16 hex number):\n");
memset(src16,0,sizeof(src16));
scanf("%s",src16);
fprintf(stderr,"Please input the Key string(16 hex number):\n");
memset(key16,0,sizeof(key16));
scanf("%s",key16);
memset(dest16,0,sizeof(dest16));
Do_DES(src16,key16,dest16,'e');
fprintf(stderr,"Result: [%s]\n",dest16);
}
else if(strcmp(argv[1],"-d") == 0)
{
fprintf(stderr,"Please input the string that you want to decrypt(16 hex number):\n");
memset(src16,0,sizeof(src16));
scanf("%s",src16);
fprintf(stderr,"Please input the Key string(16 hex number):\n");
memset(key16,0,sizeof(key16));
scanf("%s",key16);
memset(dest16,0,sizeof(dest16));
Do_DES(src16,key16,dest16,'d');
fprintf(stderr,"Result: [%s]\n",dest16);
}
else
return -1;
}
else if(strcmp(argv[2],"-3") == 0)
{
if(strcmp(argv[1],"-e") == 0)
{
fprintf(stderr,"Please input the string that you want to encrypt(16 hex number):\n");
memset(src16,0,sizeof(src16));
scanf("%s",src16);
fprintf(stderr,"Please input the Key string(16 hex number):\n");
memset(key48,0,sizeof(key48));
scanf("%s",key48);
memset(dest16,0,sizeof(dest16));
Do_3DES(src16,key48,dest16,'e');
fprintf(stderr,"Result: [%s]\n",dest16);
}
else if(strcmp(argv[1],"-d") == 0)
{
fprintf(stderr,"Please input the string that you want to decrypt(16 hex number):\n");
memset(src16,0,sizeof(src16));
scanf("%s",src16);
fprintf(stderr,"Please input the Key string(16 hex number):\n");
memset(key48,0,sizeof(key48));
scanf("%s",key48);
memset(dest16,0,sizeof(dest16));
Do_3DES(src16,key48,dest16,'d');
fprintf(stderr,"Result: [%s]\n",dest16);
}
else
return -1;
}
else
return -1;
return 0;
}
//做DES加密或解密运算
int Do_DES(char* strSrc, char* strKey, char* strDest, char flag)
{
int i,j;
unsigned char subKey[16][48+1],byte8[8+1],bits[64+1],strTmp[64+1];
unsigned char L0[32+1],R0[32+1],Lx[32+1],Rx[32+1];
if(!( flag == 'e' || flag == 'E' || flag == 'd' || flag == 'D'))
return -1;
if(strSrc == NULL || strKey == NULL)
return -2;
if(flag == 'e' || flag == 'E')
{
memset(byte8,0,sizeof(byte8));
BCDToByte(strKey, 16, byte8);
memset(bits,0,sizeof(bits));
ByteToBit(byte8, 8, bits);
Des_GenSubKey(bits,subKey);
BCDToByte(strSrc, 16, byte8);
ByteToBit(byte8, 8, bits);
Des_IP(bits, strTmp);
memcpy(L0,strTmp,32);
memcpy(R0,strTmp+32,32);
for(i=0;i<16;i++)
{
memcpy(Lx,R0,32);
Des_F(R0,subKey[i],Rx);
Do_XOR(L0,32,Rx);
memcpy(L0,Lx,32);
memcpy(R0,Rx,32);
}
memcpy(bits,R0,32);
memcpy(bits+32,L0,32);
Des_IP_1(bits,strTmp);
BitToByte(strTmp,64,byte8);
ByteToBCD(byte8,8,strDest);
}
else
{
memset(byte8,0,sizeof(byte8));
BCDToByte(strKey, 16, byte8);
memset(bits,0,sizeof(bits));
ByteToBit(byte8, 8, bits);
Des_GenSubKey(bits,subKey);
BCDToByte(strSrc, 16, byte8);
ByteToBit(byte8, 8, bits);
Des_IP(bits, strTmp);
memcpy(L0,strTmp,32);
memcpy(R0,strTmp+32,32);
for(i=0;i<16;i++)
{
memcpy(Lx,R0,32);
Des_F(R0,subKey[15-i],Rx);
Do_XOR(L0,32,Rx);
memcpy(L0,Lx,32);
memcpy(R0,Rx,32);
}
memcpy(bits,R0,32);
memcpy(bits+32,L0,32);
Des_IP_1(bits,strTmp);
BitToByte(strTmp,64,byte8);
ByteToBCD(byte8,8,strDest);
}
return 0;
}
//做3DES加密或解密运算
int Do_3DES(char* strSrc, char* strKey, char* strDest, char flag)
{
unsigned char strBCDKey[32+1],strByteKey[16+1];
unsigned char strMidDest1[16+1],strMidDest2[16+1];
unsigned char strLKey[16+1],strMKey[16+1],strRKey[16+1];
if(!( flag == 'e' || flag == 'E' || flag == 'd' || flag == 'D'))
return -1;
if(strSrc == NULL || strKey == NULL)
return -2;
if(strlen(strKey) < 32)
return -3;
if(flag == 'e' || flag == 'E')
{
memset(strBCDKey,0,sizeof(strBCDKey));
memcpy(strBCDKey,strKey,32);
memset(strLKey,0,sizeof(strLKey));
memcpy(strLKey,strBCDKey,16);
memset(strRKey,0,sizeof(strRKey));
memcpy(strRKey,strBCDKey+16,16);
Do_DES(strSrc,strLKey,strMidDest1,'e');
Do_DES(strMidDest1,strRKey,strMidDest2,'d');
Do_DES(strMidDest2,strLKey,strMidDest1,'e');
memcpy(strDest,strMidDest1,16);
}
else
{
memset(strBCDKey,0,sizeof(strBCDKey));
memcpy(strBCDKey,strKey,32);
memset(strLKey,0,sizeof(strLKey));
memcpy(strLKey,strBCDKey,16);
memset(strRKey,0,sizeof(strRKey));
memcpy(strRKey,strBCDKey+16,16);
Do_DES(strSrc,strLKey,strMidDest1,'d');
Do_DES(strMidDest1,strRKey,strMidDest2,'e');
Do_DES(strMidDest2,strLKey,strMidDest1,'d');
memcpy(strDest,strMidDest1,16);
}
return 0;
}
E. 怎么实现DES/3DES加密
这个网上有很多关于3DES和DES的加密算法以及很详细的程序实现代码。
F. 求帮助:下面这段字符串先经过base64解码再用3des解密后得到的数据 帮忙把执行的源代码发出来 谢谢了在线
首先是加密方法
package logic;
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
//import javax.crypto.SecretKey;
import javax.crypto.spec.DESedeKeySpec;
//import javax.crypto.spec.SecretKeySpec;
/*字符串 DESede(3DES) 加密*/
public class ThreeDes {
/**
* @param args在java中调用sun公司提供的3DES加密解密算法时,需要使
* 用到$JAVA_HOME/jre/lib/目录下如下的4个jar包:
*jce.jar
*security/US_export_policy.jar
*security/local_policy.jar
*ext/sunjce_provider.jar
*/
//加密密钥
public final static byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58,
(byte)0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51,
(byte)0xCB,
(byte)0xDD, 0x55, 0x66, 0x77, 0x29, 0x74,
(byte)0x98, 0x30, 0x40, 0x36,
(byte)0xE2 };
//private static final String Algorithm = "DESede/ECB/PKCS5Padding"; //定义加密算法,可用 DES,DESede,Blowfish
//keybyte为加密密钥,长度为24字节
//src为被加密的数据缓冲区(源)
public static byte[] encryptMode(byte[] keybyte,byte[] src){
try {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(keybyte);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("DESede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
//生成密钥
//SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//加密
//Cipher c1 = Cipher.getInstance(Algorithm);
//c1.init(Cipher.ENCRYPT_MODE, deskey);
return cipher.doFinal(src);//在单一方面的加密或解密
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
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 {
Key deskey = null;
//获取密钥
DESedeKeySpec spec = new DESedeKeySpec(keybyte);
//加密算法
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
//生成密码
deskey = keyfactory.generateSecret(spec);
//加入模式和填充方法
Cipher cipher = Cipher.getInstance("DESede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, deskey);
//生成密钥
//SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//解密
//Cipher c1 = Cipher.getInstance(Algorithm);
//c1.init(Cipher.DECRYPT_MODE, deskey);
return cipher.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
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();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//添加新安全算法,如果用JCE就要把它添加进去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
//24字节的密钥
String szSrc = "This is a 3DES test. 测试";
System.out.println("加密前的字符串:" + szSrc);
byte[] encoded = encryptMode(keyBytes,szSrc.getBytes());
System.out.println("加密后的字符串:" + new String(encoded));
byte[] srcBytes = decryptMode(keyBytes,encoded);
System.out.println("解密后的字符串:" + (new String(srcBytes)));
}
}
调用加密方法后给他编码 ~~~
byte[] encoded = ThreeDes.encryptMode(ThreeDes.keyBytes,body.getBytes());
//用 BASE64给密文编码(要不然解密出错)
BASE64Encoder enc = new BASE64Encoder();
String cipherString = enc.encode(encoded);
G. 3DES的算法介绍
3DES又称Triple DES,是DES加密算法的一种模式,它使用3条56位的密钥对数据进行三次加密。数据加密标准(DES)是美国的一种由来已久的加密标准,它使用对称密钥加密法,并于1981年被ANSI组织规范为ANSI X.3.92。DES使用56位密钥和密码块的方法,而在密码块的方法中,文本被分成64位大小的文本块然后再进行加密。比起最初的DES,3DES更为安全。
3DES(即Triple DES)是DES向AES过渡的加密算法(1999年,NIST将3-DES指定为过渡的加密标准),加密算法,其具体实现如下:设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代表密文,这样:
3DES加密过程为:C=Ek3(Dk2(Ek1(P)))
3DES解密过程为:P=Dk1(EK2(Dk3(C)))