首页
社区
课程
招聘
[旧帖] [原创]密码学学习之DES(申请邀请) 0.00雪花
发表于: 2009-10-30 14:50 2009

[旧帖] [原创]密码学学习之DES(申请邀请) 0.00雪花

2009-10-30 14:50
2009
最近学习了密码学,看了对称加密的相关文章,于是编程试试DES的效果,以下代码实现了对bmp图像的加解密,并且分析了它的雪崩效应、完整性分析、s盒的线性分析。
  DES算法的相关描述这里不在赘述,不明白的请上网查明,很经典的密码学算法。这里只对代码中重要的部分加以说明。
1.    DES密钥生成算法及其数据结构;
    由用户输入8字节的密钥,经过输入变换转化为56位密钥,再分别将每一位扩充成一个整型(0或1),存至unsigned int peerkey[64];中,此后每轮加密分别使0~27和28~55循环移位,并经置换选择PC2_Table产生本轮密钥。
2.    DES加、解密算法的概要描述及其数据结构设计;
    DES的加、解密算法相同,只是子密钥的使用顺序不同。下面以加密为例:
    读取64位明文,经过初始置换,开始进入16轮循环加密。
    每轮加密将明文划分为32位长的两部分,其中右32位直接作为左32位的输出,右32位输出要经过如下变换:右32位经扩充矩阵E扩展至48位,同本轮密钥(生成过程同1)异或后再经过S盒分组代换变为32位,经过P置换,再同原32位左侧输入异或后形成最终的右32位输出。将此过程重复进行16次,最后的输出左右32位交换,经过逆初始置换后,形成最终密文。
DES的解密算法同加密算法,只不过将16轮加密的子密钥从第十六轮向第一轮输入。
3.    用DES加、解密BMP灰度图像的算法;
    除让出某些特殊格式设置,基本方法同加密文本文件,只不过为保证加密后的数据仍可按图像处理,需让过bmp文件格式的文件头(140byte),加密其后的数据部分。
4.    随机明文生成算法及其数据结构设计;
    首先随机确定一位基准位(每次均不改变),其余位由rand()%2生成,存为int。按此方法生成64个int,然后每8位紧缩成一个char,就生成了随机明文。只不过没有将之以char的形式输出,而是直接以0、1字符的形式输出。
5.    S-盒的差分分布表计算算法;
    DES的S盒是6位输入、4位输出,因此由64种不同的输入差分,16种不同的输出差分。具体计算方法如下:
取遍所有的明文对组合并计算异或,同时根据输入确定输出算出输出异或,使相应的异或序对的计数+1,最后统计各异或序对的计数值即可得出结果。
6.    基于某个线性函数计算的S-盒生成算法及其数据结构设计;
    设计的线性函数:(a*x)mod16 其中a是从17开始取的整数,每用一次,对其加一。x是与16互素的数,对于8组S盒,一共要取32个这样的x。
7.    随机产生的S-盒生成算法及其数据结构设计;
    共生成32组置换(每个S盒4个),一组置换由0~15按某种顺序构成。
生成算法:定义数组flag[16] = {0},用来记录某个数字是否已经产生,若已经产生过则更改相应位为1。这样在生成随机数(mod 16)时可以保证每组置换都由0~15数字构成,且不会出现重复和缺失情况。
代码:
--------------------------des.h-----------------------------------------
#ifndef DES_H_INCLUDED
#define DES_H_INCLUDED
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define ROUND 16
/************get the peer key*****************/
void byte_to_bit(int n,unsigned char * in,int * out);
void GetPeerKey(void);
void Transform(int * out,int * in,const int * table,int n);
void RotateL(int *In, int len, int loop);
void Des_SetKey(void);
void bit_to_byte(int n, unsigned char * out,int * in);
/*****************use the des*****************/
void Xor_func(int *out,const int *in,int n);
void ten_to_bit(int *out,const int s,int n);
void S_single(const int *in,int *out,int i);
void S_func(const int *in,int *out);
void Feistel_Net(const int keyi[48],int in[32]);
void Every_Round_Des(int Li[32],int Ri[32], int keyi[16][48],int i);
void DES(int text[64],int mod);
/*****************file operate****************/
void Set_FileName(FILE ** in,FILE ** out,int mode,char *name);
void Skip_Head(FILE * in,FILE * out);
/*****************use mode********************/
void EBC(void);
void Decrpty_EBC(const char * temp);
void CBC(void);
void Decrpty_CBC(const char * temp);
/*****************analyse ********************/
void SnowSlip(void);
void Snow_Plaintext(int *p1,int *p2);
void Print_File(const int *p,FILE * fp);
void Integrality_Plaintext(int * p,const int anybit,const int bit);
void Integrality(void);
void Differential(void);
void Other_S_Box_Analyse(void);
#endif // DES_H_INCLUDED
--------------------------华丽的分割线----------------------------------

[课程]FART 脱壳王!加量不加价!FART作者讲授!

收藏
免费 0
支持
分享
最新回复 (11)
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
-------------------------------des_key.c--------------------------
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "des.h"
extern int S_Box[8][4][16];
extern int S_Box_standard[8][4][16];
unsigned char key[9];
unsigned int peerkey[64];
unsigned int first_round_key[56];//由peerkey的初始置换得到的key
const int getbit;//得到字节的末位
int subkey[16][48];
/*************从0开始计数的话,以后用表都要减1*****************/
// permuted choice table (key)
const 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
};
// permuted choice key (table)
const 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
};
// number left rotations of pc1
const int LOOP_Table[16] =
{
    1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
};
void bit_to_byte(int n, unsigned char * out,int * in)
{
    int i;
    unsigned char temp[9];
    memset(temp,'\0',9);
    for (i=0;i<n;i++)
        temp[i/8] |= in[i]<<(i%8);
    memcpy(out,temp,9);
}
void byte_to_bit(int n,unsigned char * in,int * out)
{
    int i;
    unsigned char temp[9];
    memset(temp,'\0',9);
    memcpy(temp,in,9);
    for (i=0;i<n;temp[i++/8]>>=1)
        out[i] = temp[i/8]&getbit;

}
void GetPeerKey(void)
{
    printf("Please input your 8 bytes key\n");
    gets(key);
    byte_to_bit(64,key,peerkey);//change byte into bit
}
//change byte into bit
//change the 64-bit key into 56-bit
void Transform(int * out,int * in,const int * table,int n)
{
    int i,j;
    for (i=0,j=0;i<n;i++)
        out[j++]=in[table[i]-1];
}
//Rotate funtion
void RotateL(int *In, int len, int loop)
{
    static int Tmp[64];
    memcpy(Tmp, In, loop);//Store the first (loop) bits to Tmp
    memcpy(In, In+loop, len-loop);//Copy the end of the (len-loop) bits to the front of In
    memcpy(In+len-loop, Tmp, loop);//Move the Stored (loop) bits to the end of Nes In
}
//get every round key
void Des_SetKey(void)
{
    static int *KL = &first_round_key[0], *KR = &first_round_key[28];
    int i;
    GetPeerKey();
    Transform(first_round_key,peerkey,PC1_Table,56);
    for (i=0; i<16; i++)
    {
        RotateL(KL, 28, LOOP_Table[i]); //Left shift circle
        RotateL(KR, 28, LOOP_Table[i]); //Left shift circle
        Transform(subkey[i], first_round_key, PC2_Table, 48);//PC2 Transform
    }
}
-------------------------------华丽的分割线--------------------------

-------------------------------feistel_net.c-------------------------
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "des.h"
extern char key[9];
extern const int getbit;
extern int subkey[16][48];
int S_Box[8][4][16];
// initial permutation IP
const int 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
};
// final permutation IP^-1
const int 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
};
// expansion operation matrix
const 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
};
// 32-bit permutation function P used on the output of the S-boxes
const int 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
};

// The (in)famous S-boxes

int S_Box_standard[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}}
};
void Xor_func(int *out,const int *in,int n)
{
    int i;
    for (i=0;i<n;i++)
        out[i] ^= in[i];
}
void ten_to_bit(int *out,const int s,int n)
{
    int i,j;
    for (j=0,i=n-1;i>=0;i--,j++)
        out[i] = (s >> j) & getbit;
}
void S_single(const int *in,int *out,int i)
{
    int j=(in[0]<<1)+in[5];
    int k=(in[1]<<3)+(in[2]<<2)+(in[3]<<1)+in[4];
    ten_to_bit(out,S_Box[i][j][k],4);
}
void S_func(const int *in,int *out)
{
    int i;
    for (i=0;i<8;i++,in+=6,out+=4)
        S_single(in,out,i);
}
void Feistel_Net(const int keyi[48],int in[32])
{
    static int extend[48],temp[32];
    Transform(extend,in,E_Table,48);
    Xor_func(extend,keyi,48);
    S_func(extend,in);//change 48-bit extend into 32-bit in.
    memcpy(temp,in,32*sizeof(int));
    Transform(in,temp,P_Table,32);//permute
}
void Every_Round_Des(int Li[32],int Ri[32], int keyi[16][48],int i)
{
    int temp[32];
    memcpy(temp,Ri,32*sizeof(int));
    Feistel_Net(keyi[i],Ri);
    Xor_func(Ri,Li,32);
    memcpy(Li,temp,32*sizeof(int));
}
void Exchange(int * left,int * right)
{
    int tmp[32];
    memcpy(tmp,left,32*sizeof(int));
    memcpy(left,right,32*sizeof(int));
    memcpy(right,tmp,32*sizeof(int));
}
void DES(int text[64],int mod)
{
    int i,*Li = &text[0],*Ri = &text[32],tmp[64];
    memcpy(tmp,text,64*sizeof(int));
    Transform(text,tmp,IP_Table,64);//permute plaintext first
    if (mod == 1)
    {
        for (i=0;i<ROUND;i++)//16 rounds
            Every_Round_Des(Li,Ri,subkey,i);
    }
    else
    {
        for (i=15;i>=0;i--)//16 rounds
            Every_Round_Des(Li,Ri,subkey,i);
    }

    Exchange(Li,Ri);//exchange Li and Ri
    memcpy(tmp,text,64*sizeof(int));
    Transform(text,tmp,IPR_Table,64);//permute text at last
}
-------------------------------华丽的分割线--------------------------
2009-10-30 14:53
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
-----------------------------picture.c----------------------------------
#include "des.h"
#define NAME_SIZE 64
extern int S_Box[8][4][16];
extern int S_Box_standard[8][4][16];
void EBC(void)
{
    memcpy(S_Box,S_Box_standard,8*4*16*sizeof(int));
    FILE * infp, * outfp;
    int  b[64],i,n;
    unsigned char a[9],c[9],filename[NAME_SIZE+12],temp[8];
    memset(temp,'\0',8);
    a[8]='\0';
    c[8]='\0';
    Set_FileName(&infp,&outfp,1,filename);
    Skip_Head(infp,outfp);
    while (!feof(infp))
    {
        n=fread(a,1,8,infp);
        if (n<8)
        {
            for (i=n;i<8;i++)
            {
                a[i]=(char)(rand()%255);
            }
        }
        byte_to_bit(64,a,b);
        DES(b,1);
        bit_to_byte(64,c,b);/******bit -> byte*****/
        if (n==8)
            fwrite(c,1,8,outfp);
        else
        {
            int j=0;
            for (i=n;i<8;i++)
                temp[j++] = c[i];
            fwrite(c,1,n,outfp);
        }
    }
    printf("\nEncrypt complete!\nThe picture's name is out_ebc%s\n\n",filename);
    fclose(infp);
    fclose(outfp);
    Decrpty_EBC(temp);
}
void Decrpty_EBC(const char * temp)
{
    FILE * infp, * outfp;
    int  b[64],i,n;
    char a[9],c[9],filename[NAME_SIZE+12];
    a[8]='\0';
    c[8]='\0';
    Set_FileName(&infp,&outfp,2,filename);
    Skip_Head(infp,outfp);
    while (!feof(infp))
    {
        n=fread(a,1,8,infp);
        if (n<8)
        {
            int j=0;
            for (i=n;i<8;i++)
                a[i]=temp[j++];
        }
        byte_to_bit(64,a,b);
        DES(b,2);
        bit_to_byte(64,c,b);/******bit -> byte*****/
        if (n==8)
            fwrite(c,1,8,outfp);
        else
            fwrite(c,1,n,outfp);
    }
    printf("\nDecrypt complete!\nThe picture's name is decrpty_%s\n\n",filename);
    fclose(infp);
    fclose(outfp);
}
void CBC(void)
{
    memcpy(S_Box,S_Box_standard,8*4*16*sizeof(int));
    FILE * infp, * outfp;
    int  b[64],d[64],i,n;
    unsigned char a[9],c[9],filename[NAME_SIZE+15],temp[8],IV[9]={"kerberos"};
    memset(temp,'\0',8);
    a[8]='\0';
    c[8]='\0';

    Set_FileName(&infp,&outfp,3,filename);
    Skip_Head(infp,outfp);
    byte_to_bit(64,IV,d);
    while (!feof(infp))
    {
        n=fread(a,1,8,infp);
        if (n<8)
        {
            for (i=n;i<8;i++)
            {
                a[i]=(char)(rand()%255);
            }
        }
        byte_to_bit(64,a,b);
        Xor_func(b,d,64);
        DES(b,1);
        bit_to_byte(64,c,b);/******bit -> byte*****/
        byte_to_bit(64,c,d);
        if (n==8)
            fwrite(c,1,8,outfp);
        else
        {
            int j=0;
            for (i=n;i<8;i++)
                temp[j++] = c[i];
            fwrite(c,1,n,outfp);
        }
    }
    printf("\nEncrypt complete!\nThe picture's name is out_cbc%s\n\n",filename);
    fclose(infp);
    fclose(outfp);
    Decrpty_CBC(temp);
}
void Decrpty_CBC(const char * temp)
{
    FILE * infp, * outfp;
    int  b[64],d[64],i,n;
    char a[9],c[9],filename[NAME_SIZE+12],IV[9]={"kerberos"};
    a[8]='\0';
    c[8]='\0';
    Set_FileName(&infp,&outfp,4,filename);
    Skip_Head(infp,outfp);
    byte_to_bit(64,IV,d);
    while (!feof(infp))
    {
        n=fread(a,1,8,infp);
        if (n<8)
        {
            int j=0;
            for (i=n;i<8;i++)
                a[i]=temp[j++];
        }
        byte_to_bit(64,a,b);
        DES(b,2);
        Xor_func(b,d,64);
        bit_to_byte(64,c,b);/******bit -> byte*****/
        byte_to_bit(64,a,d);
        if (n==8)
            fwrite(c,1,8,outfp);
        else
            fwrite(c,1,n,outfp);
    }
    printf("\nDecrypt complete!\nThe picture's name is decrpty_%s\n\n",filename);
    fclose(infp);
    fclose(outfp);
}
void Set_FileName(FILE ** in,FILE ** out,int mode,char * name)
{
    char filein[NAME_SIZE],fileout[NAME_SIZE+15];
    memset(filein,'\0',NAME_SIZE);
    memset(fileout,'\0',NAME_SIZE+15);
    if (mode == 1 || mode == 3)
        printf("Please input your picture to encrypt <XXX.bmp>: ");
    else
        printf("Please input your picture to decrypt <XXX.bmp>: ");

    while (gets(filein))
        if (strlen(filein))
            break;
    if (mode == 1)
        strcpy(fileout,"out_ebc");
    else if (mode == 3)
        strcpy(fileout,"out_cbc");
    else
        strcpy(fileout,"decrypt_");
    strncat(fileout,filein,NAME_SIZE);
    strcpy(name,filein);
    if (!(*in=fopen(filein,"rb")) || !(*out=fopen(fileout,"wb")))
    {
        printf("Can't open file!\n");
        exit(1);
    }
}

void Skip_Head(FILE * in,FILE * out)
{
    char buf[140];
    fread(buf,140*sizeof(char),1,in);
    fwrite(buf,140*sizeof(char),1,out);
}
-----------------------------华丽的分割线-----------------------------

-----------------------------analyse.c-------------------------------
#include "des.h"
#define TEXT_NUM 256
const int getbit=0x01;
extern const int IP_Table[64];
extern int subkey[16][48];
static int randbox[8][4][16];
extern int S_Box[8][4][16];
static int linebox[8][4][16];
const static int From16_Prime[32]={3, 5, 7, 9, 11, 13, 15, 17, 23, 19, 25, 21, 33, 31, 27, 29, 39, 37, 41, 35, 43
                                   , 49, 45, 47, 53, 57, 51, 55, 63, 65, 61, 59};
void Print_File(const int *p,FILE * fp)
{
    int i;
    for (i=0;i<64;i++)
    {
        fprintf(fp,"%d",p[i]);
        if (!((i+1)%8))
            fputc(' ',fp);
    }
    fputc('\r',fp);
    fputc('\n',fp);
}
void Snow_Plaintext(int *p1,int *p2)
{
    int i,anybit=rand()%64;

    for (i=0;i<64;i++)
    {
        p1[i] = rand()%2;
        p2[i] = p1[i];
    }
    p2[anybit] = p1[anybit] ^ getbit;
}
void SnowSlip(void)
{
    int p1[64],p2[64];
    FILE *fp;
    int i;
    int *L1i = &p1[0],*R1i = &p1[32],*L2i = &p2[0],*R2i = &p2[32],tmp1[64],tmp2[64];

    memcpy(tmp1,p1,64*sizeof(int));
    Transform(p1,tmp1,IP_Table,64);//permute plaintext first

    memcpy(tmp2,p2,64*sizeof(int));
    Transform(p2,tmp2,IP_Table,64);//permute plaintext first

    if ((fp=fopen("Snowslip_Analyse.txt","wb"))==NULL)
    {
        printf("Can't open the Snowslip_Analyse.txt\r\n");
        exit(1);
    }
    Snow_Plaintext(p1,p2);
    fprintf(fp,"\t\t\t###Snowslip_Analyse###\r\n");
    fprintf(fp,"The plaintexts :\r\n");
    Print_File(p1,fp);
    Print_File(p2,fp);

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

        if (i==0)
            fprintf(fp,"\r\nThis is the 1st round.\r\n");
        else if (i==1)
            fprintf(fp,"\r\nThis is the 2nd round.\r\n");
        else if (i==2)
            fprintf(fp,"\r\nThis is the 3rd round.\r\n");
        else
            fprintf(fp,"\r\nThis is the %dth round.\r\n",i+1);

        Every_Round_Des(L1i,R1i,subkey,i);
        Print_File(p1,fp);
        Every_Round_Des(L2i,R2i,subkey,i);
        Print_File(p2,fp);
        int j,count=0;
        for (j=0;j<64;j++)
            if (p1[j] != p2[j])
                count++;
        fprintf(fp,"%d bits different.\r\n",count);
    }
    printf("Snow Slip Analyse Complete!\nThe result is in the Snowslip_Analyse.txt\n\n");
    fclose(fp);
}
void Integrality_Plaintext(int * p,const int anybit,const int bit)
{
    int i;
    for (i=0;i<64;i++)
        p[i]=rand()%2;
    p[anybit]=bit;
}
void Integrality(void)
{
    int anybit=rand()%64,bit=rand()%2,plain[64];
    int i,count[64];
    memset(count,0,64*sizeof(int));
    for (i=0;i<TEXT_NUM;i++)
    {
        Integrality_Plaintext(plain,anybit,bit);
        DES(plain,1);
        int j;
        for (j=0;j<64;j++)
            if (plain[j])
                count[j]++;
    }
    printf("Integrality Analyse Complete!\nThe result is in the Integrality_Analyse.txt.\n\n");
    FILE * fp=fopen("Integrality_Analyse.txt","wb");
    fprintf(fp,"Every message is the same as %d in %d.\r\n",bit,anybit);
    fprintf(fp,"Every bit is 1 of the frequency.\r\n");
    fprintf(fp,"bit\tfrequency\r\n");
    for (i=0;i<64;i++)
        fprintf(fp,"%d\t%.3f\r\n",i+1,(float)count[i]/TEXT_NUM);
    fclose(fp);
}
int bit_to_ten(int * in,int n)
{
    int i,j,num=0;
    for (j=0,i=n-1;i>=0;j++,i--)
    {
        num += (getbit<<j)*in[i];
    }
    return num;
}
void Differential(void)
{
    int count[8][64][16];
    memset(count,0,8*64*16*sizeof(int));
    int i, j, k;
    FILE * fp=fopen("Differential.txt","wb");
    fprintf(fp,"The Differential analysis of every S-box.\r\n");
    for (i=0;i<8;i++)
    {
        for (j=0;j<64;j++)
        {
            int in1[6]={0},in2[6]={0},out1[4]={0},out2[4]={0};
            for (k=0;k<64;k++)
            {
                ten_to_bit(in1,j,6);//j-->bit(in1)
                S_single(in1,out1,i);
                ten_to_bit(in2,k,6);//k-->bit(in2)
                S_single(in2,out2,i);
                Xor_func(in1,in2,6);//in1 ^ in2 --> in1
                Xor_func(out1,out2,4);//out1 ^ out2 --> out1
                count[i][bit_to_ten(in1,6)][bit_to_ten(out1,4)]++;
            }
        }
    }

    for (i=0;i<8;i++)
    {
        fprintf(fp,"\r\nS-box %d Differential Table:\r\n\t",i+1);
        for (k=0;k<16;k++)
            fprintf(fp,"%d\t",k);
        fprintf(fp,"max-number\tlocation");
        for (j=0;j<64;j++)
        {
            int max=-1,local;
            fprintf(fp,"\r\n%d\t",j);
            for (k=0;k<16;k++)
            {
                if (max<count[i][j][k])
                {
                    max=count[i][j][k];
                    local=k;
                }
                fprintf(fp,"%d\t",count[i][j][k]);
            }
            fprintf(fp,"%d\t\t%d",max,local);
        }
    }
    printf("\nDifferential Analyse Comlpete!\nThe result is in the Differential.txt.\n\n");
    fclose(fp);
}
void Rand_S_Box(void)
{
    int i,j,k,flag[16],temp;
    for (i=0;i<8;i++)
    {
        for (j=0;j<4;j++)
        {
            memset(flag,0,16);
            for (k=0;k<16;k++)
            {
                for (temp=rand()%16;flag[temp];temp=rand()%16);
                randbox[i][j][k]=temp;
            }
        }
    }
    memcpy(S_Box,randbox,8*4*16*sizeof(int));
}
void Line_S_Box(void)
{
    int i,j,k,index,a=17;
    for (i=0;i<8;i++)
    {
        for (j=0;j<4;j++)
        {
            int flag[16]={0};
            for (k=0;k<16;a++,k++)
            {
                for (index=(a*From16_Prime[i*4+j])%16;flag[index];a++,index=(a*From16_Prime[i*4+j])%16);
                flag[index]=1;
                linebox[i][j][k]=index;
            }
        }
    }
    memcpy(S_Box,linebox,8*4*16*sizeof(int));
}
void Control(void)
{
    char ch;
    int choise;
    while (1)
    {
        printf("\t\t1.Snow Slip Analyse.\n");
        printf("\t\t2.Integrality Analyse.\n");
        printf("\t\t3.Differences Analyse.\n");
        printf("\t\t0.Quit.\n");
        printf("Your choice:");
        for (;!isdigit(ch=getchar()););
        choise = ch -'0';
        switch (choise)
        {
        case 1:
            SnowSlip();
            break;
        case 2:
            Integrality();
            break;
        case 3:
            Differential();
            break;
        default:
            return;
        }
    }
}
void Other_S_Box_Analyse(void)
{
    char ch;
    int choise;
    while (1)
    {
        printf("\t\t1.Use S-Line-Box\n\t\t2.Use S-Rand-Box\n\t\t0.Quit.\n");
        printf("Your choice:");
        for (;!isdigit(ch=getchar()););
        choise = ch -'0';
        switch (choise)
        {
        case 1:
            Line_S_Box();
            Control();
            break;
        case 2:
            Rand_S_Box();
            Control();
            break;
        default :
            return;
        }
    }
}

-----------------------------华丽的分割线-----------------------------
2009-10-30 14:56
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
最后是main.c文件
--------------------------main.c------------------------------------
#include "des.h"
extern int subkey[16][48];

int main()
{

    int choise;
    char ch;
    printf("\t\t\t #DES encrypt# \n");
    Des_SetKey();//get the peer key
    while (1)
    {
        printf("\t\t1.Use ECB MODE to encrypt a bmp picture.\n");
        printf("\t\t2.Use CBC MODE to encrypt a bmp picture.\n");
        printf("\t\t3.Snow Slip Analyse.\n");
        printf("\t\t4.Integrality Analyse.\n");
        printf("\t\t5.Differences Analyse.\n");
        printf("\t\t6.Other S-Box Analyse.\n");
        printf("\t\t0.Quit.\n");
        printf("Your choice:");
        for (;!isdigit(ch=getchar()););
        choise = ch -'0';
        switch (choise)
        {
        case 1:
            EBC();
            break;
        case 2:
            CBC();
            break;
        case 3:
            SnowSlip();
            break;
        case 4:
            Integrality();
            break;
        case 5:
            Differential();
            break;
        case 6:
            Other_S_Box_Analyse();
            break;
        default:
            return 0;
        }
    }
    return 0;
}
----------------------------华丽的分割线-----------------------------

以上就是我学习的DES,还请各位高手指教,希望能够申请到邀请码。
2009-10-30 14:58
0
雪    币: 2096
活跃值: (100)
能力值: (RANK:420 )
在线值:
发帖
回帖
粉丝
5
根據我和副壇主討論的結果,有些內容可以在 http://74.125.155.132/search?q=cache:GjObrGnmKYsJ:cms.hit.edu.cn/mod/assignment/view.php%3Fid%3D1888+%E9%9A%8F%E6%9C%BA%E4%BA%A7%E7%94%9F%E7%9A%84S-%E7%9B%92%E7%94%9F%E6%88%90%E7%AE%97%E6%B3%95%E5%8F%8A%E5%85%B6%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E8%AE%BE%E8%AE%A1&cd=1&hl=en&ct=clnk&client=opera 找到一些重覆的內容。
主要包括:

(1) DES 密钥生成算法;

(2) DES 加、解密算法的概要描述;

(3) 随机明文生成算法;

(4) S- 盒的差分分布表计算算法;

(5) 基于某个线性函数计算的 S- 盒 生成算法;

(6)  随机产生的 S- 盒 生成算法;

(7) 自行设计一种 S- 盒 生成算法 ( 选做 ) 。

想請 kerberosdc 解釋一下。
謝謝。

另外,我會請密碼版塊的密碼小組,對DES 學專精的成員參與你這帖的內容討論。
2009-10-30 16:07
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
以上是我通过密码学的学习编写的程序,实现了那个以上的需求,需求全部都是按照你搜索的那个网页的需求来实现的,这样实现起来比较有目的性,既有加密也有解密,并且能够实现分析,从而不用单纯的实现算法,因为通过分析这个算法能够对算法有更深的了解。

  我的解释行么?
2009-10-30 16:27
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
以上是我通过密码学的学习编写的程序,实现了那个以上的需求,需求全部都是按照你搜索的那个网页的需求来实现的,这样实现起来比较有目的性,既有加密也有解密,并且能够实现分析,从而不用单纯的实现算法,因为通过分析这个算法能够对算法有更深的了解。

  我的解释行么?
2009-10-30 16:31
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
支持这样的原创.
2009-10-30 16:48
0
雪    币: 234
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
行。介绍部分真的不怎么,基本可以算转的,毕竟只是讲了DES的算法特征,代码还是不错的,不过LZ怎么把代码切开来的,干脆搞个word下载
2009-10-30 18:33
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
哦,头一次发帖子,没有经验……下回注意……谢谢鼓励。
但是也是应该是原创吧,也就是需求不是我写的吧,我只是按照需求实现的,因为感觉这样的需求比较好。
2009-10-30 18:44
0
雪    币: 234
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
限定标题的话,这种原创贴要注意
1、算法的特征,包括可否逆(不能太多)
2、算法涉及的知识点,比如mod N加等等,这里最好用数学举例说明,可以的话配上详细练习
3、所需要的一些资料,不多的,可能入门的贴稍微多点
4、源码(注意使用下载的方法,要发要缩成1贴,不能断开)
2009-10-30 20:48
0
雪    币: 1334
活跃值: (15)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
12
哪有分析? 分析在哪呢?
不是师兄多犯古, 古人诗句犯师兄
2009-11-1 17:57
0
游客
登录 | 注册 方可回帖
返回
//