#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=0i<16i++)
{
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=0i<16i++)
{
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
}
DES 算法和 DESSede 算法统称DES系列算法,是对称加密算法领域的经典加密算法。 DESSede (又称 3DES )使用三次迭代增加算法安全性。DES算法是众多对称加密算法的基础,很多算法都是基于该算法演变而来。
虽然DES被取代了,但是DES的CBC工作模式是基础性的算法和工作模型,有很强的意义,在遗留系统中也有一些使用的。
DES的算法是采用分组加密工作模式,流程比较复杂,大致流程如下:
DES 和 3DES 适合一般加密性场景,当前大部分是遗留系统在使用,还有一部分可能是系统没有支持 AES 等其他加密手段被迫使用。
JDK仅支持 56位的密钥长度 (出口限制),对称加密系列算法的特点是:密钥长度越高安全性越高,因此JDK本身自带的 DES 和 3DES 算法仅适合学术和一般场景使用,Bouncy Castle提供了64位密钥长度的支持。
3DES 是对 DES 的一种改良算法,针对 DES 算法密钥短,迭代次数少的缺点做了改进。但是 3DES 算法速度慢,密钥计算时间长,加密效率不高,实际使用也不多。
DES 的 3 大安全痛点: