精华内容
下载资源
问答
  • DES算法研究与加解密算法C语言的实现.doc
    2021-05-24 04:47:39

    DES 算法研究与实现摘要Abstract

    With the arrival of the information society, people are enjoying information resource of great interest,but also faced with the security of information on the rack. Information security has become a worldwide problems, information security has been a threat to the country's political, Governance, economic, military, cultural, ideological and other areas ,at the same time, the issue of information security is also a possibility of retaining their own personal Privacy key. Information security and social stability is a necessary precondition for security.

    This topic describes the overview of cryptography, DES algorithm development and implementation process.Detailed description of the DES algorithm to decrypt the specific process and its analysis of the system. The use of C-DES decryption process of completion, characteristics and safety, to realize the password techniques and strengthen information security.

    On this basis, the subject realized the DES decryption using C language algorithms, and can debug in VC through the development environment, convenient and efficient implementation of the string and document testing.

    【Keywords】 DES; Decryption; C language; The password system; Key

    目录第1章 绪 论1

    1.1选题背景1

    1.2论文的研究目的和意义1

    1.3论文的主要工作2

    1.4 论文的组织2

    第2章 密码学介绍3

    2.1 密码学概述3

    2.2密码学的发展4

    2.3文件加密解密中密码体制研究6

    2.3.1密码原理7

    2.3.2密码体制7

    第3章 DES算法分析11

    3.1 DES算法描述11

    3.1.1 DES算法的基本描述11

    3.1.2 DES算法的基本设计思想11

    3.2 DES算法的发展11

    3.3 DES算法流程13

    3.3.1初始置换13

    3.3.2 迭代过程14

    3.3.3 子密钥的生成14

    3.3.4 初始逆置换16

    3.4 DES的解密过程17

    3.4.1 DES算法的f函数17

    3.4.2 数据解密19

    3.5 DES算法的安全性21

    3.5.1 弱密钥21

    4.5.2 半弱密钥21

    3.5.3 S-盒的设计原则22

    3.5.4 DES的16轮/圈(Round)22

    3.6 DES的有关变形23

    第4章 DES算法的C语言实现25

    4.1 VC++6.0平台介绍25

    4.2 C语言的特点25

    4.3系统的设计26

    4.4算法的实现27

    4.3 系统的测试38

    结束语42

    参考文献43

    致 谢45

    第1章 绪 论

    1.1 选题背景

    信息既是一种资源也是一种财富。在现代的社会中,通信技术和信息处理日益发达,保护信息的安全,尤其是保护重要信息的安全,已经成为全世界普遍关注的重大问题。随着计算机、因特网以及电子通讯技术的迅速发展,金融电子化的速度大大加快。这种数字化、电子化的趋势几乎已经波及社会生活的各个方面。人与人之间的交往活动,包括金融财务、商业贸易和其他的经济活动,

    更多相关内容
  • DES/3DES算法C语言实现

    2018-06-11 20:52:27
    1、完整的DES/3DES算法C语言实现,可用于金融POS安全认证; 2、DES-ECB加密/解密、DES-CBC加密/解密 3、3DES-ECB加密/解密、3DES-CBC加密/解密 4、内含测试程序,在Linux环境下进入目录后make即可编译,已经在...
  • DES算法c语言实现

    2017-10-07 21:25:17
    按照DES算法的过程,将它加密和解密的过程完全用c语言描述出来。
  • DES算法 C语言实现

    千次阅读 2022-04-07 11:24:52
    DES加密算法C语言实现

    数学形式:

    https://en.wikipedia.org/wiki/Data_Encryption_Standard

    总体结构

    DES总体结构

    定义常用运算

    定义四个函数,分别用于将字符串分割,合并,按位异或,置换

    /*tool.h*/
    void dividegroup(char *c, char *l, char *r, int num);
    void mergegroup(char *l, char *r, char *c, int num);
    void xor(char* c1, char* c2, char* c,int num);
    void permutation(char*x, char*y, const char*f, int size);
    
    /*tool.c*/
    #include <stdio.h>
    #include <string.h>
    
    // divide c to l and r
    void dividegroup(char *c, char *l, char *r, int num)
    {
        strncpy(l, c, num/2);
        strncpy(r, c+num/2, num/2);
    }
    
    void mergegroup(char *l, char *r, char *c, int num)
    {
        strncpy(c, l, num/2);
        strncpy(c+num/2, r, num/2);
    }
    
    void xor (char *c1, char *c2, char *c, int num) {
        for (int i = 0; i < num; i++)
        {
            c[i] = (c1[i] - '0' + c2[i] - '0') % 2 + '0';
        }
    }
    
    void permutation(char *x, char *y, const char *f, int size)
    {
        for (int i = 0; i < size; i++)
        {
            y[i] = x[f[i] - 1];
        }
    }
    

    DES计算

    DES加密计算

    f函数计算:
    F函数

    /*des.h*/
    void encrypt(char *m, char *c, char *k);
    
    /*des.c*/
    #include "des_t.h"
    
    typedef char ki[48];
    
    void f(char *r, char *k)
    {
        char tmp[48];
        permutation(r, tmp, E, 48);
        xor(tmp, k, tmp, 48);
    
        char s_p[32]; // s盒的2进制输出
    
        for (int i = 0; i < 8; i++)
        {
            int row = ((tmp[i * 6] - '0') << 1) + (tmp[i * 6 + 5] - '0'); //第1,6位组成行号
            int col = ((tmp[i * 6 + 1] - '0') << 3) + ((tmp[i * 6 + 2] - '0') << 2) + ((tmp[i * 6 + 3] - '0') << 1) + (tmp[i * 6 + 4] - '0');
    
            int s = S_Box[i][row][col]; // s盒的十进制输出
            for (int j = 0; j < 4; j++)
            {
                s_p[i * 4 + j] = ((s >> (3 - j)) & 1u) + '0';
            }
        }
    
        permutation(s_p, r, P, 32);
    }
    
    void encrypt(char *m, char *c, char *k)
    {
        char *l = (char *)malloc(sizeof(char) * 32);
        char *r = (char *)malloc(sizeof(char) * 32);
    
        char *l1 = (char *)malloc(sizeof(char) * 32);
        char *r1 = (char *)malloc(sizeof(char) * 32);
    
        // key
        ki *sk = (ki *)malloc(sizeof(ki) * 16);
        subkey(k, sk);
    
        // IP
        permutation(m, c, IP, 64);
    
        // 16轮
        dividegroup(c, l, r, 64);
    
        int round = 0;
        while (round < 16)
        {
            strncpy(l1, r, 32);
            f(r, sk[round]);
            xor(l, r, r1, 32);
            round++;
    
            strncpy(l, r1, 32);
            f(r1, sk[round]);
            xor(l1, r1, r, 32);
            round++;
        }
    
        // IP-1
        char tmp[64];
        mergegroup(r, l, tmp, 64);
        permutation(tmp, c, FP, 64);
    }
    

    计算表

    /*des_t.h*/
    #include "tool.h"
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include <string.h>
    typedef char ki[48];
    void subkey(char *k, ki *sk);
    
    const char 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};
    
    const char FP[] =
        {
            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};
    
    const char E[] =
        {
            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};
    
    const char S_Box[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,  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},
       },
    };
    
    const char 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};
    

    轮密钥生成

    轮密钥计算

    /*subkey.c*/
    #include "subkey.h"
    
    void ls(char *c, int shift)
    {
        char tmp[28];
        strncpy(tmp, c, 28);
        for (int i = 0; i < 28; i++)
        {
            c[i] = tmp[(28 + i + shift) % 28];
        }
    }
    
    void subkey(char *k, ki *sk)
    {
        char cd[56];
        char c[28];
        char d[28];
    
        // PC1
        permutation(k, cd, PC1, 56);
        dividegroup(cd, c, d, 56);
    
        for (int r = 0; r < 16; r++)
        {
            ls(c, SHIFTS[r]);
            ls(d, SHIFTS[r]);
            mergegroup(c, d, cd, 56);
            permutation(cd, sk[r], PC2, 48);
        }
    }
    

    计算表

    /*subkey.h*/
    #include "tool.h"
    #include <string.h>
    typedef char ki[48];
    char PC1[] =
        {
            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};
    
    char PC2[] =
        {
            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 SHIFTS[] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
    

    测试DES算法

    /*main.c*/
    #include <stdio.h>
    #include "des.h"
    int main()
    {
        char m[64] = "1111111011011100101110101001100001110110010101000011001000010000";
        char k[64] = "0100100000110010000110101011111110000010100101010011110010111110";
        char c[65];
        encrypt(m, c, k);
    
        c[64] = '\0';
        puts(c);
    
        return 0;
    }
    
    展开全文
  • DES,DES加密算法,DES算法源码。用C写的DES加密算法。 DES,DES加密算法,DES算法源码。 DES加密算法(c语言实现) (本程序可以直接使用)
  • DES算法C语言实现.c

    2020-04-16 11:25:21
    DES算法为密码体制中的对称密码体制,又被称为美国数据加密标准,是1972年美国IBM公司研制的对称密码体制加密算法。 明文按64位进行分组,密钥长64位,密钥事实上是56位参与DES运算(第8、16、24、32、40、48、56、64...
  • 3DES加密和解密算法, 用C语言实现的代码实例 。// printf("Original data: %s\n", data); des(data, key1, len); //产生密钥,并调整数组byte,输出密文。 /*---再解次密---*/ printf("Please input a key2:\n...
  • DES算法C语言代码及实现首先新建头文件des_encode.H内容如下:void EncodeMain(); //EncodeMain functionvoid DecodeMain(); //Sorry ,it has not usedvoid Decode(int *str,int *keychar); //decode :input 8 ...

    DES算法的C语言代码及实现

    首先新建头文件des_encode.H

    内容如下:

    void EncodeMain(); //EncodeMain function

    void DecodeMain(); //Sorry ,it has not used

    void Decode(int *str,int *keychar); //decode :input 8 chars,8 keychars

    void Encode(int *str,int *keychar); //encode: input 8 chars,8 keychars

    void keyBuild(int *keychar); //create key array

    void StrtoBin(int *midkey,int *keychar); //change into binary

    void keyCreate(int *midkey2,int movebit,int i); //call by keyBuild

    void EncodeData(int *lData,int *rData,int *srt); //encodedata function

    void F(int *rData,int *key); //F function

    void Expand(int *rData,int *rDataP); //Expand function

    void ExchangeS(int *rDataP,int *rData); //S-diagram change

    void ExchangeP(int *rData); //P change

    void FillBin(int *rData,int n,int s); // data to binary;call by S-Diagram change function

    void DecodeData(int *str,int *lData,int *rData); //DecodeData from binary

    int IP1[]={58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, //initial change

    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 IP2[]={40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, //opp initial change

    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 s[][4][16]={{ //S-diagram array

    {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,

    展开全文
  • 希望大家能够将这两个名词区别开来,很多时候我们说的DES都是在指DES算法,而不是DES数据加密标准。DES算法是一种典型的分组密码,即将固定长度的明文通过一系列复杂的操作变成同样长度密文的算法。
  • C语言实现了DES、三重DES(3DES)的加解密,支持ECB、CBC模式。 ECB支持64位密钥; CBC支持128和192位密钥
  • DES算法c语言

    2015-05-06 20:23:07
    DES算法C语言设计代码,信息安全加密类
  • des算法c语言源代码

    2019-01-13 23:19:17
    dev c++软件中的des算法源代码ecb类型,有需要的可以下载。
  • DES算法C语言

    2014-11-21 11:21:31
    DES加密解密算法,该算法是《现代密码学》清华大学出版社的课堂要求,非常适合有学习这门课程的同学,学习和参考,具体的算法原理我就不介绍了。代码简单易懂,谢谢~
  • DES算法C语言可运行源代码

    万次阅读 多人点赞 2017-05-19 19:05:05
    代码转载自: ... 题外话1: 代码虽然不是我写的,但我还是针对评论区里的两个问题在代码里加了注释 ( gets_s函数“未定义”、 ...“什么玩意,忽悠人呢,不走那个解密函数直接输出还不是原来输入的明文?...

    代码转载自:

    http://blog.csdn.net/zidane_2014/article/details/37988657

    题外话1:

    代码虽然不是我写的,但我还是针对评论区里的两个问题在代码里加了注释

    (

    gets_s函数“未定义”、

    还一个

    “什么玩意,忽悠人呢,不走那个解密函数直接输出还不是原来输入的明文?你这和直接输出明文有啥区别?”

    ),

    我请注意看

    题外话2:

    景中老师的《网络信息安全》课怎么样啊,2000字手写论文完成了吗(#滑稽)

    我是14级的,当时多半都用我的报告呢~(报告你们自己写去吧,我不给,给你们几个攒报告的链接):

    https://blog.csdn.net/github_38140310/article/details/68955580

    https://blog.csdn.net/haroroda/article/details/45769507

    https://blog.csdn.net/lisonglisonglisong/article/details/41777413

    下面就是能执行的代码了,也提一句8字节是4个汉字/8个数字或字母,验机时别露怯哦~

    #include<stdio.h>  
    #include<string.h>  
    //#define gets_s gets   //出现[Error] 'gets_s' was not declared in this scope的话就加上这句,默认使用的gcc编译器的话会出现此错误,因为gets_s是vs提供的函数
    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盒   三维数组  
    											// 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
    };
    static void CharToBit(const char input[], int output[], int bits)//把CHAR转换为INT  
    {
    	int i, j;
    	for (j = 0; j<8; j++)
    	{
    		for (i = 0; i<8; i++)
    		{
    			output[7 * (j + 1) - i + j] = (input[j] >> i) & 1;
    		}
    	}
    };
    static void BitToChar(const int intput[], char output[], int bits)//把INT转换为CHAR  
    {
    	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);
    	}
    };
    static  void IP(const int input[64], int output[64], int table[64])//初始IP置换  
    {
    	int i;
    	for (i = 0; i<64; i++)
    	{
    		output[i] = input[table[i] - 1];//减1操作不可少!!  
    	}
    };
    static  void E(const int input[32], int output[48], int table[48])//E扩展  
    {
    	int i;
    	for (i = 0; i<48; i++)
    	{
    		output[i] = input[table[i] - 1];
    	}
    };
    static  void P(const int input[32], int output[32], int table[32])//P置换  
    {
    	int i;
    	for (i = 0; i<32; i++)
    	{
    		output[i] = input[table[i] - 1];
    	}
    };
    static  void IP_In(const int input[64], int output[64], int table[64])//逆IP  
    {
    	int i;
    	for (i = 0; i<64; i++)
    	{
    		output[i] = input[table[i] - 1];
    	}
    };
    static  void PC_1(const int input[64], int output[56], int table[56])//PC_1  
    {
    	int i;
    	for (i = 0; i<56; i++)
    	{
    		output[i] = input[table[i] - 1];
    	}
    };
    static  void PC_2(const int input[56], int output[48], int table[48])//PC_2  
    {
    	int i;
    	for (i = 0; i<48; i++)
    	{
    		output[i] = input[table[i] - 1];
    	}
    };
    static  void S(const int input[48], int output[32], int table[8][4][16])//S盒压缩  
    {
    	int i = 0;
    	int j = 0;
    	int INT[8];
    	for (; i<48; i = 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[3 * (j + 1) - i + j] = (INT[j] >> i) & 1;
    		}
    	}
    };
    static void F_func(int input[32], int output[32], int subkey[48])//完成DES算法轮变换  
    {
    	int len = 48;
    	int temp[48] = { 0 };
    	int temp_1[32] = { 0 };
    	E(input, temp, E_Table);
    	Xor(temp, subkey, len);
    	S(temp, temp_1, S_Box);
    	P(temp_1, output, P_Table);
    };
    static void RotateL(const int input[28], int output[28], int leftCount)//秘钥循环左移  
    {
    	int i;
    	int len = 28;
    	for (i = 0; i<len; i++)
    	{
    		output[i] = input[(i + leftCount) % len];
    	}
    };
    static void  subKey_fun(const int input[64], int Subkey[16][48])//子密钥生成  
    {
    	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];
    		d[i] = pc_1[i + 28];
    	}
    	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++)//最后一轮的操作  
    	{
    		r[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("请输入明文(8字节)\n");
    	gets_s(MIN);
    	printf("请输入秘钥(8字节)\n");
    	gets_s(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");
    
            for(int i=0;i<8;i++) MIN[i]='0';//评论区见到有人不服,没看懂函数就说删掉解密部分输出明文没变,那就在解密前加上个赋值,你再试试?
    
    	printf("解密功能\n");
    	DES_Dfun(output, MI, MIN);
    	printf("明文如下:\n");
    	for (int i = 0; i<8; i++)
    	{
    		printf("%c", MIN[i]);
    	}
    	printf("\n\n");
    	return 0;
    }
    

     

     

     

     

     

     

     

    展开全文
  • DES算法C语言实现

    2013-04-16 13:02:48
    这是一个用C语言实现的DES算法,里面有完整的注释,能正确运行。
  • 用vs2012写的,可以直接运行想要代码的话,都在一个cpp文件中
  • DES-加密解密语言算法C语言实现文档带有完整的源代码,可加密字符串或者文件,已通过测试使用。
  • 调试通过的代码,适合用于密码学,C语言等课程的小作业
  • 3DES加密算法C语言实现

    热门讨论 2013-09-26 18:45:04
    利用C语言编写的3DES加密算法,能够对任意长度的字符数组进行加解密。通过编写相应的JNI接口文件能够很容易移植到Android平台,从而解决不同平台DES加解密结果不一致的问题。
  • DES分组加密 很经典 很不错 很强大 密码学上一个传奇的神话 有用的快抢
  • 基于STM32的软件加解密算法,包括DES,3DES的ECB,CBC模式。但是验证时CBC模式的初始向量为0时,数据的加解密正确,但是初始向量不为0时,则加解数据有错误。 注意:经测试DES,3DES的CBC模式初始向量不为0时,加...
  • ![这里写图片描述][70]![这里写图片描述][70 1]![这里写图片描述][70 2]## main.cpp ###include#include#include"tables.h" //数据表//置换函数//参数: In:待置换数据指针//Out:置换输出指针// n:置换表长度//P:...
  • DES算法代码实现(C语言

    千次阅读 多人点赞 2020-08-16 16:34:41
    通过C语言模拟DES算法的整个加密过程 初始明文(64位),首先通过IP置换表进行置换,然后将置换后的结果分成左半部分L0(32位)和右半部分R0(32位),右半部分R0直接进行交换为下一轮的L1,左半部分L0与作为输入...
  • 2. 统计DES算法在密钥固定情况,输入明文改变1位、2位,。。。64位时。输出密文位数改变情况。 3. 统计DES算法在明文固定情况,输入密钥改变1位、2位,。。。64位时。输出密文位数改变情况。 为了具有客观性,2,3...
  • 3des加密算法C语言实现

    热门讨论 2013-10-25 18:10:14
    3DES加密算法C语言实现代码,很好很实用.
  • des算法C语言实现版本

    2009-10-13 00:38:39
    标准C语言实现DES算法。一个.h和一个.cpp
  • DES算法详解及实现(C语言

    千次阅读 2022-02-23 17:37:10
    DES算法详解及C语言实现

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,308
精华内容 1,723
关键字:

des算法c语言