作者:finallyliuyu 出处:博客园
主函数调用
注意:目前算法仅能对八的倍数比特明文进行加密。如要实现对任意长度明文加密,需要padding补零,使其成为8的倍数比特
代码
#include "string.h"
void main()
{
printf("finish\n");
char mingwen[8]={'a','b','c','d','e','f','g','h'};
char key[24]={'q','w','r','t','y','u','0','b','v','s','a','z','l',';','y','5','?','3','n','m','3','0','a','q'};
for (int i=0;i<8;i++)
{
printf("%c",mingwen[i]);
}
printf("\n");
char minwen[8];
char descrptedmingwen[8];
myDES des;
des.RunDes(true,mingwen,minwen,8,key,24);
for (int i=0;i<8;i++)
{
printf("%c",minwen[i]);
}
printf("\n");
des.RunDes(false,minwen,descrptedmingwen,8,key,24);
for (int i=0;i<8;i++)
{
printf("%c",descrptedmingwen[i]);
}
int end;
scanf("%d",&end);
上图为运行结果。 原明文(abcdefgh),加密后密文,解密后明文。
#include "myDES.h"
#include "string.h"
void main()
{
printf("finish\n");
char mingwen[8]={'a','b','c','d','e','f','g','h'};
char key[24]={'q','w','r','t','y','u','0','b','v','s','a','z','l',';','y','5','?','3','n','m','3','0','a','q'};
for (int i=0;i<8;i++)
{
printf("%c",mingwen[i]);
}
printf("\n");
char minwen[8];
char descrptedmingwen[8];
myDES des;
des.RunDes(true,mingwen,minwen,8,key,24);
for (int i=0;i<8;i++)
{
printf("%c",minwen[i]);
}
printf("\n");
des.RunDes(false,minwen,descrptedmingwen,8,key,24);
for (int i=0;i<8;i++)
{
printf("%c",descrptedmingwen[i]);
}
int end;
scanf("%d",&end);