Des加密和解密c语言

在Solaris下的系统中,有一个C做的加密工具,调用Sunwcry的des(1)对文件进行加密,然后用java对文件进行解密。java中使用标准的DES/CBC/NoPadding算法,但是解密后发现开头8个字节的数据是错误的。请给我一些建议。

用于加密的C程序。

用于解密的C程序。

TestDescbc.java:用于解密的java程序。

Test01.dat原始文件

Test03.dat cbc_encrypt加密文件

Test05.dat cbc_decrypt解密文件

Test06.dat TestDescbc解密文件

DES加密算法的c语言实现 #includeiostream.h

Class SubKey{ //将子项定义为一个类。

公共:

int key[8][6];

}子项[16];//定义子项对象的数组。

DES级

int加密_解密;//判断是加密还是解密。

int key _ in[8][8];//用户最初输入的64位二进制数。

int key _ out[8][7];//删除每行的最后一个奇偶校验位。

int c0 _ d0[8][7];//存储PC-1转换的56位数据。

int c0[4][7],d0[4][7];//分别存储C0和D0

int text[8][8];//64位明文

int text _ IP[8][8];//IP转换后的明文

int A[4][8],B[4][8];///A、B and B分别存储IP转换后的两部分明文,方便交换。

int temp[8][6];//存储扩展置换后的48位二进制值。

int temp 1[8][6];//存储与子密钥异或的结果。

int s _ result[8][4];//存储S变换后的32位值。

int text _ p[8][4];//P置换后的32位结果

int secret _ IP[8][8];//逆IP转换后的密文

公共:

void Key _ puting();

void PC _ 1();

int函数(int,int);//异或

void SubKey _ Production();

void IP _ Convert();

void f();

void _ IP _ Convert();

void Out _ secret();

};

void DES::Key _ puting()//获取密钥中对算法有用的56位。

{

Cout "请输入64位密钥(8行8列,奇数为1):\n每行):\ n ";

for(int I = 0;i8;我)

for(int j = 0;j8;j ){

cinkey _ in[I][j];

如果(j!= 7)key _ out[I][j]= key _ in[I][j];

}

}

Void DES::PC_1() //PC-1置换函数

{

int pc_1[8][7]={ //PC-1

{57, 49, 41, 33, 25, 17, 9},

{1, 58, 50, 42, 34, 26, 18},

{10, 2, 59, 51, 43, 35, 27},

{19, 11, 3, 60, 52, 44, 36},

{63, 55, 47, 39, 31, 23, 15},

{7, 62, 54, 46, 38, 30, 22},

{14, 6, 61, 53, 45, 37, 29},

{21, 13, 5, 28, 20, 12, 4}

};

int i,j;

for(I = 0;i8;我)

for(j = 0;j7;j)

c0 _ d0[I][j]= key _ out[(PC _ 1[I][j]-1)/8][(PC _ 1[I][j]-1)% 8];

}

Int DES::function(int a,int b) //模拟二进制数的异或运算,其中a和b为整数0和1,返回值为整数0或1。

{

如果(a!=b)返回1;

否则返回0;

}

Void DES::SubKey_Production() //生成子密钥

{

Int move[16][2]={ //循环左移的位数。

1 , 1 , 2 , 1 ,

3 , 2 , 4 , 2 ,

5 , 2 , 6 , 2 ,

7 , 2 , 8 , 2 ,

9 , 1, 10 , 2,

11 , 2, 12 , 2,

13 , 2, 14 , 2,

15 , 2, 16 , 1

};

int pc_2[8][6]={ //PC-2

14, 17 ,11 ,24 , 1 , 5,

3 ,28 ,15 , 6 ,21 ,10,

23, 19, 12, 4, 26, 8,

16, 7, 27, 20 ,13 , 2,

41, 52, 31, 37, 47, 55,

30, 40, 51, 45, 33, 48,

44, 49, 39, 56, 34, 53,

46, 42, 50, 36, 29, 32

};

for(int I = 0;i16I) //生成一个子密钥

{

int j,k;

int a[2],b[2];

int bb[28],cc[28];

for(j = 0;j4;j)

for(k = 0;k7;k)

c0[j][k]= c0 _ d0[j][k];

for(j = 4;j8;j)

for(k = 0;k7;k)

d0[j-4][k]= c0 _ d0[j][k];

for(j = 0;j4;j)

for(k = 0;k7;k ){

bb[7 * j k]= c0[j][k];

cc[7 * j k]= d0[j][k];

}

for(j = 0;jmove[I][1];j ){

a[j]= bb[j];

b[j]= cc[j];

}

for(j = 0;j28-移动[I][1];j ){

bb[j]= bb[j 1];

cc[j]= cc[j 1];

}

for(j = 0;jmove[I][1];j ){

bb[27-j]= a[j];

cc[27-j]= b[j];

}

for(j = 0;j28j ){

c0[j/7][j % 7]= bb[j];

d0[j/7][j % 7]= cc[j];

}

for(j = 0;j4;J)//l123-l128是将c0和d0合并成c0_d0。

for(k = 0;k7;k)

c0 _ d0[j][k]= c0[j][k];

for(j = 4;j8;j)

for(k = 0;k7;k)

c0 _ d0[j][k]= d0[j-4][k];

for(j = 0;j8;J) //取代CI和DI的PC-2。

for(k = 0;K6;k)

子项[i]。key[j][k]= c0 _ d0[(PC _ 2[j][k]-1)/7][(PC _ 2[j][k]-1)% 7];

}

}

void DES::IP_Convert()

{

Int IP[8][8]={ //初始置换IP矩阵

58, 50, 42, 34, 26, 18, 10, 2,

60, 52, 44, 36, 28, 20, 12, 4,

62, 54, 46, 38, 30, 22, 14, 6,

64, 56, 48, 40, 32, 24, 16, 8,

57, 49, 41, 33, 25, 17, 9, 1,

59, 51, 43, 35, 27, 19, 11, 3,

61, 53, 45, 37, 29, 21, 13, 5,

63, 55, 47, 39, 31, 23, 15, 7

};

Cout“您好,请问您是要加密还是解密?加密请按1(输入1),解密请按2,确认。”\ n ';

cinencipher _ decipher

char * s;

if(if(encrypt _ deciper = = 1)s = 1)s = "明文";

Else s= "密文";

Cout "请输入64位“s”(二进制):\ n ";

int i,j;

for(I = 0;i8;我)

for(j = 0;j8;j)

CIN text[I][j];

for(I = 0;i8;I) // IP转换。

for(j = 0;j8;j)

text _ IP[I][j]= text[(IP[I][j]-1)/8][(IP[I][j]-1)% 8];

}

求用c语言写的DES加密解密源代码 我是从别的地方抄来的。

#包含“stdio.h”

#包含“time.h”

#包含" stdlib.h "

#define PLAIN_FILE_OPEN_ERROR -1

#define KEY_FILE_OPEN_ERROR -2

#定义密码文件打开错误-3

#定义OK 1;

typedef char ElemType

/*初始替换表IP */

int IP_Table[64] = { 57,49,41,33,25,17,9,1,

59,51,43,35,27,19,11,3,

61,53,45,37,29,21,13,5,

63,55,47,39,31,23,15,7,

56,48,40,32,24,16,8,0,

58,50,42,34,26,18,10,2,

60,52,44,36,28,20,12,4,

62,54,46,38,30,22,14,6};

/*逆初始替换表IP-1 */

int IP_1_Table[64] = {39,7,47,15,55,23,63,31,

38,6,46,14,54,22,62,30,

37,5,45,13,53,21,61,29,

36,4,44,12,52,20,60,28,

35,3,43,11,51,19,59,27,

34,2,42,10,50,18,58,26,

33,1,41,9,49,17,57,25,

32,0,40,8,48,16,56,24};

/*扩展替换表E */

int E_Table[48] = {31,0,1,2,3,4,

3, 4, 5, 6, 7, 8,

7, 8,9,10,11,12,

11,12,13,14,15,16,

15,16,17,18,19,20,

19,20,21,22,23,24,

23,24,25,26,27,28,

27,28,29,30,31, 0};

/*置换函数P */

int P_Table[32] = {15,6,19,20,28,11,27,16,

0,14,22,25,4,17,30,9,

1,7,23,13,31,26,2,8,

18,12,29,5,21,10,3,24};

/* S框*/

int S[8][4][16] =/* S1 */

{{{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},

{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},

{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},

{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}},

/* S2 */

{{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},

{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},

{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},

{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}},

/* S3 */

{{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},

{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},

{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},

{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}},

/* S4 */

{{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},

{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},

{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},

{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}},

/* S5 */

{{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},

{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},

{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},

{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}},

/* S6 */

{{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},

{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},

{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},

{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}},

/* S7 */

{{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},

{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},

{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},

{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}},

/* S8 */

{{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},

{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},

{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},

{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}}};

/*替换选择1 */

int PC_1[56] = {56,48,40,32,24,16,8,

0,57,49,41,33,25,17,

9,1,58,50,42,34,26,

18,10,2,59,51,43,35,

62,54,46,38,30,22,14,

6,61,53,45,37,29,21,

13,5,60,52,44,36,28,

20,12,4,27,19,11,3};

/*替换选项2 */

int PC_2[48] = {13,16,10,23,0,4,2,27,

14,5,20,9,22,18,11,3,

25,7,15,6,26,19,12,1,

40,51,30,36,46,54,29,39,

50,44,32,46,43,48,38,55,

33,52,45,41,49,35,28,31};

/*关于左移次数的规定*/

int MOVE_TIMES[16] = {1,1,2,2,2,2,2,1,2,2,2,2,2,2,2,1 };

int ByteToBit(ElemType ch,elem type bit[8]);

int BitToByte(ElemType位[8],elem type * ch);

int Char8ToBit64(ElemType ch[8],elem type bit[64]);

int bit 64 tochar 8(elem type bit[64],elem type ch[8]);

int DES _ make subKeys(elem type key[64],elem type subKeys[16][48]);

int DES _ PC1 _ Transform(elem type key[64],elem type tempbts[56]);

int DES _ PC2 _ Transform(elem type key[56],elem type tempbts[48]);

int DES_ROL(ElemType data[56],int time);

int DES _ IP _ Transform(elem type data[64]);

int DES _ IP _ 1 _ Transform(elem type data[64]);

int DES _ E _ Transform(elem type data[48]);

int DES _ P _ Transform(elem type data[32]);

int DES _ SBOX(elem type data[48]);

int DES_XOR(ElemType R[48],ElemType L[48],int count);

int DES_Swap(ElemType left[32],elem type right[32]);

int DES _ encrypt block(elem type plain block[8],ElemType subKeys[16][48],elem type cipher block[8]);

int DES _ decrypt block(elem type cipher block[8],ElemType subKeys[16][48],elem type plain block[8]);

int DES_Encrypt(char *plainFile,char *keyStr,char * cipher file);

int DES_Decrypt(char *cipherFile,char *keyStr,char * plain file);

/*字节转换为二进制*/

int ByteToBit(ElemType ch,ElemType bit[8]){

int cnt

for(CNT = 0;CNT 8;cnt ){

*(bit CNT)=(ch CNT)1;

}

返回0;

}

/*二进制到字节*/

int BitToByte(ElemType位[8],ElemType *ch){

int cnt

for(CNT = 0;CNT 8;cnt ){

* ch | = *(bit CNT)CNT;

}

返回0;

}

/*将长度为8的字符串转换为二进制位字符串*/

int Char8ToBit64(ElemType ch[8],ElemType bit[64]){

int cnt

for(CNT = 0;CNT 8;cnt ){

ByteToBit(*(ch cnt),bit(CNT 3));

}

返回0;

}

/*将二进制位字符串转换为长度为8的字符串*/

int bit 64 tochar 8(elem type bit[64],ElemType ch[8]){

int cnt

memset(ch,0,8);

for(CNT = 0;CNT 8;cnt ){

BitToByte(位(cnt3),通道计数);

}

返回0;

}

/*生成子项*/

int DES _ make subKeys(elem type key[64],ElemType subKeys[16][48]){

elem type temp[56];

int cnt

DES_PC1_Transform(key,temp);/* PC1的更换*/

for(CNT = 0;cnt 16Cnt ){/* 16轮替换,产生16个子键*/

DES_ROL(temp,MOVE _ TIMES[CNT]);/*向左循环*/

DES_PC2_Transform(temp,subKeys[CNT]);/* PC2置换以生成子密钥*/

}

返回0;

}

/*密钥排列1 */

int DES _ PC1 _ Transform(elem type key[64],ElemType tempbts[56]){

int cnt

for(CNT = 0;cnt 56cnt ){

tempbts[CNT]= key[PC _ 1[CNT]];

}

返回0;

}

/*密钥排列2 */

int DES _ PC2 _ Transform(elem type key[56],ElemType tempbts[48]){

int cnt

for(CNT = 0;cnt 48cnt ){

tempbts[CNT]= key[PC _ 2[CNT]];

}

返回0;

}

/*向左循环*/

int DES_ROL(ElemType data[56],int time){

elem type temp[56];

/*保存要循环向右移动的位*/

memcpy(温度、数据、时间);

memcpy(临时时间,数据28,时间);

/*前28位移位*/

memcpy(数据,数据时间,28次);

memcpy(数据28-time,temp,time);

/*移动最后28位*/

memcpy(数据28,数据28次,28次);

memcpy(数据56次,临时时间,时间);

返回0;

}

/* IP更换*/

int DES _ IP _ Transform(elem type data[64]){

int cnt

elem type temp[64];

for(CNT = 0;cnt 64cnt ){

temp[CNT]= data[IP _ Table[CNT]];

}

memcpy(数据,温度,64);

返回0;

}

/* IP逆置换*/

int DES _ IP _ 1 _ Transform(elem type data[64]){

int cnt

elem type temp[64];

for(CNT = 0;cnt 64cnt ){

temp[CNT]= data[IP _ 1 _ Table[CNT]];

}

memcpy(数据,温度,64);

返回0;

}

/*扩展置换*/

int DES _ E _ Transform(elem type data[48]){

int cnt

elem type temp[48];

for(CNT = 0;cnt 48cnt ){

temp[CNT]= data[E _ Table[CNT]];

}

memcpy(数据,温度,48);

返回0;

}

/* P置换*/

int DES _ P _ Transform(elem type data[32]){

int cnt

elem type temp[32];

for(CNT = 0;cnt 32cnt ){

temp[CNT]= data[P _ Table[CNT]];

}

memcpy(数据,温度,32);

返回0;

}

/*异或*/

int DES_XOR(ElemType R[48],ElemType L[48],int count){

int cnt

for(CNT = 0;计数;cnt ){

^=·l[CNT];

}

返回0;

}

/* S盒更换*/

int DES_SBOX(ElemType data[48]){

int cnt

int line,row,output

int cur1,cur2

for(CNT = 0;CNT 8;cnt ){

cur 1 = CNT * 6;

cur2 = cnt2

/*计算S框中的行和列*/

line =(data[cur 1]1)data[cur 1 5];

row =(data[cur 1 1]3)(data[cur 1 2]2)

(data[cur 1 3]1)data[cur 1 4];

output = S[CNT][line][row];

/*到二进制*/

data[cur 2]=(output 0x 08)3;

data[cur 2 1]=(output 0x 04)2;

data[cur 2 2]=(output 0x 02)1;

data[cur2 3] =输出0x 01;

}

返回0;

}

/*交换*/

int DES_Swap(ElemType left[32],ElemType right[32]){

elem type temp[32];

memcpy(温度,左侧,32);

memcpy(左,右,32);

memcpy(右,温度,32);

返回0;

}

/*加密单个数据包*/

int DES _ encrypt block(elem type plain block[8],ElemType subKeys[16][48],ElemType cipherBlock[8]){

elem type plain bits[64];

ElemType版权[48];

int cnt

Char8ToBit64(plainBlock,plain bits);

/*初始替换(IP替换)*/

DES _ IP _ Transform(plain bits);

/* 16次迭代*/

for(CNT = 0;cnt 16cnt ){

memcpy(版权所有,plainBits 32,32);

/*将右半部分从32位扩展到48位*/

DES_E_Transform(版权所有);

/*将右半部分与子密钥进行异或运算*/

DES_XOR(版权所有,subKeys[cnt],48);

/* XOR结果进入S盒并输出32位结果*/

DES_SBOX(版权所有);

/* P置换*/

DES_P_Transform(版权所有);

/*对明文的左右部分进行异或运算*/

DES_XOR(plainBits,版权,32);

如果(cnt!= 15){

/*最后完成左右部分的交换*/

DES_Swap(plainBits,plain bits 32);

}

}

/*逆初始排列(IP^1排列)*/

DES _ IP _ 1 _ Transform(plain bits);

Bit64ToChar8(明文位,密码块);

返回0;

}

/*解密单个数据包*/

int DES _ decrypt block(elem type cipher block[8],ElemType subKeys[16][48],ElemType plainBlock[8]){

元素型密码比特[64];

ElemType版权[48];

int cnt

Char8ToBit64(密码块,密码位);

/*初始替换(IP替换)*/

DES_IP_Transform(密码位);

/* 16次迭代*/

for(CNT = 15;CNT = 0;cnt - ){

memcpy(版权所有,cipherBits 32,32);

/*将右半部分从32位扩展到48位*/

DES_E_Transform(版权所有);

/*将右半部分与子密钥进行异或运算*/

DES_XOR(版权所有,subKeys[cnt],48);

/* XOR结果进入S盒并输出32位结果*/

DES_SBOX(版权所有);

/* P置换*/

DES_P_Transform(版权所有);

/*对明文的左右部分进行异或运算*/

DES_XOR(cipherBits,版权,32);

如果(cnt!= 0){

/*最后完成左右部分的交换*/

DES_Swap(密码位,密码位32);

}

}

/*逆初始排列(IP^1排列)*/

DES_IP_1_Transform(密码位);

Bit64ToChar8(cipherBits,plain block);

返回0;

}

/*加密文件*/

int DES_Encrypt(char *plainFile,char *keyStr,char *cipherFile){

文件*普通,*密码;

int计数;

ElemType plainBlock[8],cipherBlock[8],key block[8];

elem type bKey[64];

ElemType子密钥[16][48];

if((plain = fopen(plainFile," Rb))= = NULL){

返回PLAIN _ FILE _ OPEN _ ERROR

}

if((cipher = fopen(cipherFile," WB))= = NULL){

返回CIPHER _ FILE _ OPEN _ ERROR

}

/*设置密钥*/

memcpy(keyBlock,keyStr,8);

/*将密钥转换为二进制流*/

Char8ToBit64(keyBlock,bKey);

/*生成子项*/

DES_MakeSubKeys(bKey,subKeys);

而(!feof(plain)){

/*一次读取8个字节,返回成功读取的字节数*/

if((count = fread(plainBlock,sizeof(char),8,plain)) == 8){

DES_EncryptBlock(plainBlock,subKeys,cipher block);

fwrite(cipherBlock,sizeof(char),8,密码);

}

}

如果(计数){

/*填充*/

memset(纯块计数,' \0 ',7 -计数);

/*最后一个字符保存包括最后一个字符在内的填充字符数*/

plainBlock[7] = 8计数;

DES_EncryptBlock(plainBlock,subKeys,cipher block);

fwrite(cipherBlock,sizeof(char),8,密码);

}

fclose(素色);

fclose(密码);

退货OK;

}

/*解密文件*/

int DES_Decrypt(char *cipherFile,char *keyStr,char *plainFile){

文件*普通,*密码;

int计数,times = 0;

长fileLen

ElemType plainBlock[8],cipherBlock[8],key block[8];

elem type bKey[64];

ElemType子密钥[16][48];

if((cipher = fopen(cipherFile," Rb))= = NULL){

返回CIPHER _ FILE _ OPEN _ ERROR

}

if((plain = fopen(plainFile," WB))= = NULL){

返回PLAIN _ FILE _ OPEN _ ERROR

}

/*设置密钥*/

memcpy(keyBlock,keyStr,8);

/*将密钥转换为二进制流*/

Char8ToBit64(keyBlock,bKey);

/*生成子项*/

DES_MakeSubKeys(bKey,subKeys);

/*取文件长度*/

fseek(cipher,0,SEEK _ END);/*尾文件指针*/

fileLen = ftell(密码);/*获取文件指针的当前位置*/

倒带(密码);/*将文件指针重定向到文件头*/

while(1){

/*密文的字节数必须是8的整数倍*/

fread(cipherBlock,sizeof(char),8,密码);

DES_DecryptBlock(cipherBlock,subKeys,plain block);

次数= 8;

if(times fileLen){

fwrite(plainBlock,sizeof(char),8,plain);

}

否则{

打破;

}

}

/*判断末端是否填充*/

if(plainBlock[7] 8){

for(count = 8-plain block[7];第7项;计数){

if(plainBlock[count]!= '\0'){

打破;

}

}

}

If(count == 7){/*有填充*/

fwrite(plainBlock,sizeof(char),8 - plainBlock[7],plain);

}

Else{/*无填充*/

fwrite(plainBlock,sizeof(char),8,plain);

}

fclose(素色);

fclose(密码);

退货OK;

}

int main()

{ DES_Encrypt("1.txt "," key.txt "," 2 . txt ");

系统(“暂停”);

DES_Decrypt("2.txt "," key.txt "," 3 . txt ");

getchar();

返回0;

}

相关文章

发表新评论