DES加密算法C 实现
DES算法的c++实现
DES算法的c++实现⽤数组存的位,改天⽤unsigned int重写下。
依然不负责填充。
#include<stdio.h>#include<string.h>int IP_Table[64] = { //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 };int E_Table[48] = { //扩展矩阵32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 };int P_Table[32] = { // P 盒16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25};int IPR_Table[64] = { //逆IP置换矩阵40, 8, 48, 16, 56, 24, 64, 32, 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 };int PC1_Table[56] = { //密钥第⼀次置换矩阵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 PC2_Table[48] = { // 密钥第⼆次置换矩阵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 };int S_Box[8][4][16]={ //8个S盒三维数组// S114, 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,// S215, 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,// S310, 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,// S47, 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,// S52, 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,// S612, 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,// S74, 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,// S813, 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};//把CHAR转换为INTstatic void CharToBit(const char input[],int output[],int bits){int i,j;for(j=0;j<8;j++){for(i=0;i<8;i++){output[8*j+7-i]=(input[j]>>i)&1;}}};//把INT转换为CHARstatic void BitToChar(const int intput[],char output[],int bits){int i,j;for(j=0;j<8;j++){for(i=0;i<8;i++){output[j]=output[j]*2+intput[i+8*j];}}};//异或操作static void Xor(int *INA,int *INB,int len){int i;for(i=0;i<len;i++){*(INA+i)=*(INA+i)^*(INB+i);}};//初始IP置换,64->64,左右分别32static void IP(const int input[64],int output[64],int table[64]){int i;for(i=0;i<64;i++){output[i]=input[table[i]-1];//减1操作不可少!!}};//E扩展32->48static void E(const int input[32],int output[48],int table[48]){int i;for(i=0;i<48;i++){output[i]=input[table[i]-1];}};//P置换,static void P(const int input[32],int output[32],int table[32]){int i;for(i=0;i<32;i++){output[i]=input[table[i]-1];}};//逆IPstatic void IP_In(const int input[64],int output[64],int table[64]){int i;for(i=0;i<64;i++){output[i]=input[table[i]-1];}};//PC_1,⽤于密钥置换static void PC_1(const int input[64],int output[56],int table[56]){int i;for(i=0;i<56;i++){output[i]=input[table[i]-1];}};//PC_2static void PC_2(const int input[56],int output[48],int table[48]){int i;for(i=0;i<48;i++){output[i]=input[table[i]-1];}};//S盒压缩static void S(const int input[48],int output[32],int table[8][4][16]){int i=0;int j=0;int INT[8];for(;i<48;i+=6){INT[j]=table[j][(input[i]<<1)+(input[i+5])][(input[i+1]<<3)+(input[i+2]<<2)+(input[i+3]<<1)+(input[i+4])]; j++;}for(j=0;j<8;j++){for(i=0;i<4;i++){output[4*j+3-i]=(INT[j]>>i)&1;}}};//完成DES算法轮变换,就是f函数的内部实现,就是⼀个异或⽽已static void F_func(int input[32],int output[32], int subkey[48]){int len=48;int temp[48]={0};int temp_1[32]={0};E(input,temp,E_Table);//32->48Xor(temp,subkey,len);S(temp,temp_1,S_Box);//48->32P(temp_1,output,P_Table);//位数不变};//秘钥循环左移static void RotateL(const int input[28],int output[28], int leftCount){int i;int len=28;//因为不是位运算,所以可以不⽤unsignedfor(i=0;i<len;i++){output[i]=input[(i+leftCount)%len];}};//⼦密钥⽣成static void subKey_fun(const int input[64],int Subkey[16][48]){//注意输⼊和输出的位数,int只存放01,密钥为18位16轮int loop=1,loop_2=2;int i,j;int c[28],d[28];int pc_1[56]={0};int pc_2[16][56]={0};int rotatel_c[16][28]={0};int rotatel_d[16][28]={0};PC_1(input,pc_1,PC1_Table);for(i=0;i<28;i++){c[i]=pc_1[i];//Ld[i]=pc_1[i+28];//R}int leftCount=0;for(i=1;i<17;i++){if(i==1||i==2||i==9||i==16){//左移⼀位leftCount+=loop;RotateL(c,rotatel_c[i-1],leftCount);RotateL(d,rotatel_d[i-1],leftCount);}else{//左移两位leftCount += loop_2;RotateL(c,rotatel_c[i-1],leftCount);RotateL(d,rotatel_d[i-1],leftCount);}}for(i=0;i<16;i++){for(j=0;j<28;j++){pc_2[i][j]=rotatel_c[i][j];pc_2[i][j+28]=rotatel_d[i][j];}}for(i=0;i<16;i++){PC_2(pc_2[i],Subkey[i],PC2_Table);}};//加密实现static void DES_Efun(char input[8],char key_in[8],int output[64]){int Ip[64]={0};//存储初始置换后的矩阵int output_1[64]={0};int subkeys[16][48];int chartobit[64]={0};int key[64];int l[17][32],r[17][32];CharToBit(input,chartobit,8);//转换为64个⼆进制数IP(chartobit,Ip,IP_Table);//IP初始置换!CharToBit(key_in,key,8);subKey_fun(key,subkeys);for(int i=0;i<32;i++){l[0][i]=Ip[i];r[0][i]=Ip[32+i];}for(int j=1;j<16;j++){//前15轮的操作for(int k=0;k<32;k++){l[j][k]=r[j-1][k];}F_func(r[j-1],r[j],subkeys[j-1]);Xor(r[j],l[j-1],32);}int t=0;for(t=0;t<32;t++){//最后⼀轮的操作,合并了将l,r swapr[16][t]=r[15][t];}F_func(r[15],l[16],subkeys[15]);Xor(l[16],l[15],32);for(t=0;t<32;t++){output_1[t]=l[16][t];output_1[32+t]=r[16][t];}IP_In(output_1,output,IPR_Table);};//解密实现static void DES_Dfun(int input[64],char key_in[8],char output[8]){ int Ip[64]={0};//存储初始置换后的矩阵int output_1[64]={0};int output_2[64]={0};int subkeys[16][48];//int chartobit[64]={0};int key[64];int l[17][32],r[17][32];IP(input,Ip,IP_Table);//IP初始置换CharToBit(key_in,key,8);subKey_fun(key,subkeys);for(int i=0;i<32;i++){l[0][i]=Ip[i];r[0][i]=Ip[32+i];}for(int j=1;j<16;j++){//前15轮的操作for(int k=0;k<32;k++){l[j][k]=r[j-1][k];}F_func(r[j-1],r[j],subkeys[16-j]);Xor(r[j],l[j-1],32);}int t=0;for(t=0;t<32;t++){//最后⼀轮的操作r[16][t]=r[15][t];}F_func(r[15],l[16],subkeys[0]);Xor(l[16],l[15],32);for(t=0;t<32;t++){output_1[t]=l[16][t];output_1[32+t]=r[16][t];}IP_In(output_1,output_2,IPR_Table);BitToChar(output_2,output,8);};int main(){int output[64]={0};char MIN[9]={0};char MI[9]={0};printf("请输⼊明⽂\n");gets(MIN);printf("请输⼊秘钥\n");gets(MI);DES_Efun(MIN,MI,output);printf("密⽂如下:\n");for(int i=0;i<64;i++){printf("%d",output[i]);if((i+1)%4==0)printf("\n");}printf("\n");printf("解密功能\n");DES_Dfun(output,MI,MIN);printf("明⽂如下:\n");for(int i=0;i<8;i++){printf("%c",MIN[i]);}printf("\n");return0;}。
加密算法 c语言
加密算法 c语言C语言是一种广泛应用于计算机科学领域的编程语言,它具有高效、灵活、可靠的特点。
在密码学中,加密算法是一种用于保护信息安全的重要工具。
本文将介绍一些常见的加密算法和在C语言中的实现。
一、对称加密算法对称加密算法是一种使用相同的密钥进行加密和解密的算法。
其中,最常见的对称加密算法是DES(Data Encryption Standard)和AES(Advanced Encryption Standard)。
1. DES算法DES算法是一种将64位明文加密为64位密文的块加密算法。
它使用56位的密钥和一系列的置换、替换和移位操作来进行加密。
C语言中可以使用openssl库中的函数来实现DES算法的加密和解密。
2. AES算法AES算法是一种使用128位、192位或256位密钥进行加密和解密的块加密算法。
它使用一系列的置换、替换和线性变换操作来进行加密。
C语言中可以使用openssl库中的函数来实现AES算法的加密和解密。
二、非对称加密算法非对称加密算法是一种使用不同的密钥进行加密和解密的算法。
其中,最常见的非对称加密算法是RSA(Rivest-Shamir-Adleman)算法。
1. RSA算法RSA算法是一种基于数论的非对称加密算法。
它使用一对公钥和私钥来进行加密和解密。
C语言中可以使用openssl库中的函数来实现RSA算法的加密和解密。
三、散列函数散列函数是一种将任意长度的输入映射为固定长度输出的算法。
其中,最常见的散列函数是MD5(Message Digest Algorithm 5)和SHA(Secure Hash Algorithm)系列算法。
1. MD5算法MD5算法是一种广泛使用的散列函数,它将任意长度的输入映射为128位的输出。
C语言中可以使用openssl库中的函数来实现MD5算法。
2. SHA算法SHA算法是一系列散列函数,其中最常见的是SHA-1、SHA-256和SHA-512。
DES加密算法 C++版
/*** Des加密算法初步完成版* 使用基础函数实现** @version 0.9* @author alexWu* @电子邮箱:alexthinking(at)* @新浪微博:/vikingalex* @完成日期:2011-10-25**/#include <cstdlib>#include <iostream>using namespace std;//轮密钥,由轮密钥生成器getKeys(key)获得并赋值int keys[16][48];//密匙置换选择1表,调用:getKeysint keyTable11[4][7] = {{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}};int keyTable12[4][7] = {{63, 55, 47, 39, 31, 33, 15},{7, 62, 54, 46, 38, 30, 22},{14, 6, 61, 53, 45, 37, 29},{21, 13, 5, 28, 20, 12, 4}};//密匙置换选择2表,调用:getKeysint keyTable2[8][6] = {{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}};//明文初始置换,调用:encryptint displaceTable[8][8] = {{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}};//明文逆初始置换,调用:encryptint antiDisplaceTable[8][8] = {{40, 8, 48, 16, 56, 24, 64, 32},{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}};//扩展变换E,在F变换中调用:fTrans()int expandTable[8][6] = {{32, 1, 2, 3, 4, 5},{4, 5, 6, 7, 8, 9},{8, 9, 10, 11, 12, 13},{12, 13, 14, 15, 16, 17},{16, 17, 18, 19, 20, 21},{20, 21, 22, 23, 24, 25},{24, 25, 26, 27, 28, 29},{28, 29, 30, 31, 32, 1}};//S盒压缩,调用:sBoxTransint sBox[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} }};//置换变换P,调用:fTransint pTable[8][4] = {{16, 7, 20, 21},{29, 12, 28, 17},{1, 15, 23, 26},{5, 18, 31, 10},{2, 8, 24, 14},{32, 27, 3, 9},{19, 13, 30, 6},{22, 11, 4, 25}};//将16进制字符串转换为二进制整形数组void HexToBin(char content[], int intArr[]) {for (int i = 0; i < 16; i++) {switch (content[i]) {case '0': intArr[i*4]=0; intArr[i*4+1]=0; intArr[i*4+2]=0; intArr[i*4+3]=0; break;case '1': intArr[i*4]=0; intArr[i*4+1]=0; intArr[i*4+2]=0; intArr[i*4+3]=1; break;case '2': intArr[i*4]=0; intArr[i*4+1]=0; intArr[i*4+2]=1; intArr[i*4+3]=0; break;case '3': intArr[i*4]=0; intArr[i*4+1]=0; intArr[i*4+2]=1; intArr[i*4+3]=1; break;case '4': intArr[i*4]=0; intArr[i*4+1]=1; intArr[i*4+2]=0; intArr[i*4+3]=0; break;case '5': intArr[i*4]=0; intArr[i*4+1]=1; intArr[i*4+2]=0; intArr[i*4+3]=1; break;case '6': intArr[i*4]=0; intArr[i*4+1]=1; intArr[i*4+2]=1; intArr[i*4+3]=0; break;case '7': intArr[i*4]=0; intArr[i*4+1]=1; intArr[i*4+2]=1; intArr[i*4+3]=1; break;case '8': intArr[i*4]=1; intArr[i*4+1]=0; intArr[i*4+2]=0; intArr[i*4+3]=0; break;case '9': intArr[i*4]=1; intArr[i*4+1]=0; intArr[i*4+2]=0; intArr[i*4+3]=1; break;case 'A': intArr[i*4]=1; intArr[i*4+1]=0; intArr[i*4+2]=1; intArr[i*4+3]=0; break;case 'B': intArr[i*4]=1; intArr[i*4+1]=0; intArr[i*4+2]=1; intArr[i*4+3]=1; break;case 'C': intArr[i*4]=1; intArr[i*4+1]=1; intArr[i*4+2]=0; intArr[i*4+3]=0; break;case 'D': intArr[i*4]=1; intArr[i*4+1]=1; intArr[i*4+2]=0; intArr[i*4+3]=1; break;case 'E': intArr[i*4]=1; intArr[i*4+1]=1; intArr[i*4+2]=1; intArr[i*4+3]=0; break;case 'F': intArr[i*4]=1; intArr[i*4+1]=1; intArr[i*4+2]=1; intArr[i*4+3]=1; break;}}}//将二进制整形数组转换为16进制字符串void BinToHex(int intArr[], char content[], int hexLen) {int tep;for (int i = 0; i < hexLen; i++) {tep = intArr[i*4]*8 + intArr[i*4+1]*4 + intArr[i*4+2]*2 + intArr[i*4+3];switch (tep) {case 0: content[i]='0'; break;case 1: content[i]='1'; break;case 2: content[i]='2'; break;case 3: content[i]='3'; break;case 4: content[i]='4'; break;case 5: content[i]='5'; break;case 6: content[i]='6'; break;case 7: content[i]='7'; break;case 8: content[i]='8'; break;case 9: content[i]='9'; break;case 10: content[i]='A'; break;case 11: content[i]='B'; break;case 12: content[i]='C'; break;case 13: content[i]='D'; break;case 14: content[i]='E'; break;case 15: content[i]='F'; break;}}if (hexLen < strlen(content)) {for (int i = hexLen; i < strlen(content); i++) {content[i]=' ';}}}//将密钥中C和D合并void plusArray(int c[], int d[], int result[], int len) {for (int i = 0; i < len; i++) {result[i] = c[i];}for (int i = 0; i < len; i++) {result[i + len] = d[i];}}//密钥置换选择1void keyTableTrans1(int binArr[64], int resultArr[28], int table[4][7]){ int count = 0;for (int i = 0; i < 4; i++) {for (int j = 0; j < 7; j++) {resultArr[count++] = binArr[table[i][j]-1];}}}//密钥置换选择2void keyTableTrans2(int binArr[56], int resultArr[16][48], int table[8][6], int index){ int count = 0;for (int i = 0; i < 8; i++) {for (int j = 0; j < 6; j++) {resultArr[index][count++] = binArr[table[i][j]-1];}}}//循环左移void leftMove(int count, int arr[]) {int arrLen = 28;int moveTable[16] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};if (moveTable[count] == 1) {int tep0 = arr[0];for (int i = 0; i < arrLen - 1; i++) {arr[i] = arr[i+1];}arr[arrLen-1] = tep0;} else {int tep0 = arr[0];int tep1 = arr[1];for (int i = 0; i < arrLen - 2; i++) {arr[i] = arr[i+2];}arr[arrLen-2] = tep0;arr[arrLen-1] = tep1;}}//获取轮密钥void getKeys(char key[]){cout<<"密钥:"<<key<<endl;int keyIntArr[64], C[28], D[28];//将16进制字符串转换为二进制整形数组HexToBin(key, keyIntArr);//依次输出64位的二进制数组密钥//for (int i = 0; i < 64; i++) {// cout<<keyIntArr[i];//}//cout<<endl;//密钥置换选择1keyTableTrans1(keyIntArr, C, keyTable11);keyTableTrans1(keyIntArr, D, keyTable12);int tepKeyArr[56];//密钥C和D合并的暂存数组int tepKeyRstArr[48];//置换选择2后的48位数组char keysStr[12];//密钥转换的16进制字符串for (int i = 0; i < 16; i++) {leftMove(i, C);leftMove(i, D);//密钥C和D合并plusArray(C, D, tepKeyArr, 28);keyTableTrans2(tepKeyArr, keys, keyTable2, i);//依次输出置换选择2后的48位的二进制数组密钥//for (int j = 0; j < 48; j++) {// cout<<keys[i][j];//}//cout<<endl;//将二维数组keys的行key[i]赋给一维数组tepKeyRstArr//以便转换为16进制字符串for (int j = 0; j < 48; j++) {tepKeyRstArr[j] = keys[i][j];}BinToHex(tepKeyRstArr, keysStr, 12);cout<<"K"<<i+1<<" : "<<keysStr<<endl;}}//明文初始置换void plaintextTableTrans(int binArr[64], int table[8][8]){int tepBinary[64];int count = 0;for (int i = 0; i < 8; i++) {for (int j = 0; j < 8; j++) {tepBinary[count++] = binArr[table[i][j]-1];}}memcpy(binArr, tepBinary, sizeof(int) * 64);}//扩展运算Evoid expandTableTrans(int binArr[64], int resultArr[48], int table[8][6]){int count = 0;for (int i = 0; i < 8; i++) {for (int j = 0; j < 6; j++) {resultArr[count++] = binArr[table[i][j]-1];}}}//置换运算Pvoid pTableTrans(int binArr[32], int table[8][4]){int tepBinary[32];int count = 0;for (int i = 0; i < 8; i++) {for (int j = 0; j < 4; j++) {tepBinary[count++] = binArr[table[i][j]-1];}}memcpy(binArr, tepBinary, sizeof(int) * 32);}//复制数组void arrCopy(int sourceArray[], int result[], int startIndex) { //已知要复制数组长度为32for (int i = 0; i < 32; i++) {result[i] = sourceArray[startIndex + i];}}//数组每个元素异或运算void arrayXOR(int array1[], int array2[], int len) {for (int i = 0; i <len ; i++) {array1[i] = array1[i] ^ array2[i];}}//将S盒中取出的1-15的十进制数字转换为2进制4位整型数组void DecToBin(int intArr4[], int result) {intArr4[0] = result/8;intArr4[1] = (result%8)/4;intArr4[2] = (result%8%4)/2;intArr4[3] = result%8%4%2;}//选择压缩变换,S盒代替void sBoxTrans(int resultArr[], int arr[]) {int result = 0;int intArr4[4];int j = 0;for (int i = 0; i < 8; i++, j = i*6) {//获取S盒中对应的数据result = sBox[i][arr[j]*2 + arr[j+5]][arr[j+1]*8 + arr[j+2]*4 + arr[j+3]*2 + arr[j+4]];//将S盒中取出的十进制数转化成二进制DecToBin(intArr4, result);for (int n = 0, m = i*4; n < 4; n++) {resultArr[m+n] = intArr4[n];;}}}//F变换void fTrans(int R[], int index) {//F变换中的扩展运算Eint tepR[48];expandTableTrans(R, tepR, expandTable);//将二维数组keys的行key[i]赋给一维数组tepKeyArrint key[48];for (int j = 0; j < 48; j++) {key[j] = keys[index][j];}//扩展运算的得到的tepR与轮密匙key进行异或运算arrayXOR(tepR, key, 48);//选择压缩变换,S盒代替int sBoxArr[32];sBoxTrans(sBoxArr, tepR);//置换运算PpTableTrans(sBoxArr, pTable);memcpy(R, sBoxArr, sizeof(int) * 32);}//迭代处理void loop(int arr[]) {int L[32],R[32];arrCopy(arr, L, 0);arrCopy(arr, R, 32);char LStr[9]=" ",RStr[9]=" ";//先定义好L和RBinToHex(L, LStr, 8);BinToHex(R, RStr, 8);cout<<"L0:"<<LStr<<endl;cout<<"R0:"<<RStr<<endl;int tep[32];//前15轮乘积变换for (int i = 0; i < 15; i++) {memcpy(tep, R, sizeof(int) * 32);fTrans(R, i);//F变换arrayXOR(R, L, 32);//异或运算,得到最后的Rmemcpy(L, tep, sizeof(int) * 32);BinToHex(L, LStr, 8);BinToHex(R, RStr, 8);cout<<"L"<<i+1<<":"<<LStr<<" ";cout<<"R"<<i+1<<":"<<RStr<<endl;}//最后一轮乘积变换memcpy(tep, R, sizeof(int) * 32);fTrans(R, 15);//F变换arrayXOR(L, R, 32);//异或运算,得到最后的Rmemcpy(R, tep, sizeof(int) * 32);BinToHex(L, LStr, 8);BinToHex(R, RStr, 8);cout<<"L16:"<<LStr<<" ";cout<<"R16:"<<RStr<<endl;plusArray(L, R, arr, 32);}//加密void encrypt(char plaintext[]) {int binary[64];//二进制整型明文数组//将16进制表示的字符串转换为二进制字符串HexToBin(plaintext, binary);//初始置换plaintextTableTrans(binary, displaceTable);//16轮乘积变换loop(binary);//逆初始置换plaintextTableTrans(binary, antiDisplaceTable);BinToHex(binary, plaintext, 16);cout<<"密文:"<<plaintext<<endl;}//主函数int main(){char test[17]= "123456ABCD132536";//测试所用明文cout<<"明文:"<<test<<endl; //输出明文getKeys("AABB09182736CCDD");//获取密钥encrypt(test);//加密system("PAUSE");return 0;}。
des算法c++实现--文档
高级编程DES加密算法C++程序实现姓名:***学号:*********专业:电路与系统一、DES算法简要介绍DES算法为密码体制中的对称密码体制,又被成为美国数据加密标准,是1972年美国IBM公司研制的对称密码体制加密算法。
明文按64位进行分组, 密钥长64位,密钥事实上是56位参与DES运算(第8、16、24、32、40、48、56、64位是校验位,使得每个密钥都有奇数个1)分组后的明文组和56位的密钥按位替代或交换的方法形成密文组的加密方法。
DES加密算法特点:分组比较短、密钥太短、密码生命周期短、运算速度较慢。
DES工作的基本原理是,其入口参数有三个:key、data、mode。
key为加密解密使用的密钥,data为加密解密的数据,mode为其工作模式。
当模式为加密模式时,明文按照64位进行分组,形成明文组,key用于对数据加密,当模式为解密模式时,key用于对数据解密。
实际运用中,密钥只用到了64位中的56位,这样才具有高的安全性。
DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位。
DES算法具有极高安全性,到目前为止,除了用穷举搜索法对DES算法进行攻击外,还没有发现更有效的办法。
而56位长的密钥的穷举空间为256,这意味着如果一台计算机的速度是每一秒种检测一百万个密钥,则它搜索完全部密钥就需要将近2285年的时间,可见,这是难以实现的,当然,随着科学技术的发展,DES算法加密的明文已经被高速计算机破解,但是DES目前仍然得到广泛的应用,在某些场合仍然发挥余热。
12二、C++实现简要介绍2.1 密钥生成 2.1.1 取得密钥从用户处取得一个64位(本文如未特指,均指二进制位))长的密码key ,在程序实现中,用一9位字符数组存放用户输入的八个字符,然后将字符数组转换成二进制形式存储在一长度为64的int 型数组当中。
程序流程图如图表1示。
去除64位密码中作为奇偶校验位的第8、16、24、32、40、48、56、64位,剩下的56位作为有效输入密钥。
DES算法的C语言实现
DES算法的C语⾔实现利⽤C语⾔实现DES算法,分组密码原理过程很简单,但是在写的过程中检查了好久才发现错误原因,主要有两点:1.在加密过程16轮迭代过程中,最后⼀轮迭代运算后的结果并没有进⾏交换,即C=IP-1(R16,L16),这样做的⽬的是为了加密解密使⽤同⼀个算法2.在S盒的过程中,移位后应该加括号,否则+的优先级⾼于<<,会出错,下⾯是算法源码:1 #include "des.h"2 #include <stdio.h>3 #include <stdlib.h>45const unsigned char IP_table[64] = {658, 50, 42, 34, 26, 18, 10, 2,760, 52, 44, 36, 28, 20, 12, 4,862, 54, 46, 38, 30, 22, 14, 6,964, 56, 48, 40, 32, 24, 16, 8,1057, 49, 41, 33, 25, 17, 9, 1,1159, 51, 43, 35, 27, 19, 11, 3,1261, 53, 45, 37, 29, 21, 13, 5,1363, 55, 47, 39, 31, 23, 15, 714 };1516const unsigned char IPR_table[64] = {1740, 8, 48, 16, 56, 24, 64, 32,1839, 7, 47, 15, 55, 23, 63, 31,1938, 6, 46, 14, 54, 22, 62, 30,2037, 5, 45, 13, 53, 21, 61, 29,2136, 4, 44, 12, 52, 20, 60, 28,2235, 3, 43, 11, 51, 19, 59, 27,2334, 2, 42, 10, 50, 18, 58, 26,2433, 1, 41, 9, 49, 17, 57, 2525 };2627const unsigned char E_table[48] = {2832, 1, 2, 3, 4, 5,294, 5, 6, 7, 8, 9,308, 9, 10, 11, 12, 13,3112, 13, 14, 15, 16, 17,3216, 17, 18, 19, 20, 21,3320, 21, 22, 23, 24, 25,3424, 25, 26, 27, 28, 29,3528, 29, 30, 31, 32, 136 };3738const unsigned char P_table[32] = {3916, 7, 20, 21, 29, 12, 28, 17,401, 15, 23, 26, 5, 18, 31, 10,412, 8, 24, 14, 32, 27, 3, 9,4219, 13, 30, 6, 22, 11, 4, 2543 };4445const unsigned char PC1_table[56] = {4657, 49, 41, 33, 25, 17, 9,471, 58, 50, 42, 34, 26, 18,4810, 2, 59, 51, 43, 35, 27,4919, 11, 3, 60, 52, 44, 36,5063, 55, 47, 39, 31, 23, 15,517, 62, 54, 46, 38, 30, 22,5214, 6, 61, 53, 45, 37, 29,5321, 13, 5, 28, 20, 12, 454 };5556const unsigned char LOOP_table[16] = {571, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 158 };5960const unsigned char PC2_table[48] = {6114, 17, 11, 24, 1, 5,623, 28, 15, 6, 21, 10,6323, 19, 12, 4, 26, 8,6416, 7, 27, 20, 13, 2,6541, 52, 31, 37, 47, 55,6630, 40, 51, 45, 33, 48,6744, 49, 39, 56, 34, 53,6846, 42, 50, 36, 29, 3269 };7071const unsigned char sbox[8][4][16] = {72// S17314, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,740, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,754, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,7615, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,77//S27815, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,793, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,800, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,8113, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,82//S38310, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,8413, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,8513, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,861, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,87//S4887, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,8913, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,9010, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,913, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,92//S5932, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,9414, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,954, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,9611, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,97//S69812, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,9910, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,1009, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,1014, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,102//S71034, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,10413, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,1051, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,1066, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,107//S810813, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,1091, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,1107, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,1112, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11112 };113114void ByteToBit(unsigned char* out, const unsigned char* in, const int bits){115for(int i=0;i<bits;i++)116out[i]=(in[i/8]>>(7-i%8))&1;117 }118119void BitToByte(unsigned char* out, const unsigned char* in, const int bits){120 memset(out, 0, (bits + 7) / 8);121for(int i=0;i<bits;i++)122out[i/8]|=in[i]<<(7-i%8);123 }124125void Transform(unsigned char* out,const unsigned char* in, const unsigned char* table, const int len){ 126 unsigned char tmp[64] = {0};127for (int i = 0; i < len; i++)128 tmp[i] = in[table[i] - 1];129 memcpy(out, tmp, len);130 }131132void RotateL(unsigned char* in, const int len, int loop){133static unsigned char tmp[64];134 memcpy(tmp, in, len);135 memcpy(in, in + loop, len - loop);136 memcpy(in + len - loop, tmp, loop);137 }138139static unsigned char subKey[16][48] = { 0 };140void setKey(const unsigned char* in){141char key[64] = { 0 };142 ByteToBit(key, in, 64);143char temp[56]={0};144 Transform(temp, key, PC1_table, 56);145for(int i=0;i<16;i++){146 RotateL(temp, 28, LOOP_table[i]);147 RotateL(temp + 28, 28, LOOP_table[i]);148 Transform(subKey[i], temp, PC2_table, 48);149 }150 }151152void xor(unsigned char* in1,const unsigned char* in2,int len){153for(int i=0;i<len;i++)154 in1[i]^=in2[i];155 }156157void sbox_exchange(unsigned char* out,const unsigned char* in){158char row, column;159for (int i = 0; i < 8; i++){160char num = 0;161 row = (in[6 * i]<<1)+ in[6 * i + 5];162 column = (in[6 * i + 1] << 3) + (in[6 * i + 2] << 2) + (in[6 * i + 3] << 1) + in[6 * i + 4]; 163 num = sbox[i][row][column];164for (int j = 0; j < 4; j++)165 {166out[4 * i + j] = (num >> (3 - j)) & 1;167 }168 }169 }170171void F_func(unsigned char* out,const unsigned char* in,unsigned char* subKey){172 unsigned char temp[48]={0};173 unsigned char res[32]={0};174 Transform(temp, in, E_table, 48);175 xor(temp,subKey,48);176 sbox_exchange(res,temp);177 Transform(out, res, P_table, 32);178 }179180void encryptDES(unsigned char* out,const unsigned char* in, const unsigned char* key){ 181 unsigned char input[64] = { 0 };182 unsigned char output[64] = { 0 };183 unsigned char tmp[64] = { 0 };184 ByteToBit(input, in, 64);185 Transform(tmp, input, IP_table, 64);186char* Li = &tmp[0], *Ri = &tmp[32];187 setKey(key);188for(int i=0;i<16;i++){189char temp[32]={0};190 memcpy(temp,Ri,32);191 F_func(Ri, Ri,subKey[i]);192 xor(Ri, Li, 32);193 memcpy(Li,temp,32);194 }195 RotateL(tmp, 64, 32);//the input is LR,output is RL196 Transform(output, tmp, IPR_table, 64);197 BitToByte(out, output,64);198 }199200void decryptDES(unsigned char* out,const unsigned char* in, const unsigned char* key){ 201 unsigned char input[64] = { 0 };202 unsigned char output[64] = { 0 };203 unsigned char tmp[64] = { 0 };204 ByteToBit(input, in, 64);205 Transform(tmp, input, IP_table, 64);206char* Li = &tmp[0], *Ri = &tmp[32];207 setKey(key);208 RotateL(tmp, 64, 32);209for (int i = 0; i < 16; i++){210char temp[32] = { 0 };211 memcpy(temp, Li, 32);212 F_func(Li, Li,subKey[15 - i]);213 xor(Li, Ri, 32);214 memcpy(Ri, temp, 32);215 }216 Transform(output, tmp, IPR_table, 64);217 BitToByte(out, output, 64);218 }。
用c语言实现des算法
用c语言实现des算法由于DES算法被广泛应用于加密和数据安全方面,因此在计算机科学领域,了解和实现DES算法是非常重要的。
C语言是一种强大的编程语言,其通用性和高效性使其成为实现DES算法的理想选择。
以下是使用C语言实现DES算法的步骤和方法:1.将明文和密钥转换为二进制格式在DES算法中,明文和密钥必须先转换为二进制格式才能进行加密或解密操作。
可以使用一个字符串数组来存储明文和密钥,然后使用C 语言的位运算符将每个字符转换为二进制格式。
2.对明文进行初始置换DES算法的第一步是对明文进行初始置换。
可以使用一个int型数据来存储初始置换后的结果。
3.密钥生成使用密钥生成算法来生成16个48位的子密钥。
可以使用一个二维数组来存储每个子密钥。
4.将初始置换后的明文分为左右两个部分将初始置换后的明文分为左右两个部分,每个部分32位。
5.进行16轮迭代在每轮迭代中,右半部分32位的明文和48位的子密钥进行异或运算,然后使用S盒置换和P盒置换来处理数据。
最后将结果与左半部分32位的明文异或,以更新下一轮迭代所需的数据。
6.合并左右两个部分在进行最后一轮迭代后,将左右两个部分合并成一段64位的二进制数据。
7.进行最后的逆置换使用逆置换来处理上一步生成的64位二进制数据,以生成最终的密文。
实现DES算法需要一定的数学知识和编程技能,因此建议有一定编程基础的人才尝试实现此算法。
列表:1.使用C语言实现DES算法步骤2.将明文和密钥转换为二进制格式的方法3.对明文进行初始置换的具体过程4.密钥生成算法的原理和实现方法5.如何将初始置换后的明文分为左右两个部分6.DES算法16轮迭代的详细过程7.如何合并左右两个部分的数据8.DES算法中的最后一步逆置换的作用和过程9.DES算法的应用场景和重要性10.如何使用C语言实现DES算法的具体步骤和技巧。
DES对称加密算法详解和c++代码实现(带样例和详细的中间数据)
DES对称加密算法详解和c++代码实现(带样例和详细的中间数据)特点:1.DES是对称性加密算法,即加密和解密是对称的,⽤的是同⼀个密钥2.DES只处理⼆进制数据,所以需要将明⽂转换成为2进制数据3.DES每次处理64位的数据,所以应该将明⽂切割成64位的分组,当最后⼀组数据不⾜64位的时候,⾼位补04.DES使⽤64位的密钥,但因为密钥中的每8位会被忽略,所以有效的密钥长度是56位,从⽽产⽣16个48位的⼦密钥(变换过程后⾯会说明)5.每64位数据⼀个块,是DES的永恒组织⽅式具体样例分析:(仅以⼀组64位数据为例分析加密过程)明⽂M是:8787878787878787密钥K是:0E329232EA6D0D73上⾯的信息都是16进制的,转换为2进制明⽂M是:0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111密钥K是:00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001第⼀步:根据密钥⽣成16个⼦密钥1.根据密钥初始置换表将64位的密钥转化为58位的密钥57 49 41 33 25 17 91 58 50 42 34 26 1810 2 59 51 43 35 2719 11 3 60 52 44 3663 55 47 39 31 23 157 62 54 46 38 30 2214 6 61 53 45 37 2921 13 5 28 20 12 4由于上表中第⼀个元素为57,这将使原秘钥的第57位变换为新秘钥K+的第1位。
同理,原秘钥的第49位变换为新秘钥的第2位……原秘钥的第4位变换为新秘钥的最后⼀位。
注意原秘钥中只有56位会进⼊新秘钥,上表也只有56个元素。
原密钥K:00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001新密钥K:1111000 0110011 0010101 0101111 0101010 1011001 1001111 00011112.将新密钥拆分成C0和D0,每组都有28位⽐如新密钥C0:1111000 0110011 0010101 0101111D0:0101010 1011001 1001111 00011113.根据密钥轮次左移表,左移特定的位数1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 161 12 2 2 2 2 2 1 2 2 2 2 2 2 1⽐如第⼀轮是左移1位,第⼆轮也是左移1位,第三轮是左移两位所以C1:1110000110011001010101011111D1:1010101011001100111100011110下⾯给出C1,D1到C16,D16的数据:C1 = 1110000110011001010101011111D1 = 1010101011001100111100011110C2 = 1100001100110010101010111111D2 = 0101010110011001111000111101C3 = 0000110011001010101011111111D3 = 0101011001100111100011110101C4 = 0011001100101010101111111100D4 = 0101100110011110001111010101C5 = 1100110010101010111111110000D5 = 0110011001111000111101010101C6 = 0011001010101011111111000011D6 = 1001100111100011110101010101C7 = 1100101010101111111100001100D7 = 0110011110001111010101010110C8 = 0010101010111111110000110011D8 = 1001111000111101010101011001C9 = 0101010101111111100001100110D9 = 0011110001111010101010110011C10 = 0101010111111110000110011001D10 = 1111000111101010101011001100C11 = 0101011111111000011001100101D11 = 1100011110101010101100110011C12 = 0101111111100001100110010101D12 = 0001111010101010110011001111C13 = 0111111110000110011001010101D13 = 0111101010101011001100111100C14 = 1111111000011001100101010101D14 = 1110101010101100110011110001C15 = 1111100001100110010101010111D15 = 1010101010110011001111000111C16 = 1111000011001100101010101111D16 = 0101010101100110011110001111需要记住的是:每⼀对Cn 和 Dn都是由前⼀对Cn-1 和 Dn-1移位⽽来!4.得到Cn,Dn后合并CnDn,然后根据密钥压缩置换表将56位密钥压缩成48位的⼦密钥密钥压缩置换表:14 17 11 24 1 53 28 15 6 21 1023 19 12 4 26 816 7 27 20 13 241 52 31 37 47 5530 40 51 45 33 4844 49 39 56 34 5346 42 50 36 29 32每对⼦秘钥有56位,但PC-2仅仅使⽤其中的48位。
DES加解密算法C语言源代码
DES加解密算法C语言源代码以下是一个实现DES加解密算法的C语言源代码,包含了加密和解密函数。
请注意,这个代码只是为了演示DES算法的工作原理,并不是一个完整的、安全的加密算法实现。
```c#include <stdio.h>#include <stdint.h>typedef structuint8_t key[8];uint8_t subkeys[16][6];} DESKey;void generateSubkeys(uint8_t* key, uint8_t subkeys[16][6]) //略过子密钥生成算法的具体实现//这里只是假设生成的子密钥都是随机的,实际生成过程要更复杂for (int i = 0; i < 16; i++)for (int j = 0; j < 6; j++)subkeys[i][j] = (i+j) % 256;}}void DES(uint8_t* input, uint8_t key[8], uint8_t* output, int encrypt)//略过DES加密算法的具体实现DESKey desKey;for (int i = 0; i < 8; i++)desKey.key[i] = key[i];}generateSubkeys(key, desKey.subkeys);//这里只是假设输入输出是8字节长,实际上可以支持任意长度//执行加解密操作if (encrypt)printf("Encrypting: ");} elseprintf("Decrypting: ");}for (int i = 0; i < 8; i++)output[i] = encrypt ? input[i] ^ desKey.subkeys[0][i%6] : input[i] ^ desKey.subkeys[15][i%6];printf("%02X ", output[i]);}printf("\n");int maiuint8_t input[8] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0};uint8_t key[8] = {0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6, 0x07,0x08};uint8_t output[8];DES(input, key, output, 1);DES(output, key, output, 0);return 0;```在这个代码中,`generateSubkeys` 函数用于生成 16 个子密钥,之后分别在加密和解密函数 `DES` 中使用。
C语言加密与解密算法
C语言加密与解密算法在计算机科学与信息安全领域,加密与解密算法起着至关重要的作用。
加密算法用于将原始数据转换为不可读的密文,而解密算法则用于将密文还原为可读的原始数据。
C语言是一种常用的编程语言,具备高效性和灵活性,适用于加密与解密算法的开发。
本文将介绍几种常用的C语言加密与解密算法。
一、凯撒密码算法凯撒密码算法是一种最简单的替换加密算法,通过将字母按照固定的偏移量进行替换来实现加密与解密。
以下是一个简单的C语言凯撒密码实现例子:```c#include <stdio.h>void caesarEncrypt(char* message, int key) {int i = 0;while (message[i] != '\0') {if (message[i] >= 'a' && message[i] <= 'z') {message[i] = (message[i] - 'a' + key) % 26 + 'a';} else if (message[i] >= 'A' && message[i] <= 'Z') {message[i] = (message[i] - 'A' + key) % 26 + 'A';}i++;}}void caesarDecrypt(char* message, int key) {int i = 0;while (message[i] != '\0') {if (message[i] >= 'a' && message[i] <= 'z') {message[i] = (message[i] - 'a' - key + 26) % 26 + 'a'; } else if (message[i] >= 'A' && message[i] <= 'Z') {message[i] = (message[i] - 'A' - key + 26) % 26 + 'A'; }i++;}}int main() {char message[] = "Hello, World!";int key = 3;printf("Original message: %s\n", message);caesarEncrypt(message, key);printf("Encrypted message: %s\n", message);caesarDecrypt(message, key);printf("Decrypted message: %s\n", message);return 0;}```以上程序演示了凯撒密码的加密与解密过程,通过指定偏移量实现对消息的加密与解密。
C++实现DES加密算法
程序设计文档(一)设计原理:本次作业是完成DES加密算法的。
DES加密算法是用64位明文和64位密钥进行相关的运算得到。
具体的实现步骤为:1)将输入的明文字符转为64位的二进制代码2)将输入的密钥字符转为64位的二进制代码3)将64位明文二进制进行位变换4)将变换后的明文二进制分为左右两个32数组5)将32位右数组扩展为48位数组6)将64位密钥进行除奇偶转换变成56位数组7)将56位数组等分为ab两个数组8)将a,b两个数组分别进行移位9)将移位后的ab两个数组进行合并成56位数组10)将合并后的56位数组变换成48位数组11)在第10步生成的48位数组和第5步生成的48位进行异或得到新的48位数组12)新的48位数组进行s盒变换后得到32位数组13)将32位数组进行变换得到新的32位数组14)新的32位数组和明文的左数组进行异或得到新的右数组,原来的右数组变成新的左数组15)回到第5步一共迭代16次16)最后将右左数组合并成64位数组17)64位数组进行变换得到新的64位数组18)将64位数组变换成8个字符这次作业要求用类的思想来解决,不过我没有很好的把加密算法抽象成类的概念,只是把整个大的加密算法当成一个类,每次加密都是一个类,所有的子函数都作为算法类的公有函数。
或许可以把明文抽象成一个类,密文又抽象成一个类,把他们共有的函数作为这个类的公有函数;又或许把几个函数作为一个类的公有函数,但是这样只是采用了类的形式而已并非很好的运用类的思想。
本次作业采取的是第一个把整个大的加密算法当为一个类的方法。
(二)设计工作流程(三)程序代码#include<iostream>#include<string>using namespace std;class wangxianDES{public:void sr(char a[8]);//将输入的字符转入shur字符数组里去void mwerjinzhi();//明文进行二进制转换,并放入mingwen[64]数组里void merjinzhi();//密钥进行二进制转换,并放入miyue[64]数组里 void bmingwen();//64位初始明文进行移位变换,并放入mingwenbianhuan[64]数组里void fenmingwen();//变换后的明文分为r和l两个32位数组,放入mingwenr[32]和mingwenl[32]void kuozhan();//r数组由32位扩展到48位,放到mingwenrk[48] void cjo();//64位密钥除去奇偶位变成56位,放到miyuecjo[56] void df();//将56位密钥等分成a和b两个28位的数组,放到miyuea[28]和miyueb[28]void ayw(int n);//a数组进行移位void byw(int n);//b数组进行移位void hb();//将移位后的a和b数组合并,放到miyuehb[56]void bh();//合并后将56位数组变换成48位数组,放到miyuehbh[48] void tyh();//将扩展后的r数组和合并后的48位数组进行异或运算,放到yh[48]void s();//对48位数据进行s盒变换,得到32位数据放到sbh[32] void sh();//sbh[32]的数据再变换放到szbh[32]里void zyh();//将szbh[32]和mingwenl[32]异或放到mingwenr[32]里void zhb();//最后将mingwenr[32]和mingwenl[32]合并到zh[64]里void zzz();//zh[64]再变换放到aaa[64]里int aaa[64];private:char shur[8];//定义了所需要的数组,具体用途上边函数注释里提到int bhs[8], mingwen[64],mingwenbianhuan[64],mingwenl[32],mingwenr[32],mingwenrk[48],miyue[64],miyuecjo[56],miyuea[28],miyueb[28],miyueyw[16],mi yuehb[56],miyuehbh[48],yh[48],sbh[32],szbh[32],zhong[32],zh[64];};int mwbhjz[64]={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};//明文变换所用到的变换矩阵intmwrkzjz[48]={32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14, 15,16,17,16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1};//mingwenr[32]扩展所用到的变换矩阵int mycjojz[56]= {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 myyw[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};//密钥移位用到的矩阵int myhbhzj[48]= {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};//密钥合并用到的矩阵int sbhjz[32]= {16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25};//密钥合并后变换用到的矩阵int zhjz[64]={40,8,48,16,56,24,64,32,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}; //最后64位变换的矩阵int B[4][16]= {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};//s盒变换的矩阵int C[4][16]= {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};//s盒变换的矩阵int D[4][16]= {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};//s盒变换的矩阵int E[4][16]= {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};//s盒变换的矩阵int F[4][16]= {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};//s盒变换的矩阵int G[4][16]= {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};//s盒变换的矩阵int H[4][16]= {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,//s盒变换的矩阵6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12};int I[4][16]= {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};//s盒变换的矩阵void wangxianDES::sr(char a[8]){ int i;for (i=0;i<8;i++)shur[i]=a[i];cout<<"将输入的字符转入shur字符数组里去"<<endl;}void wangxianDES::mwerjinzhi(){ int a=8,i,j;for(j=0;j<8;j++){ for(i=8;i>0;i--){mingwen[i+a-1]=shur[j]%2;shur[j]=shur[j]/2;}a=a+8;}cout<<"明文进行二进制转换,并放入mingwen[64]数组里"<<endl;} void wangxianDES::merjinzhi(){ int a=8;int i,j;for(j=0;j<8;j++){ for(i=8;i>0;i--){miyue[i+a-1]=shur[j]%2;shur[j]=shur[j]/2;}a=a+8;}cout<<"密钥进行二进制转换,并放入miyue[64]数组里"<<endl; } void wangxianDES::bmingwen(){ int i;for(i=0;i<64;i++)mingwenbianhuan[i]=mingwen[mwbhjz[i]-1];cout<<"64位初始明文进行移位变换,并放入mingwenbianhuan[64]数组里"<<endl;}void wangxianDES::fenmingwen(){ int i;for(i=0;i<32;i++)mingwenl[i]=mingwenbianhuan[i];for(i=0;i<32;i++)mingwenr[i]=mingwenbianhuan[i+32];cout<<"变换后的明文分为r和l两个32位数组,放入mingwenr[32]和mingwenl[32]"<<endl;}void wangxianDES::kuozhan(){ int i;for(i=0;i<48;i++)mingwenrk[i]=mingwenr[mwrkzjz[i]-1];cout<<"r数组由32位扩展到48位,放到mingwenrk[48]"<<endl;} void wangxianDES::cjo(){ int i;for(i=0;i<56;i++)miyuecjo[i]=miyue[mycjojz[i]-1];cout<<"64位密钥除去奇偶位变成56位,放到miyuecjo[56]"<<endl;}void wangxianDES::df(){ int i;for(i=0;i<28;i++)miyuea[i]=miyuecjo[i];for(i=0;i<28;i++)miyueb[i]=miyuecjo[i+28];cout<<"将56位密钥等分成a和b两个28位的数组,放到miyuea[28]和miyueb[28]"<<endl;}void wangxianDES::ayw(int n){ int a,i;a=myyw[n];if(a==1){int b;b=miyuea[0];for(i=0;i<27;i++)miyuea[i]=miyuea[i+1];miyuea[28]=b;}else{int b,c;b=miyuea[0];c=miyuea[1];for(i=0;i<26;i++)miyuea[i]=miyuea[i+2];miyuea[26]=b;miyuea[27]=c;}cout<<"a数组进行移位"<<endl;}void wangxianDES::byw(int n){ int a,i;a=myyw[n];if(a==1){int b;b=miyueb[0];for(i=0;i<27;i++)miyueb[i]=miyueb[i+1];miyueb[28]=b;}else{int b,c;b=miyueb[0];c=miyueb[1];for(i=0;i<26;i++)miyueb[i]=miyueb[i+2];miyueb[26]=b;miyueb[27]=c;}cout<<"b数组进行移位"<<endl;}void wangxianDES::hb(){ int i;for(i=0;i<28;i++)miyuehb[i]=miyuea[i];for(i=0;i<28;i++)miyuehb[i+28]=miyueb[i];cout<<"将移位后的a和b数组合并,放到miyuehb[56]"<<endl;} void wangxianDES::bh(){ int i;for(i=0;i<48;i++)miyuehbh[i]=miyuehb[myhbhzj[i]-1];cout<<"合并后将56位数组变换成48位数组,放到miyuehbh[48]"<<endl;}void wangxianDES::tyh(){ int i;for(i=0;i<48;i++){if(miyuehbh[i]==mingwenrk[i])yh[i]=0;else yh[i]=1;}cout<<"将扩展后的r数组和合并后的48位数组进行异或运算,放到yh[48]"<<endl;}void wangxianDES::s(){ int a,b;a=yh[0]*2+yh[5];b=yh[1]*8+yh[2]*4+yh[3]*2+yh[4];bhs[0]=B[a][b];a=yh[6]*2+yh[11];b=yh[7]*8+yh[8]*4+yh[9]*2+yh[10];bhs[1]=C[a][b];a=yh[12]*2+yh[17];b=yh[13]*8+yh[14]*4+yh[15]*2+yh[16];bhs[2]=D[a][b];a=yh[18]*2+yh[23];b=yh[19]*8+yh[20]*4+yh[21]*2+yh[22];bhs[3]=E[a][b];a=yh[24]*2+yh[29];b=yh[25]*8+yh[26]*4+yh[27]*2+yh[28];bhs[4]=F[a][b];a=yh[30]*2+yh[35];b=yh[31]*8+yh[32]*4+yh[33]*2+yh[34];bhs[5]=G[a][b];a=yh[36]*2+yh[41];b=yh[37]*8+yh[38]*4+yh[39]*2+yh[40];bhs[6]=H[a][b];a=yh[42]*2+yh[47];b=yh[43]*8+yh[44]*4+yh[45]*2+yh[46];bhs[7]=I[a][b];int k=8,i,j;for(j=0;j<8;j++){ for(i=4;i>0;i--){sbh[i+k-1]=bhs[j]%2;bhs[j]=bhs[j]/2;}k=k+8;}cout<<"对48位数据进行s盒变换,得到32位数据放到sbh[32]"<<endl;}void wangxianDES::sh(){ int i;for(i=1;i<32;i++)szbh[i]=sbh[sbhjz[i]-1];cout<<"sbh[32]的数据再变换放到szbh[32]里"<<endl;}void wangxianDES::zyh(){ int i;for(i=1;i<32;i++){ zhong[i]=mingwenr[i];if(szbh[i]==mingwenl[i])mingwenr[i]=0;else mingwenr[i]=1;mingwenl[i]=zhong[i];}cout<<"将szbh[32]和mingwenl[32]异或放到mingwenr[32]里,原来mingwenr里的转到mingwenl里"<<endl;}void wangxianDES::zhb(){ int i;for(i=0;i<32;i++)zh[i]=mingwenr[i];for(i=0;i<32;i++)zh[i+32]=mingwenl[i];cout<<"最后将mingwenr[32]和mingwenl[32]合并到zh[64]里"<<endl;}void wangxianDES::zzz(){ int i;for(i=0;i<64;i++)aaa[i]=zh[zhjz[i]-1];cout<<"zh[64]再变换放到aaa[64]里"<<endl;}void main(){ wangxianDES A;char shuru[8],shuchu[8];int p;int w, y;cout<<"输入明文(小于9个字符)"<<endl;cin>>shuru;A.sr(shuru);A.mwerjinzhi();cout<<"输入密钥(8个字符)"<<endl;cin>>shuru;A.sr(shuru);A.merjinzhi();A.bmingwen();A.fenmingwen();A.cjo();A.df();for(p=0;p<16;p++){ A.ayw(p);A.byw(p);A.hb();A.bh();A.kuozhan();A.tyh();A.s();A.sh();A.zyh();}A.zhb();A.zzz();cout<<"加密后的密文二进制"<<endl;for(y=0;y<64;y++){cout<<A.aaa[y]<<" ";p=y+1;if(p%8==0)cout<<endl;}cout<<endl;w=0;cout<<"加密后的8位密文"<<endl;for(p=0;p<8;p++){(char)shuchu[p]=A.aaa[7+w]+A.aaa[6+w]*2+A.aaa[5+w]*4+A.aaa[4+w ]*8+A.aaa[3+w]*16+A.aaa[2+w]*32+A.aaa[1+w]*64+A.aaa[0+w]*128;cout<<shuchu[p];w=w+8;}}(四)运行结果(五)设计总结本次作业是按照自己的思路和想法来完成的,最后得到加密后的密文。
des加密算法c语言源代码
#include<iostream.h>int IP[64] = {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};int IP_1[64] = {40,8,48,16,56,24,64,32,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};int E[48] = {32,1, 2, 3, 4, 5,4, 5, 6, 7, 8, 9,8, 9, 10,11,12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1};int P[32]={16 ,7 , 20 , 21 ,29,12 ,28 , 17 ,1, 15 ,23 , 26 ,5, 18 ,31 , 10 ,2, 8 , 24 , 14 ,32,27, 3 , 9 ,19,13, 30 , 6 ,22,11 ,4 , 25 };void Char_to_Int(unsigned char in[],int out[],int len){for(int i=0;i<len;i++)out[i]=int(in[i]);}void Int_to_Char(int in[],unsigned char out[],int len){for(int i=0;i<len;i++)out[i]=char(in[i]);}void B_to_H(int in[],unsigned char out[]){for(int i=0;i<16;i++){int temp=in[i*4]*8+in[i*4+1]*4+in[i*4+2]*2+in[i*4+3];switch(temp){case 10 :out[i]='A';break;case 11 :out[i]='B';break;case 12 :out[i]='C';break;case 13 :out[i]='D';break;case 14 :out[i]='E';break;case 15 :out[i]='F';break;default :out[i]=unsigned char(temp+48);break;}}}void H_to_B(unsigned char in[],int out[]){for(int i=0;i<16;i++)switch(in[i]){case 'A':out[i*4]=1;out[i*4+1]=0;out[i*4+2]=1;out[i*4+3]=0;break;case 'B':out[i*4]=1;out[i*4+1]=0;out[i*4+2]=1;out[i*4+3]=1;break;case 'C':out[i*4]=1;out[i*4+1]=1;out[i*4+2]=0;out[i*4+3]=0;break;case 'D':out[i*4]=1;out[i*4+1]=1;out[i*4+2]=0;out[i*4+3]=1;break;case 'E':out[i*4]=1;out[i*4+1]=1;out[i*4+2]=1;out[i*4+3]=0;break;case 'F':out[i*4]=1;out[i*4+1]=1;out[i*4+2]=1;out[i*4+3]=1;break;case '0':out[i*4]=0;out[i*4+1]=0;out[i*4+2]=0;out[i*4+3]=0;break;case '1':out[i*4]=0;out[i*4+1]=0;out[i*4+2]=0;out[i*4+3]=1;break;case '2':out[i*4]=0;out[i*4+1]=0;out[i*4+2]=1;out[i*4+3]=0;break;case '3':out[i*4]=0;out[i*4+1]=0;out[i*4+2]=1;out[i*4+3]=1;break;case '4':out[i*4]=0;out[i*4+1]=1;out[i*4+2]=0;out[i*4+3]=0;break;case '5':out[i*4]=0;out[i*4+1]=1;out[i*4+2]=0;out[i*4+3]=1;break;case '6':out[i*4]=0;out[i*4+1]=1;out[i*4+2]=1;out[i*4+3]=0;break;case '7':out[i*4]=0;out[i*4+1]=1;out[i*4+2]=1;out[i*4+3]=1;break;case '8':out[i*4]=1;out[i*4+1]=0;out[i*4+2]=0;out[i*4+3]=0;break;case '9':out[i*4]=1;out[i*4+1]=0;out[i*4+2]=0;out[i*4+3]=1;break;}}void O_to_B(int in[],int out[],int len){int i,j;for(i=0;i<len;i++)for(j=i*len+7;j>=i*len;j--){out[j]=in[i]%2; in[i]=in[i]/2;}}void B_to_O(int in[],int out[],int len){int j;for(int i=0;i<len;i++){j=8*i;out[i]=in[j]*128+in[j+1]*64+in[j+2]*32+in[j+3]*16+in[j+4]*8+in[j+5]*4+in[j+6]*2+in[j+7]*1;}}void Convert(int in[],int out[],int table[],int len){for(int i=0;i<len;i++)out[i]=in[table[i]-1];}void Divide(int in[],int out1[],int out2[],int len){for(int i=0;i<len;i++)if(i<len/2)out1[i]=in[i];elseout2[i-len/2]=in[i];}//正确void Combine(int in1[],int in2[],int out[],int len){for(int i=0;i<len;i++)if(i<len/2)out[i]=in1[i];elseout[i]=in2[i-len/2];}//正确void RLC1(int in[],int out[],int len){int temp=in[0];for(int i=0;i<len-1;i++)out[i]=in[i+1];out[len-1]=temp;}//正确void RLC2(int in[],int out[],int len){int temp1=in[0],temp2=in[1];for(int i=0;i<len-2;i++)out[i]=in[i+2];out[len-2]=temp1;out[len-1]=temp2;}//正确void COPY(int in[],int out[],int len){for(int i=0;i<len;i++)out[i]=in[i];}//ZHENGQUEvoid XOR(int in1[],int in2[],int out[],int len){for(int i=0;i<len;i++)out[i]=(in1[i]+in2[i])%2;}void GetKey(unsigned char in[],int out[16][48]){int i;int Msg_Int[8],Msg_Bin[64],Msg_Bin_PC1[56],Key_Bin[56],C[17][28],D[17][28];char Msg_H[16];int PC1[56]={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},PC2[48]={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};Char_to_Int(in,Msg_Int,8);//正确O_to_B(Msg_Int,Msg_Bin,8);//正确cout<<endl;Convert(Msg_Bin,Msg_Bin_PC1,PC1,56); Divide(Msg_Bin_PC1,C[0],D[0],56);for(i=0;i<16;i++){if(i==0){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else if(i==1){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else if(i==8){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else if(i==15){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else{RLC2(C[i],C[i+1],28);RLC2(D[i],D[i+1],28);}Combine(C[i+1],D[i+1],Key_Bin,56);Convert(Key_Bin,out[i],PC2,48);}} //生成子密钥正确void S_box(int in[],int out[]){int i;int SBox[8][64] ={{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},{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},{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},{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},{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,},{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},{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},{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}};int s1,s3,s2[8];for(i=0;i<8;i++){s1=in[i*6]*2+in[i*6+5]*1;s3=in[i*6+1]*8+in[i*6+2]*4+in[i*6+3]*2+in[i*6+4]*1;s2[i]=SBox[i][s1*16+s3];}int j;for(i=0;i<8;i++)for(j=i*4+3;j>=i*4;j--){out[j]=s2[i]%2; s2[i]=s2[i]/2;}}void Encode(unsigned char in[],unsigned char Final_H[],int K[16][48]){int i;intORINT[8],ORBYTE[64],FinalBYTE[64],LR[64],R48[48],S_in[48],S_out[32],F_RL[32],FinalINT[8],L[17] [32],R[17][32];char ORH[16];Char_to_Int(in,ORINT,8);cout<<""<<endl;O_to_B(ORINT,ORBYTE,8);Convert(ORBYTE,LR,IP,64);Divide(LR,L[0],R[0],64);for(i=1;i<17;i++){Convert(R[i-1],R48,E,48);XOR(R48,K[i-1],S_in,48);S_box(S_in,S_out);Convert(S_out,F_RL,P,32);XOR(F_RL,L[i-1],R[i],32);COPY(R[i-1],L[i],32);}Combine(R[16],L[16],LR,64);Convert(LR,FinalBYTE,IP_1,64);cout<<"二进制的密文:"<<endl;for(i=0;i<64;i++){cout<<FinalBYTE[i];}cout<<endl<<endl;B_to_H(FinalBYTE,Final_H);}void DeCode(unsigned char in[],unsigned char Final_H[],int K[16][48]){int i;intORINT[8],ORBYTE[64],LR[64],R48[48],S_in[48],S_out[32],F_RL[32],FinalBYTE[64],FinalINT[8],L[17] [32],R[17][32];char ORH[16];Char_to_Int(in,ORINT,8);cout<<""<<endl;O_to_B(ORINT,ORBYTE,8);Convert(ORBYTE,LR,IP,64);Divide(LR,L[0],R[0],64);for(i=1;i<17;i++){COPY(R[i-1],L[i],32);Convert(R[i-1],R48,E,48);XOR(R48,K[16-i],S_in,48);S_box(S_in,S_out);Convert(S_out,F_RL,P,32);XOR(L[i-1],F_RL,R[i],32);}Combine(R[16],L[16],LR,64);Convert(LR,FinalBYTE,IP_1,64);B_to_H(FinalBYTE,Final_H);}void main(){int i;unsigned char Msg[8],UnCodeMsg[16],CodeMsg[16],Key[8],Msg_H[16],CodeChar[8];int SKey[16][48];int OrByte[64],OrInt[8],FinalByte[64],FinalInt[8];cout<<"请输入明文:"<<endl;for(i=0;i<8;i++)cin>>Msg[i];cout<<"请输入初始密钥:"<<endl;for(i=0;i<8;i++)cin>>Key[i];GetKey(Key,SKey);Encode(Msg,CodeMsg,SKey);cout<<"十六进制的密文:"<<endl;for(i=0;i<16;i++)cout<<CodeMsg[i];cout<<endl;cout<<"字符型密文:"<<endl;H_to_B(CodeMsg,FinalByte);B_to_O(FinalByte,FinalInt,8);Int_to_Char(FinalInt,CodeChar,8);for(i=0;i<8;i++)cout<<CodeChar[i];cout<<endl;cout<<"请输入十六进制的密文:"<<endl;for(i=0;i<16;i++)cin>>CodeMsg[i];B_to_O(FinalByte,FinalInt,8);Int_to_Char(FinalInt,CodeChar,8);cout<<"请输入解密密钥:"<<endl;for(i=0;i<8;i++)cin>>Key[i];GetKey(Key,SKey);DeCode(CodeChar,Msg_H,SKey);//for(i=0;i<16;i++)// cout<<Msg_H[i];cout<<endl;H_to_B(Msg_H,OrByte);B_to_O(OrByte,OrInt,8);Int_to_Char(OrInt,Msg,8);cout<<"明文是:"<<endl;for(i=0;i<8;i++)cout<<Msg[i];cout<<endl;}。
DES加密的算法
信息安全概论·课程设计DES加密的C语言实现目录摘要....................................................... 错误!未定义书签。
Abstract ................................................... 错误!未定义书签。
关键词..................................................... 错误!未定义书签。
1.算法描述................................................ 错误!未定义书签。
1.1加/解密算法的一般原理............................... 错误!未定义书签。
1.2加/解密机制的应用................................... 错误!未定义书签。
2.S盒设计................................................ 错误!未定义书签。
3.DES程序实例与分析...................................... 错误!未定义书签。
4.DES实例运行结果........................................ 错误!未定义书签。
5.结语.................................................... 错误!未定义书签。
6.参考文献................................................. 错误!未定义书签。
信息安全概论·课程设计DES加密的C语言实现C language achieve DES algorithm摘要DES算法是一种数据加密算法,自从1977年公布以来,一直是国际上的商用保密通信和计算机通信的最常用的加密标准。
关于实现C#8字节的DES加密算法(DESCryptoServiceProvider)
关于实现C#8字节的DES加密算法(DESCryptoServiceProvider)1.参考资料(1)原理(2)代码<1>C语⾔<2>C#2.C#实现(8字节加密)public class DES{private static int[] C, D;private int[] R, L;public int[] Data;public int[] Ciphertext = new int[64];public static int[] Key;private static int[] Temp, Kong;public static int[,] ZiKeyS = new int[16, 48];private static int[] PC_1HE;private static int[] ShiftHE;private static int[] PC_2HE;private int[] IP_Output;private int[] IPHE;private int[] EHE;private int[,] SHE;private int[] PHE;private int[] FMethod;private int[] SHEOutput;private int[] PHEOutput;private int[] IP_1HE;private int[] XorOutput;public byte[] ciphertext = new byte[8];public DES(){C = new int[28] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };D = new int[28] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };R = new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };L = new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };XorOutput = new int[48]{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};FMethod = new int[48]{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};SHEOutput = new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };PHEOutput = new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };Data = new int[64]{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};Key = new int[64] { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0 };IP_Output = new int[64] { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0 }; PC_1HE = new int[56] {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 };Temp = new int[56] {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 };Kong = new int[56] {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 };PC_2HE = new int[48]{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};ShiftHE = new int[16] { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };SHE = new int[8, 64]{{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 },{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 },{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 },{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 },{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 },{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 },{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 },{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 }};IP_1HE = new int[64]{ 40,8,48,16,56,24,64,32,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 };EHE = new int[48] {32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1 };PHE = new int[32] { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 };IPHE = new int[64]{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};}public void Encryption(int flag, int count, ref byte[] data_1){for (int n = 0; n < count; n++){BTD(data_1, n);int[] IP_ip = new int[64] { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0 };int i;IP_Replace(flag);for (i = 0; i < 16; i++){Ffunction(i, flag);}for (i = 0; i < 32; i++){IP_ip[i] = L[i];IP_ip[i + 32] = R[i];}for (i = 0; i < 64; i++){Ciphertext[i] = IP_ip[IP_1HE[i] - 1];}TTB(ref data_1, n);}}protected void Ffunction(int n, int flag){int i;int[] Lzhongjian = new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };for (i = 0; i < 48; i++){FMethod[i] = R[EHE[i] - 1];}for (i = 0; i < 48; i++){if (flag == 1){XorOutput[i] = FMethod[i] ^ ZiKeyS[n, i];}else{XorOutput[i] = FMethod[i] ^ ZiKeyS[15 - n, i];}}SHE_Replace();for (i = 0; i < 32; i++){PHEOutput[i] = SHEOutput[PHE[i] - 1];}for (i = 0; i < 32; i++){Lzhongjian[i] = L[i];L[i] = R[i];}for (i = 0; i < 32; i++){if (n != 15){R[i] = Lzhongjian[i] ^ PHEOutput[i];}else{R[i] = L[i];L[i] = Lzhongjian[i] ^ PHEOutput[i];}}}protected void SHE_Replace(){int i, j, S_1, S_2;int[] S_S = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };for (i = 0; i < 8; i++){S_1 = XorOutput[i * 6] * 2 + XorOutput[i * 6 + 5];S_2 = XorOutput[i * 6 + 1] * 8 + XorOutput[i * 6 + 2] * 4 + XorOutput[i * 6 + 3] * 2 + XorOutput[i * 6 + 4]; S_S[i] = SHE[i, S_1 * 16 + S_2];}for (i = 0; i < 8; i++){for (j = 3; j >= 0; j--){SHEOutput[i * 4 + j] = S_S[i] % 2;S_S[i] = S_S[i] / 2;}}}protected void IP_Replace(int flag){int i;for (i = 0; i < 64; i++){IP_Output[i] = Data[IPHE[i] - 1];}for (i = 0; i < 32; i++){L[i] = IP_Output[i];R[i] = IP_Output[i + 32];}}public static void MiKey(byte[] key_1){int i;BTK(key_1);PC_1();for (i = 0; i < 16; i++){PC_2(ShiftHE[i], i);}}protected static void PC_1(){int i;for (i = 0; i < 56; i++){Temp[i] = Key[PC_1HE[i] - 1];}for (i = 0; i < 28; i++){C[i] = Temp[i];D[i] = Temp[i + 28];}}protected static void Shift_OP(int k){int i;int a = C[0], b = C[1], c = D[0], d = D[1];if (k == 2){for (i = 0; i < 26; i++){C[i] = C[i + 2];D[i] = D[i + 2];}C[26] = a;C[27] = b;D[26] = c;D[27] = d;}else{for (i = 0; i < 27; i++){C[i] = C[i + 1];D[i] = D[i + 1];}C[27] = a;D[27] = c;}}protected static void PC_2(int p, int LunCount){int i;Shift_OP(p);for (i = 0; i < 28; i++){Temp[i] = C[i];Temp[i + 28] = D[i];}for (i = 0; i < 48; i++){ZiKeyS[LunCount, i] = Temp[PC_2HE[i] - 1];}}protected void BTD(byte[] data_1, int _count){int i, j;int[] S_T_1 = new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 };for (i = 0; i < 8; i++){S_T_1[i] = data_1[i + _count * 8];for (j = 0; j < 8; j++){Data[i * 8 + 7 - j] = S_T_1[i] % 2;S_T_1[i] = S_T_1[i] / 2;}}}protected static void BTK(byte[] key_1){int i, j;int[] S_T_1 = new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 };for (i = 0; i < 8; i++){S_T_1[i] = key_1[i];for (j = 0; j < 8; j++){Key[i * 8 + 7 - j] = S_T_1[i] % 2;S_T_1[i] = S_T_1[i] / 2;}}}protected void TTB(ref byte[] buffer, int _count){int i, j;for (i = 0; i < 8; i++){int zeng = 1;int jieguo = 0;for (j = 0; j < 8; j++){if (j == 0){zeng = 1;}else{zeng = zeng * 2;}jieguo += Ciphertext[i * 8 + 7 - j] * zeng;}buffer[i + _count * 8] = (byte)jieguo;}}}internal sealed class Encryption{public static string EncryptDES(string paymnetCode, byte[] = null){var bytesText = Encoding.UTF8.GetBytes(paymnetCode);int mod = bytesText.Length % 8;int sMod = 8 - mod;byte[] mByte = new byte[bytesText.Length + sMod];for (int i = 0; i < bytesText.Length; i++){mByte[i] = bytesText[i];}for (int i = bytesText.Length; i < mByte.Length; i++){mByte[i] = (byte)sMod;}//==============================================================// if (key == null) key = Encoding.ASCII.GetBytes("默认⽹站密钥");byte[] keybuffer = new byte[8];for (int i = 0; i < keybuffer.Length; i++){keybuffer[i] = i < key.Length ? key[i] : (byte)0;}//==============================================================// DES des = new DES();DES.MiKey(keybuffer);//==============================================================// int div = mByte.Length / 8;if ((mByte.Length % 8) != 0) div += 1;byte[][] divmbyte = new byte[div][];for (int i = 0; i < div; i++){divmbyte[i] = new byte[8];divmbyte[i][0] = mByte[i * 8 + 0];divmbyte[i][1] = mByte[i * 8 + 1];divmbyte[i][2] = mByte[i * 8 + 2];divmbyte[i][3] = mByte[i * 8 + 3];divmbyte[i][4] = mByte[i * 8 + 4];divmbyte[i][5] = mByte[i * 8 + 5];divmbyte[i][6] = mByte[i * 8 + 6];divmbyte[i][7] = mByte[i * 8 + 7];}byte[][] divcint = new byte[div][];for (int i = 0; i < div; i++){divcint[i] = new byte[8];des.Encryption(1, 1, ref divmbyte[i]); divcint[i] = divmbyte[i];}byte[] cint = new byte[div * 8];for (int i = 0; i < div; i++){for (int j = 0; j < 8; j++){cint[i * 8 + j] = divcint[i][j];}}return Convert.ToBase64String(cint); }}。
des密码算法程序c语言
des密码算法程序c语言一、概述DES(数据加密标准)是一种常用的对称加密算法,它采用64位的密钥,对数据进行加密和解密。
本程序使用C语言实现DES算法,包括密钥生成、数据加密和解密等操作。
二、算法实现1.密钥生成:使用初始置换算法IP(56位)将明文转化为56位的分组,再将该分组经过一系列的逻辑函数F进行6轮处理,最终生成一个56位的密文。
其中密钥包括56位数据位和8位奇偶校验位。
2.数据加密:将需要加密的数据转化为56位的分组,再经过DES 算法处理,得到密文。
3.数据解密:将密文经过DES算法处理,还原成原始明文。
三、程序代码```c#include<stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>//DES算法参数定义#defineITERATIONS6//加密轮数#defineKEY_LENGTH8//密钥长度,单位为字节#defineBLOCK_SIZE8//数据分组长度,单位为字节#definePADDINGPKCS7Padding//填充方式#defineMAX_INPUT_LENGTH(BLOCK_SIZE*2)//数据输入的最大长度//初始置换函数voidinit_permutation(unsignedcharinput[BLOCK_SIZE]){inti;for(i=0;i<BLOCK_SIZE;i++){input[i]=i;}}//逻辑函数F的定义voidlogic_function(unsignedcharinput[BLOCK_SIZE],unsigned charoutput[BLOCK_SIZE]){inti;for(i=0;i<BLOCK_SIZE;i++){output[i]=input[(i+1)%BLOCK_SIZE]^input[i]^(i+1)/BLOCK_SI ZE;}}//DES算法主函数voiddes_encrypt(unsignedchar*input,unsignedchar*output){ unsignedcharkey[KEY_LENGTH];//密钥数组unsignedchariv[BLOCK_SIZE];//初始置换的输入数组unsignedcharciphertext[MAX_INPUT_LENGTH];//密文数组unsignedcharpadding[BLOCK_SIZE];//填充数组unsignedintlength=strlen((char*)input);//数据长度(以字节为单位)unsignedintpadding_length=(length+BLOCK_SIZE-1)%BLOCK_SIZE;//需要填充的字节数unsignedintround=0;//加密轮数计数器unsignedintj=0;//数据指针,用于循环读取数据和填充数据intkey_offset=((1<<(32-KEY_LENGTH))-1)<<(32-(ITERATIONS*BLOCK_SIZE));//密钥索引值,用于生成密钥数组和填充数组的初始值unsignedintk=0;//DES算法中每个轮次的密钥索引值,用于生成每个轮次的密钥数组和填充数组的值unsignedintkplus1=(k+1)%((1<<(32-BLOCK_SIZE))-1);//DES算法中每个轮次的密钥索引值加一后的值,用于下一个轮次的密钥生成charseed[32];//使用MD5作为初始种子值生成随机数序列chartmp[MAX_INPUT_LENGTH];//临时变量数组,用于数据交换和中间计算结果存储等操作time_tt;//时间戳变量,用于生成随机数序列的种子值srand((unsignedint)time(&t));//设置随机数种子值,确保每次运行生成的随机数序列不同init_permutation(iv);//初始置换操作,将输入数据转化为56位分组(需要重复填充时)或一个随机的分组(不需要重复填充时)memcpy(key,key_offset,sizeof(key));//将初始化的密钥数组复制到相应的位置上,以便于接下来的轮次生成不同的密钥值memcpy(padding,seed,sizeof(seed));//将种子值复制到填充数组中,以便于接下来的轮次生成不同的随机数序列值for(round=0;round<ITERATIONS;round++){//进行加密轮次操作,每轮包括。
c++实现DES加密算法
#include <stdlib.h>#include <stdio.h>#include <string.h>#define ENCRYPT 1#define DECRYPT 0static void printHex( char *cmd, int len );static void printArray( const char *In, int len );static void F_func(bool In[32], const bool Ki[48]); // f函数static void S_func(bool Out[32], const bool In[48]); // S盒代替static void Transform(bool *Out, bool *In, const char *Table, int len); // 变换static void Xor(bool *InA, const bool *InB, int len); // 异或static void RotateL(bool *In, int len, int loop); // 循环左移static void ByteToBit(bool *Out, const char *In, int bits); // 字节组转换成位组static void BitToByte(char *Out, const bool *In, int bits); // 位组转换成字节组// 16位子密钥static bool SubKey[16][48];// 64位经过PC1转换为56位(PC-1)const static char PC1_Table[56] ={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};// 左移const static char LOOP_Table[16] ={1, 1, 2, 2, 2, 2, 2, 2,1, 2, 2, 2, 2, 2, 2, 1};// 排列选择2 (PC-2)const static char PC2_Table[48] ={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};// Ri_1(32位)经过变换E后膨胀为48位(E) void F_funcstatic const char E_Table[48] ={32, 1, 2, 3, 4, 5,4, 5, 6, 7, 8, 9,8, 9, 10, 11, 12, 13,12, 13, 14, 15, 16, 17,16, 17, 18, 19, 20, 21,20, 21, 22, 23, 24, 25,24, 25, 26, 27, 28, 29,28, 29, 30, 31, 32, 1};// 8个4比特合并为32比特的排列Pconst static char P_Table[32] ={16, 7, 20, 21,29, 12, 28, 17,1, 15, 23, 26,5, 18, 31, 10,2, 8, 24, 14,32, 27, 3, 9,19, 13, 30, 6,22, 11, 4, 25,};// 经过S盒S-boxesconst static char S_Box[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 } }};// 初始排列(IP)const static char IP_Table[64] ={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};// L16与R16合并后经过IP_1的最终排列(IP**-1)const static char IPR_Table[64] ={40, 8, 48, 16, 56, 24, 64, 32,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};void Des_SetKey(const char Key[8]); //生成子密钥void Des_Run(char Out[8], char In[8], bool Type); //DES算法void main(int argc, char *argv[]){char key[12]={1,2,3,4,5,6,7,8};char str[12]="Hello";char str2[12];//printArray( PC2_Table, sizeof(PC2_Table)/sizeof(PC2_Table[0]) );printf("Before encrypting: ");puts(str);Des_SetKey(key);memset(str2, 0, sizeof(str2));Des_Run(str2, str, ENCRYPT);printf("After encrypting: ");printHex( str2, 8 );memset(str, 0, sizeof(str));printf("After decrypting: ");Des_Run(str, str2, DECRYPT);puts(str);}void Des_SetKey(const char Key[8]){int i;static bool K[64], *KL = &K[0], *KR = &K[28];ByteToBit(K, Key, 64); //转换为二进制Transform(K, K, PC1_Table, 56); //64比特的密钥K,经过PC-1后,生成56比特的串。
DES 加密解密算法的C完成 实验报告
美国国家标准局 1973 年开始研究除国防部外的其它部门的计算机系统的数据加密标准, 于 1973 年 5 月 15 日和 1974 年 8 月 27 日先后两次向公众发出了征求加密算法的公告。加 密算法要达到的目的通常称为 DES 密码算法要求主要为以下四点:
DES 算法的入口参数有三个:Key、Data、Mode。其中 Key 为 8 个字节共 64 位,是 DES 算法的工作密钥;Data 也为 8 个字节 64 位,是要被加密或被解密的数据;Mode 为 DES 的工作方式,有两种:加密或解密。
DES 算法是这样工作的:如 Mode 为加密,则用 Key 去把数据 Data 进行加密, 生成 Data 的密码形式(64 位)作为 DES 的输出结果;如 Mode 为解密,则用 Key 去把密码形 式的数据 Data 解密,还原为 Data 的明码形式(64 位)作为 DES 的输出结果。在通信网络 的两端,双方约定一致的 Key,在通信的源点用 Key 对核心数据进行 DES 加密,然后以密 码形式在公共通信网(如电话网)中传输到通信网络的终点,数据到达目的地后,用同样 的 Key 对密码数据进行解密,便再现了明码形式的核心数据。这样,便保证了核心数据 (如 PIN、MAC 等)在公共通信网中传输的安全性和可靠性。
目前在这里,随着三金工程尤其是金卡工程的启动,DES 算法在 POS、ATM、磁卡及 智能卡(IC 卡)、加油站、高速公路收费站等领域被广泛应用,以此来实现关键数据的保 密,如信用卡持卡人的 PIN 的加密传输,IC 卡与 POS 间的双向认证、金融交易数据包的 MAC 校验等,均用到 DES 算法。
DES_加密解密算法的C++实现
string plaintext_;/*明文存储*/ string ciphertext_;/*密文存储*/ string key_;/*密钥存储*/
const unsigned int DES::IPR_[64] = { 40, 8, 48, 16, 56, 24, 64, 32, 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 };
算法的整体结构:有 16 个相同的处理过程,称为“回次”,并在首位各 有一次置换。在主处理回次前,数据块被分成两个 32 位的半块,并被分别 处理。图中的⊕符号代表异或操作。“F 函数”将数据半块与某个子密钥进行 处理。然后,一个 F 函数的输出与另一个半块异或之后,再与原本的半块 组合并交换顺序,进入下一个回次的处理。在最后一个回次完成时,两个 半块不必交换顺序 。
const unsigned int DES::PC_1_[56] = { /*注释的部分是对应 64 位带奇偶校验的*/ /*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*/ 50, 43, 36, 29, 22, 15, 8, 1, 51, 44, 37, 30, 23, 16, 9, 2, 52, 45, 38, 31, 24, 17, 10, 3, 53, 46, 39, 32, 56, 49, 42, 35, 28, 21, 14, 7, 55, 48, 41, 34, 27, 20, 13, 6, 54, 47, 40, 33, 26, 19, 12, 5, 25, 18, 11, 4 };
C语言实现DES加密解密算法
C语言实现DES加密解密算法
最近几十年里,DES(Data Encryption Standard)算法的发展起到
了极其重要的作用。
Des算法是一种基于分组密码的算法。
算法将64位
的明文数据块按位分组成8个字节,每一组以8位为单位转换成一个64
位的密文数据块,采用16轮的分组加密,每次密码变化,保证加密强度。
本文详细介绍了DES算法的C语言实现,并分别介绍了加解密算法的实现
步骤以及DES加解密测试过程。
一、DES算法C语言实现
1.函数原型
DES算法的实现包括加密和解密函数,函数原型如下:
unsigned char* DesEncrypt(unsigned char *src, unsigned char
*key); // DES加密函数
unsigned char* DesDecrypt(unsigned char *src, unsigned char
*key); // DES解密函数
输入参数src是指明文源数据,key是加解密密钥,输出参数为一个
指向加解密结果的字符串指针。
2.加解密算法
(1)DES加密算法
DES加密算法步骤如下:
(i)初始置换:将64位明文块做一次IP置换得到L0R0。
(ii)迭代轮换:对L0R0经过16次迭代轮换后,最终结果为
L16R16
(iii)逆置换:L16R16进行逆置换得到64位密文。
(2)DES解密算法
DES解密算法步骤和DES加密算法步骤是一样的,只是将置换步骤改为逆置换,将轮换步骤改为逆轮换即可。
三、DES加解密测试
1.程序测试
在C语言编写完DES加解密算法之后。
C语言实现数据加密算法
C语言实现数据加密算法数据加密是对敏感信息进行转换的过程,以保护数据的机密性和完整性。
C语言提供了强大的工具和库来实现各种加密算法,包括对称加密和非对称加密等。
对称加密算法是一种使用相同密钥加密和解密数据的方法。
其中最常见的算法是DES(Data Encryption Standard)和AES(Advanced Encryption Standard)。
下面是一个实现AES算法的示例代码:```c#include <stdio.h>#include <stdlib.h>#include <string.h>#include <openssl/aes.h>void encrypt_data(const unsigned char *data, size_t len, const unsigned char *key, unsigned char *encrypted_data) AES_KEY aes_key;AES_set_encrypt_key(key, 128, &aes_key);AES_encrypt(data, encrypted_data, &aes_key);void decrypt_data(const unsigned char *encrypted_data,size_t len, const unsigned char *key, unsigned char *data) AES_KEY aes_key;AES_set_decrypt_key(key, 128, &aes_key);AES_decrypt(encrypted_data, data, &aes_key);int maiunsigned char data[AES_BLOCK_SIZE] = "hello world!";size_t len = sizeof(data);unsigned char encrypted_data[AES_BLOCK_SIZE];encrypt_data(data, len, key, encrypted_data);unsigned char decrypted_data[AES_BLOCK_SIZE];decrypt_data(encrypted_data, len, key, decrypted_data);printf("Original Data: %s\n", data);printf("Encrypted Data: ");for (int i = 0; i < len; i++)printf("%02x ", encrypted_data[i]);}printf("\nDecrypted Data: %s\n", decrypted_data);return 0;```以上代码使用了OpenSSL库中的AES加密算法。
DES加密算法的C语言实现
DES加密算法的C语言实现DES(Data Encryption Standard)是一种对称密钥加密算法,它的核心思想是将明文分成64位的数据块,并通过一系列的轮次操作对数据块进行加密,最终得到密文。
下面是一种用C语言实现DES加密算法的示例代码:```c#include <stdio.h>unsigned char initial_permutation(unsigned char block)unsigned char result = 0;result ,= (block & 0x80) >> 7;result ,= (block & 0x40) >> 5;result ,= (block & 0x20) >> 3;result ,= (block & 0x10) >> 1;result ,= (block & 0x08) << 1;result ,= (block & 0x04) << 3;result ,= (block & 0x02) << 5;result ,= (block & 0x01) << 7;return result;unsigned char final_permutation(unsigned char block)unsigned char result = 0;result ,= (block & 0x80) >> 7;result ,= (block & 0x40) >> 5;result ,= (block & 0x20) >> 3;result ,= (block & 0x10) >> 1;result ,= (block & 0x08) << 1;result ,= (block & 0x04) << 3;result ,= (block & 0x02) << 5;result ,= (block & 0x01) << 7;return result;void des_encrypt(unsigned char* plaintext, unsigned char* key, unsigned char* ciphertext)unsigned char block;unsigned char round_key;unsigned char i;// Initial Permutationblock = initial_permutation(*plaintext);// Round Permutationfor (i = 0; i < 16; i++)round_key = key[i];block ^= round_key;block = substitution(block);block = permutation(block);}// Final Permutation*ciphertext = final_permutation(block);int maiunsigned char plaintext = 0x55; // 明文unsigned char key[16] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}; // 密钥unsigned char ciphertext;des_encrypt(&plaintext, key, &ciphertext);printf("明文: 0x%02X\n", plaintext);printf("密钥: ");for (unsigned char i = 0; i < 16; i++)printf("%02X ", key[i]);}printf("\n");printf("密文: 0x%02X\n", ciphertext);return 0;```上述代码实现了DES算法的加密功能。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
5、按 1 开始进行解密,流程与加密相同,中间过程省去,只给出最终结果:
7、实验总结
通过完成本实验, 我详细了解了 DES 算法的实现过程和工作原理, 相比较在课堂上学到 的理论知识,我认为通过实践后掌握的更加深刻。在课堂上我们学习 DES 算法,很多都是基 于理论上的知识,但并没有真正的使用过 DES,而通过编码实现 DES,我们更好地理解了该 怎么用 DES 去解决实际问题,这对我们以后的学习是很有帮助的,掌握了 DES 的算法实现 后,我们在以后写到相关程序时可以快速的将其利用,使其真正的为我们服务。
DES 加密算法的 C++实现
实验一
1、实验题目
利用 C/C++ 编程实现 DES 加密算法或 MD5 加密算法。我选择的是用 C++ 语言实现 DES 的加密算法。
2、实验目的
通过编码实现 DES 算法或 MD5 算法,深入掌握算法的加密原理,理解其实际应用 价值,同时要求用 C/C++语言实现该算法,让我们从底层开始熟悉该算法的实现过程
2, 4, 6, 8, 1, 3, 5, 7
//逆置换矩阵 int IP_ANTEX[64]= { 40, 8, 48, 16, 56, 24, 64, 32, 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, 41, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 }; //扩展矩阵 int EXTEND[48]= {
3、实验环境
操作系统:WIN7 旗舰版 开发工具:Visual Studio 2010 旗舰版 开发语言:C++
4、实验原理
DES 加密流程:
1
如上图所示为 DES 的加密流程,其中主要包含初始置换,压缩换位 1,压缩换位 2,扩 展置换,S 盒置换,异或运算、终结置换等过程。 初始置换是按照初始置换表将 64 位明文重新排列次序 扩展置换是将原 32 为数据扩展为 48 位数据,它主要由三个目的: 1、产生与子密钥相同的长度 2、提供更长的结果,使其在加密过程中可以被压缩 3、产生雪崩效应,使得输入的一位将影响两个替换 S 盒置换是 DES 算法中最核心的内容, 在 DES 中, 只有 S 盒置换是非线性的, 它比 DES 中其他任何一步都提供更好的安全性 终结置换与初始置换相对应, 它们都不影响 DES 的安全性, 主要目的是为了更容易将 明文与密文数据一字节大小放入 DES 的 f 算法中 DES 解密流程与加密流程基本相同,只不过在进行 16 轮迭代元算时,将子密钥生成的 K 的次序倒过来进行迭代运算
函数执行次序和调用关系关系如下:
3
6、实验结果
1、根据提示,输入任意 8 字节的原文,并将其转换为 64 为二进制明文:
2、将 64 为二进制明文进行初始置换:
3、将原 64 位明文分成左右两半,并对右半边进行扩展置换:
4
4 依次获得子密钥。 按上述流程完成 16 轮迭代运算后进行终结置换, 得到 64 位密文 (整 个过程设计数据过多,没有一一输出,只给出最终截图内容)
7
{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} }, { {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} }, { {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} } }; //直接置换 int DIREX[32]= { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 }; //左移移位表 int MOVELEFT[16]= { 1,1,2,2,2,2,2,2, 1,2,2,2,2,2,2,1 }; //压缩换位表 2 int CutEX[48]= { 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
6
32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 1, 2 }; //S 盒 int S[8][4][16]= { { {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} }, { {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} }, { {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} }, { { 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} }, { {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清晰的认识后,编码过程中我将其分为几个关键部分分别进行编码, 最后将整个过程按顺序执行,即可完成 DES 的加密,代码的主要几个函数如下:
//Byte转为Bit ByteToBit(ElemType ch,ElemType bit[8]) //Bit转为Byte BitToByte(ElemType bit[8],ElemType &ch) //初始置换 InitialEX(ElemType Inorder[64],ElemType Disorder[64]) //终结置换 AntiEx(ElemType Disorder[64]) //扩展置换 ExpandEX(ElemType RightMsg[32],ElemType ExpandMsg[48]) //16轮迭代加密 MoveLeft(ElemType C[28],ElemType D[28],ElemType L0[32],ElemType R0[32])
//置换矩阵 int IP_EX[64]= { 58, 50, 42, 34, 26, 18, 10, 60, 52, 44, 36, 28, 20, 12, 62, 54, 46, 38, 30, 22, 14, 64, 56, 48, 40, 32, 24, 16, 57, 49, 41, 33, 25, 17, 9, 59, 51, 43, 35, 27, 19, 11, 61, 53, 45, 37, 29, 21, 13, 63, 55, 47, 39, 31, 23, 15, };