C语言实现数据加密算法

时间:2025-04-11

/*-------------------------------------------------------

Data Encryption Standard 56位密钥加密64位数据

2011.10

--------------------------------------------------------*/

#include <stdlib.h>

#include <stdio.h>

#include "bool.h" // 位处理

#include "tables.h"

void BitsCopy(bool *DatOut,bool *DatIn,int Len); // 数组复制

void ByteToBit(bool *DatOut,char *DatIn,int Num); // 字节到位

void BitToByte(char *DatOut,bool *DatIn,int Num); // 位到字节

void BitToHex(char *DatOut,bool *DatIn,int Num); // 二进制到十六进制 64位 to 4*16字符 void HexToBit(bool *DatOut,char *DatIn,int Num); // 十六进制到二进制

void TablePermute(bool *DatOut,bool *DatIn,const char *Table,int Num); // 位表置换函数 void LoopMove(bool *DatIn,int Len,int Num); // 循环左移 Len长度 Num移动位数 void Xor(bool *DatA,bool *DatB,int Num); // 异或函数

void S_Change(bool DatOut[32],bool DatIn[48]); // S盒变换

void F_Change(bool DatIn[32],bool DatKi[48]); // F函数

void SetKey(char KeyIn[8]); // 设置密钥

void PlayDes(char MesOut[8],char MesIn[8]); // 执行DES加密

void KickDes(char MesOut[8],char MesIn[8]); // 执行DES解密

int main()

{

int i=0;

char MesHex[16]={0}; // 16个字符数组用于存放 64位16进制的密文

char MyKey[8]={0}; // 初始密钥 8字节*8

char YourKey[8]={0}; // 输入的解密密钥 8字节*8

char MyMessage[8]={0}; // 初始明文

/*-----------------------------------------------*/

printf("Welcome! Please input your Message(64 bit):\n");

gets(MyMessage); // 明文

printf("Please input your Secret Key:\n");

gets(MyKey); // 密钥

while(MyKey[i]!='\0') // 计算密钥长度

{

i++;

}

while(i!=8) // 不是8 提示错误

{

printf("Please input a correct Secret Key!\n");

gets(MyKey);

i=0;

while(MyKey[i]!='\0') // 再次检测

{

i++;

}

}

SetKey(MyKey); // 设置密钥 得到子密钥Ki

PlayDes(MesHex,MyMessage); // 执行DES加密

printf("Your Message is Encrypted!:\n"); // 信息已加密

for(i=0;i<16;i++)

{

printf("%c ",MesHex[i]);

}

printf("\n");

printf("\n");

printf("Please input your Secret Key to Deciphering:\n"); // 请输入密钥以解密 gets(YourKey); // 得到密钥

SetKey(YourKey); // 设置密钥

KickDes(MyMessage,MesHex); // 解密输出到MyMessage

printf("Deciphering Over !!:\n"); // 解密结束

for(i=0;i<8;i++)

{

printf("%c ",MyMessage[i]);

}

printf("\n");

system("pause");

/*------------------------------------------------*/

/*-------------------------------

把DatIn开始的长度位Len位的二进制

复制到DatOut后

--------------------------------*/

void BitsCopy(bool *DatOut,bool *DatIn,int Len) // 数组复制 OK

{

int i=0;

for(i=0;i<Len;i++)

{

DatOut[i]=DatIn[i];

}

}

/*-------------------------------

字节转换成位函数

每8次换一个字节 每次向右移一位

和1与取最后一位 共64位

--------------------------------*/

void ByteToBit(bool *DatOut,char *DatIn,int Num)

{

int i=0;

for(i=0;i<Num;i++)

{

DatOut[i]=(DatIn[i/8]>>(i%8))&0x01;

}

}

/*-------------------------------

位转换成字节函数

字节数组每8次移一位

位每次向左移 与上一次或

---------------------------------*/

void BitToByte(char *DatOut,bool *DatIn,int Num)

{

int i=0;

for(i=0;i<(Num/8);i++)

{

DatOut[i]=0;

}

for(i=0;i<Num;i++)

{

DatOut[i/8]|=DatIn[i]<<(i%8); // OK // OK

}

}

/*----------------------------------

二进制密文转换为十六进制

需要16个字符表示

-----------------------------------*/

void BitToHex(char *DatOut,bool *DatIn,int Num)

{

int i=0;

for(i=0;i<Num/4;i++)

{

DatOut[i]=0;

}

for(i=0;i<Num/4;i++)

{

DatOut[i] = DatIn[i*4]+(DatIn[i*4+1]<<1)

+(DatIn[i*4+2]<<2)+(DatIn[i*4+3]<<3);

if((DatOut[i]%16)>9)

{

DatOut[i]=DatOut[i]%16+'7'; // 余数大于9时处理 10-15 to A-F } // 输出字符

else

{

DatOut[i]=DatOut[i]%16+'0'; // 输出字符

}

}

}

/*---------------------------------------------

十六进制字符转二进制

----------------------------------------------*/

void HexToBit(bool *DatOut,char *DatIn,int Num)

{

int i=0; // 字符型输入

for(i=0;i<Num;i++)

{

if((DatIn[i/4])>'9') // 大于9

{

DatOut[i]=((DatIn[i/4]-'7')>>(i%4))&0x01;

}

else

{

DatOut[i]=((DatIn[i/4]-'0')>>(i%4))&0x01;

}

}

}

// 表置换函数 OK

void TablePermute(bool *DatOut,bool *DatIn,const char *Table,int Num)

{

int i=0;

static boo …… 此处隐藏:4206字,全部文档内容请下载后查看。喜欢就下载吧 ……

C语言实现数据加密算法.doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219