zoukankan      html  css  js  c++  java
  • DES加密算法-C语言

    头文件:DES.h

     1 #ifndef DES_hpp
     2 #define DES_hpp
     3 
     4 #include <stdio.h>
     5 #include <memory.h>
     6 #include <time.h>
     7 #include <stdlib.h>
     8 
     9 #define PLAIN_FILE_OPEN_ERROR -1
    10 #define KEY_FILE_OPEN_ERROR -2
    11 #define CIPHER_FILE_OPEN_ERROR -3
    12 #define OK 1
    13 
    14 typedef char ElemType;
    15 
    16 int ByteToBit(ElemType ch,ElemType bit[8]);
    17 int BitToByte(ElemType bit[8],ElemType *ch);
    18 int Char8ToBit64(ElemType ch[8],ElemType bit[64]);
    19 int Bit64ToChar8(ElemType bit[64],ElemType ch[8]);
    20 int DES_MakeSubKeys(ElemType key[64],ElemType subKeys[16][48]);
    21 int DES_PC1_Transform(ElemType key[64], ElemType tempbts[56]);
    22 int DES_PC2_Transform(ElemType key[56], ElemType tempbts[48]);
    23 int DES_ROL(ElemType data[56], int time);
    24 int DES_IP_Transform(ElemType data[64]);
    25 int DES_IP_1_Transform(ElemType data[64]);
    26 int DES_E_Transform(ElemType data[48]);
    27 int DES_P_Transform(ElemType data[32]);
    28 int DES_SBOX(ElemType data[48]);
    29 int DES_XOR(ElemType R[48], ElemType L[48],int count);
    30 int DES_Swap(ElemType left[32],ElemType right[32]);
    31 int DES_EncryptBlock(ElemType plainBlock[8], ElemType subKeys[16][48], ElemType cipherBlock[8]);
    32 int DES_DecryptBlock(ElemType cipherBlock[8], ElemType subKeys[16][48], ElemType plainBlock[8]);
    33 int DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile);
    34 int DES_Decrypt(char *cipherFile, char *keyStr,char *plainFile);
    35 
    36 #endif /* DES_hpp */
    View Code

    C代码:DES.cpp

      1 #include "DES.h"
      2 
      3 //初始置换表IP
      4 int IP_Table[64] = {  57,49,41,33,25,17,9,1,
      5     59,51,43,35,27,19,11,3,
      6     61,53,45,37,29,21,13,5,
      7     63,55,47,39,31,23,15,7,
      8     56,48,40,32,24,16,8,0,
      9     58,50,42,34,26,18,10,2,
     10     60,52,44,36,28,20,12,4,
     11     62,54,46,38,30,22,14,6};
     12 //逆初始置换表IP^-1
     13 int IP_1_Table[64] = {39,7,47,15,55,23,63,31,
     14     38,6,46,14,54,22,62,30,
     15     37,5,45,13,53,21,61,29,
     16     36,4,44,12,52,20,60,28,
     17     35,3,43,11,51,19,59,27,
     18     34,2,42,10,50,18,58,26,
     19     33,1,41,9,49,17,57,25,
     20     32,0,40,8,48,16,56,24};
     21 
     22 //扩充置换表E
     23 int E_Table[48] = {31, 0, 1, 2, 3, 4,
     24     3,  4, 5, 6, 7, 8,
     25     7,  8,9,10,11,12,
     26     11,12,13,14,15,16,
     27     15,16,17,18,19,20,
     28     19,20,21,22,23,24,
     29     23,24,25,26,27,28,
     30     27,28,29,30,31, 0};
     31 
     32 //置换函数P
     33 int P_Table[32] = {15,6,19,20,28,11,27,16,
     34     0,14,22,25,4,17,30,9,
     35     1,7,23,13,31,26,2,8,
     36     18,12,29,5,21,10,3,24};
     37 
     38 //S盒
     39 int S[8][4][16] =//S1
     40 {{{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
     41     {0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
     42     {4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
     43     {15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}},
     44     //S2
     45     {{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
     46         {3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
     47         {0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
     48         {13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}},
     49     //S3
     50     {{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
     51         {13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
     52         {13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
     53         {1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}},
     54     //S4
     55     {{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
     56         {13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
     57         {10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
     58         {3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}},
     59     //S5
     60     {{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
     61         {14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
     62         {4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
     63         {11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}},
     64     //S6
     65     {{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
     66         {10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
     67         {9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
     68         {4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}},
     69     //S7
     70     {{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
     71         {13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
     72         {1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
     73         {6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}},
     74     //S8
     75     {{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
     76         {1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
     77         {7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
     78         {2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}}};
     79 //置换选择1
     80 int PC_1[56] = {56,48,40,32,24,16,8,
     81     0,57,49,41,33,25,17,
     82     9,1,58,50,42,34,26,
     83     18,10,2,59,51,43,35,
     84     62,54,46,38,30,22,14,
     85     6,61,53,45,37,29,21,
     86     13,5,60,52,44,36,28,
     87     20,12,4,27,19,11,3};
     88 
     89 //置换选择2
     90 int PC_2[48] = {13,16,10,23,0,4,2,27,
     91     14,5,20,9,22,18,11,3,
     92     25,7,15,6,26,19,12,1,
     93     40,51,30,36,46,54,29,39,
     94     50,44,32,46,43,48,38,55,
     95     33,52,45,41,49,35,28,31};
     96 
     97 //对左移次数的规定
     98 int MOVE_TIMES[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
     99 
    100 //字节转换成二进制
    101 int ByteToBit(ElemType ch, ElemType bit[8]){
    102     int cnt;
    103     for(cnt = 0;cnt < 8; cnt++){
    104         *(bit+cnt) = (ch>>cnt)&1;
    105     }
    106     return 0;
    107 }
    108 
    109 //二进制转换成字节
    110 int BitToByte(ElemType bit[8],ElemType *ch){
    111     int cnt;
    112     for(cnt = 0;cnt < 8; cnt++){
    113         *ch |= *(bit + cnt)<<cnt;
    114     }
    115     return 0;
    116 }
    117 
    118 //将长度为8的字符串转为二进制位串
    119 int Char8ToBit64(ElemType ch[8],ElemType bit[64]){
    120     int cnt;
    121     for(cnt = 0; cnt < 8; cnt++){
    122         ByteToBit(*(ch+cnt),bit+(cnt<<3));
    123     }
    124     return 0;
    125 }
    126 
    127 //将二进制位串转为长度为8的字符串
    128 int Bit64ToChar8(ElemType bit[64],ElemType ch[8]){
    129     int cnt;
    130     memset(ch,0,8);
    131     for(cnt = 0; cnt < 8; cnt++){
    132         BitToByte(bit+(cnt<<3),ch+cnt);
    133     }
    134     return 0;
    135 }
    136 
    137 //生成子密钥
    138 int DES_MakeSubKeys(ElemType key[64],ElemType subKeys[16][48]){
    139     ElemType temp[56];
    140     int cnt;
    141     DES_PC1_Transform(key,temp);//PC1置换
    142     for(cnt = 0; cnt < 16; cnt++){//16轮跌代,产生16个子密钥
    143         DES_ROL(temp,MOVE_TIMES[cnt]);//循环左移
    144         DES_PC2_Transform(temp,subKeys[cnt]);//PC2置换,产生子密钥
    145     }
    146     return 0;
    147 }
    148 
    149 //密钥置换1
    150 int DES_PC1_Transform(ElemType key[64], ElemType tempbts[56]){
    151     int cnt;
    152     for(cnt = 0; cnt < 56; cnt++){
    153         tempbts[cnt] = key[PC_1[cnt]];
    154     }
    155     return 0;
    156 }
    157 
    158 //密钥置换2
    159 int DES_PC2_Transform(ElemType key[56], ElemType tempbts[48]){
    160     int cnt;
    161     for(cnt = 0; cnt < 48; cnt++){
    162         tempbts[cnt] = key[PC_2[cnt]];
    163     }
    164     return 0;
    165 }
    166 
    167 //循环左移
    168 int DES_ROL(ElemType data[56], int time){
    169     ElemType temp[56];
    170     
    171     //保存将要循环移动到右边的位
    172     memcpy(temp,data,time);
    173     memcpy(temp+time,data+28,time);
    174     
    175     //前28位移动
    176     memcpy(data,data+time,28-time);
    177     memcpy(data+28-time,temp,time);
    178     
    179     //后28位移动
    180     memcpy(data+28,data+28+time,28-time);
    181     memcpy(data+56-time,temp+time,time);
    182     
    183     return 0;
    184 }
    185 
    186 //IP置换
    187 int DES_IP_Transform(ElemType data[64]){
    188     int cnt;
    189     ElemType temp[64];
    190     for(cnt = 0; cnt < 64; cnt++){
    191         temp[cnt] = data[IP_Table[cnt]];
    192     }
    193     memcpy(data,temp,64);
    194     return 0;
    195 }
    196 
    197 //IP逆置换
    198 int DES_IP_1_Transform(ElemType data[64]){
    199     int cnt;
    200     ElemType temp[64];
    201     for(cnt = 0; cnt < 64; cnt++){
    202         temp[cnt] = data[IP_1_Table[cnt]];
    203     }
    204     memcpy(data,temp,64);
    205     return 0;
    206 }
    207 
    208 //扩展置换
    209 int DES_E_Transform(ElemType data[48]){
    210     int cnt;
    211     ElemType temp[48];
    212     for(cnt = 0; cnt < 48; cnt++){
    213         temp[cnt] = data[E_Table[cnt]];
    214     }
    215     memcpy(data,temp,48);
    216     return 0;
    217 }
    218 
    219 //P置换
    220 int DES_P_Transform(ElemType data[32]){
    221     int cnt;
    222     ElemType temp[32];
    223     for(cnt = 0; cnt < 32; cnt++){
    224         temp[cnt] = data[P_Table[cnt]];
    225     }
    226     memcpy(data,temp,32);
    227     return 0;
    228 }
    229 
    230 //异或
    231 int DES_XOR(ElemType R[48], ElemType L[48] ,int count){
    232     int cnt;
    233     for(cnt = 0; cnt < count; cnt++){
    234         R[cnt] ^= L[cnt];
    235     }
    236     return 0;
    237 }
    238 
    239 //S盒置换
    240 int DES_SBOX(ElemType data[48]){
    241     int cnt;
    242     int line,row,output;
    243     int cur1,cur2;
    244     for(cnt = 0; cnt < 8; cnt++){
    245         cur1 = cnt*6;
    246         cur2 = cnt<<2;
    247         
    248         //计算在S盒中的行与列
    249         line = (data[cur1]<<1) + data[cur1+5];
    250         row = (data[cur1+1]<<3) + (data[cur1+2]<<2)
    251         + (data[cur1+3]<<1) + data[cur1+4];
    252         output = S[cnt][line][row];
    253         
    254         //化为2进制
    255         data[cur2] = (output&0X08)>>3;
    256         data[cur2+1] = (output&0X04)>>2;
    257         data[cur2+2] = (output&0X02)>>1;
    258         data[cur2+3] = output&0x01;
    259     }
    260     return 0;
    261 }
    262 
    263 //交换
    264 int DES_Swap(ElemType left[32], ElemType right[32]){
    265     ElemType temp[32];
    266     memcpy(temp,left,32);
    267     memcpy(left,right,32);
    268     memcpy(right,temp,32);
    269     return 0;
    270 }
    271 
    272 //加密单个分组
    273 int DES_EncryptBlock(ElemType plainBlock[8], ElemType subKeys[16][48], ElemType cipherBlock[8]){
    274     ElemType plainBits[64];
    275     ElemType copyRight[48];
    276     int cnt;
    277     
    278     Char8ToBit64(plainBlock,plainBits);
    279     //初始置换(IP置换)
    280     DES_IP_Transform(plainBits);
    281     
    282     //16轮迭代
    283     for(cnt = 0; cnt < 16; cnt++){
    284         memcpy(copyRight,plainBits+32,32);
    285         //将右半部分进行扩展置换,从32位扩展到48位
    286         DES_E_Transform(copyRight);
    287         //将右半部分与子密钥进行异或操作
    288         DES_XOR(copyRight,subKeys[cnt],48);
    289         //异或结果进入S盒,输出32位结果
    290         DES_SBOX(copyRight);
    291         //P置换
    292         DES_P_Transform(copyRight);
    293         //将明文左半部分与右半部分进行异或
    294         DES_XOR(plainBits,copyRight,32);
    295         if(cnt != 15){
    296             //最终完成左右部的交换
    297             DES_Swap(plainBits,plainBits+32);
    298         }
    299     }
    300     //逆初始置换(IP^1置换)
    301     DES_IP_1_Transform(plainBits);
    302     Bit64ToChar8(plainBits,cipherBlock);
    303     return 0;
    304 }
    305 
    306 //解密单个分组
    307 int DES_DecryptBlock(ElemType cipherBlock[8], ElemType subKeys[16][48],ElemType plainBlock[8]){
    308     ElemType cipherBits[64];
    309     ElemType copyRight[48];
    310     int cnt;
    311     
    312     Char8ToBit64(cipherBlock,cipherBits);
    313     //初始置换(IP置换)
    314     DES_IP_Transform(cipherBits);
    315     
    316     //16轮迭代
    317     for(cnt = 15; cnt >= 0; cnt--){
    318         memcpy(copyRight,cipherBits+32,32);
    319         //将右半部分进行扩展置换,从32位扩展到48位
    320         DES_E_Transform(copyRight);
    321         //将右半部分与子密钥进行异或操作
    322         DES_XOR(copyRight,subKeys[cnt],48);
    323         //异或结果进入S盒,输出32位结果
    324         DES_SBOX(copyRight);
    325         //P置换
    326         DES_P_Transform(copyRight);
    327         //将明文左半部分与右半部分进行异或
    328         DES_XOR(cipherBits,copyRight,32);
    329         if(cnt != 0){
    330             //最终完成左右部的交换
    331             DES_Swap(cipherBits,cipherBits+32);
    332         }
    333     }
    334     //逆初始置换(IP^1置换)
    335     DES_IP_1_Transform(cipherBits);
    336     Bit64ToChar8(cipherBits,plainBlock);
    337     return 0;
    338 }
    339 
    340 //加密文件
    341 int DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile){
    342     FILE *plain,*cipher;
    343     int count;
    344     ElemType plainBlock[8],cipherBlock[8],keyBlock[8];
    345     ElemType bKey[64];
    346     ElemType subKeys[16][48];
    347     if((plain = fopen(plainFile,"rb")) == NULL){
    348         return PLAIN_FILE_OPEN_ERROR;
    349     }
    350     if((cipher = fopen(cipherFile,"wb")) == NULL){
    351         return CIPHER_FILE_OPEN_ERROR;
    352     }
    353     //设置密钥
    354     memcpy(keyBlock,keyStr,8);
    355     //将密钥转换为二进制流
    356     Char8ToBit64(keyBlock,bKey);
    357     //生成子密钥
    358     DES_MakeSubKeys(bKey,subKeys);
    359     while(!feof(plain)){
    360         //每次读8个字节,并返回成功读取的字节数
    361         if((count = fread(plainBlock,sizeof(char),8,plain)) == 8){
    362             DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
    363             fwrite(cipherBlock,sizeof(char),8,cipher);
    364         }
    365     }
    366     if(count){
    367         //填充
    368         memset(plainBlock + count,'',7 - count);
    369         //最后一个字符保存包括最后一个字符在内的所填充的字符数量
    370         plainBlock[7] = 8 - count;
    371         DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
    372         fwrite(cipherBlock,sizeof(char),8,cipher);
    373     }
    374     fclose(plain);
    375     fclose(cipher);
    376     return OK;
    377 }
    378 
    379 //解密文件
    380 int DES_Decrypt(char *cipherFile, char *keyStr,char *plainFile){
    381     FILE *plain, *cipher;
    382     int count,times = 0;
    383     long fileLen;
    384     ElemType plainBlock[8],cipherBlock[8],keyBlock[8];
    385     ElemType bKey[64];
    386     ElemType subKeys[16][48];
    387     if((cipher = fopen(cipherFile,"rb")) == NULL){
    388         return CIPHER_FILE_OPEN_ERROR;
    389     }
    390     if((plain = fopen(plainFile,"wb")) == NULL){
    391         return PLAIN_FILE_OPEN_ERROR;
    392     }
    393     
    394     //设置密钥
    395     memcpy(keyBlock,keyStr,8);
    396     //将密钥转换为二进制流
    397     Char8ToBit64(keyBlock,bKey);
    398     //生成子密钥
    399     DES_MakeSubKeys(bKey,subKeys);
    400     
    401     //取文件长度
    402     fseek(cipher,0,SEEK_END);   //将文件指针置尾
    403     fileLen = ftell(cipher);    //取文件指针当前位置
    404     rewind(cipher);             //将文件指针重指向文件头
    405     while(1){
    406         //密文的字节数一定是8的整数倍
    407         fread(cipherBlock,sizeof(char),8,cipher);
    408         DES_DecryptBlock(cipherBlock,subKeys,plainBlock);
    409         times += 8;
    410         if(times < fileLen){
    411             fwrite(plainBlock,sizeof(char),8,plain);
    412         }
    413         else{
    414             break;
    415         }
    416     }
    417     //判断末尾是否被填充
    418     if(plainBlock[7] < 8){
    419         for(count = 8 - plainBlock[7]; count < 7; count++){
    420             if(plainBlock[count] != ''){
    421                 break;
    422             }
    423         }
    424     }
    425     if(count == 7){//有填充
    426         fwrite(plainBlock,sizeof(char),8 - plainBlock[7],plain);
    427     }
    428     else{//无填充
    429         fwrite(plainBlock,sizeof(char),8,plain);
    430     }
    431     
    432     fclose(plain);
    433     fclose(cipher);
    434     return OK;
    435 }
    View Code

     main.cpp

     1 #include <iostream>
     2 
     3 #include "DES.h"
     4 
     5 int main(int argc, const char * argv[]) {
     6     // insert code here...
     7     std::cout << "CrypDes Start!
    ";
     8     std::cout << "加密文件:res/test_origin.png
    ";
     9     DES_Encrypt("res/test_origin.png","kkkkkkkk","res/test_encrypt.png");
    10     std::cout << "输出加密文件:res/test_encrypt.png
    ";
    11     std::cout << "解密文件:res/test_encrypt.png
    ";
    12     DES_Decrypt("res/test_encrypt.png","kkkkkkkk","res/test_decrypt.png");
    13     std::cout << "输出加密文件:res/test_decrypt.png
    ";
    14     return 0;
    15 }
    View Code

    输出:

  • 相关阅读:
    java 取汉字首字母
    详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
    详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(上)
    IntelliJ IDEA maven项目new里没有package
    IntelliJ IDEA上操作GitHub
    IntelliJ IDEA部署tomcat时Edit Configuration Deployment无artifact选项
    Java开发需掌握的常用Linux命令(持续更新)
    Spring JDBC 示例
    java 获取日期的几天前,几个月前和几年前
    Anaconda3(0)环境基本使用
  • 原文地址:https://www.cnblogs.com/ring1992/p/11866490.html
Copyright © 2011-2022 走看看