DES演算法C++程式碼實現-密碼學

kewlgrl發表於2016-11-08

DES演算法

 

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <bitset>
#include <cstring>
using namespace std;

int ss[8][4][16];//S盒
void Init(int in[],int out[],int ip[],int l[],int r[],int e[], int k[],int kk[],int kcd[],int pc1[],int pc2[],int c[],int d[],int cc[],int dd[],int p[],int fp[],int fout[],int nip[]) //初始化
{

    for(int i=0; i<64; ++i)
        cin>>in[i];//加密明文
    for(int i=0; i<64; ++i)
        cin>>ip[i];//初始置換IP
    for(int i=0; i<48; ++i)
        cin>>e[i];//輪函式置換E
    for(int i=0; i<64; ++i)
        cin>>k[i];
    for(int i=0; i<56; ++i)
        cin>>pc1[i];//置換選擇1
    for(int i=0; i<48; ++i)
        cin>>pc2[i];//置換選擇2
    for(int i=0; i<32; ++i)
        cin>>p[i];
    for(int i=0; i<64; ++i)
        cin>>nip[i];
    for(int u=0; u<8; ++u)//S盒
        for(int i=0; i<4; ++i)
            for(int j=0; j<16; ++j)
                cin>>ss[u][i][j];
}

void ShiftIP(int in[],int out[],int n,int ip[])//初始置換IP
{
    for(int i=0; i<n; ++i)
        out[i]=in[ip[i]-1];
    cout<<"初始置換IP:";
    for(int i=0; i<64; ++i)
        cout<<out[i];
    puts("");
    puts("");
}

void SplitLR(int out[],int l[],int r[],int n)//分割成L0和R0
{
    for(int i=0; i<n/2; ++i)
        l[i]=out[i];
    int j=0;
    for(int i=n/2; i<n; ++i)
        r[j++]=out[i];
}
void ShiftPC1(int k[],int pc1[],int kk[])//K置換選擇1
{
    for(int i=0; i<56; ++i)
        kk[i]=k[pc1[i]-1];
}
void SplitCD(int kk[],int c[],int d[])//子金鑰分割
{
    for(int i=0; i<28; ++i)
        c[i]=kk[i];
    int j=0;
    for(int i=28; i<56; ++i)
        d[j++]=kk[i];
}
void LS(int pos,int c[],int d[],int cc[],int dd[])//迴圈左移pos個位置
{
    for(int i=0; i<28; ++i)
        cc[i]=c[(i+pos)%28];
    for(int i=0; i<28; ++i)
        dd[i]=d[(i+pos)%28];
    for(int i=0; i<28; ++i)
        c[i]=cc[i];
    for(int i=0; i<28; ++i)
        d[i]=dd[i];
}
void ShiftPC2(int k[],int pc2[],int kk[])//K置換選擇2
{
    for(int i=0; i<48; ++i)
        kk[i]=k[pc2[i]-1];
}
void Wheetemp(int r[],int e[],int r1[])//1.輪函式置換
{
    for(int i=0; i<48; ++i)
        r1[i]=r[e[i]-1];
    cout<<"E(R):";
    for(int i=0; i<48; ++i)
        cout<<r1[i];
    puts("");
}

void SBox(int r1[],int fp[])//S盒
{
    int s[6],j=0,si=0,cp=0;
    memset(s,0,sizeof(s));
    for(int i=0; i<48; ++i)
    {
        s[j++]=r1[i];
        if((i!=0&&(i+1)%6==0)||i==47)//每6個為一組
        {
            j=0;
            int row=s[0]*2+s[5];
            int col=s[1]*8+s[2]*4+s[3]*2+s[4];
            //cout<<"ss["<<si<<"]["<<row<<"]["<<col<<"]="<<ss[si][row][col]<<endl;
            bitset<4>s(ss[si][row][col]);//宣告bitset,並指定要轉換的數p和位數4
            ++si;
            string c=s.to_string<char,char_traits<char>,allocator<char> >();//bitset轉成二進位制字串
            for(int u=0; u<c.size(); ++u)
                fp[cp++]=int(c[u])-'0';
        }
    }

}
void Wheel2(int r1[],int kcd[],int fp[])//2.輪函式置換
{
    for(int i=0; i<48; ++i)
        r1[i]^=kcd[i];//異或運算
    cout<<"^K1:";
    for(int i=0; i<48; ++i)
        cout<<r1[i];
    puts("");
    SBox(r1,fp);
    cout<<"S盒:";
    for(int i=0; i<32; ++i)
        cout<<fp[i];
    puts("");
}
void Wheel3(int fp[],int fout[],int p[])//3.輪函式置換P運算
{
    for(int i=0; i<32; ++i)
        fout[i]=fp[p[i]-1];
    cout<<"f(R[i-1],K[i]):";
    for(int i=0; i<32; ++i)
        cout<<fout[i];
    puts("");
}

void f(int r[],int e[],int r1[],int kcd[],int fp[],int p[],int fout[])//輪函式置換
{
    Wheetemp(r,e,r1);//1.輪函式置換
    Wheel2(r1,kcd,fp);//2.輪函式置換
    Wheel3(fp,fout,p);//3.輪函式置換
}
void ShiftNIP(int nin[],int nout[],int n,int nip[])//逆初始置換IP^(-1)
{
    for(int i=0; i<64; ++i)
        nout[i]=nin[nip[i]-1];
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("D:/x/read.txt","r",stdin);
    freopen("D:/x/out.txt","w",stdout);
#endif
    int in[64],out[64];//明文、初始置換IP後
    int ip[64];//初始置換IP
    int l[64/2],r[64/2];//置換輸出資料分成兩半
    int e[48];//輪函式置換E
    int k[64],kk[56],kcd[48];//原金鑰,置換選擇1後,置換選擇2後
    int pc1[56],pc2[48];//置換選擇1、2
    int c[28],d[28];//子金鑰置換選擇1後分成兩半
    int cc[28],dd[28];//迴圈左移
    int p[32],fp[32],fout[32];//置換P,S盒代換運算,置換P運算
    int nip[64];//逆初始置換IP^(-1)
    Init(in,out,ip,l,r,e,k, kk, kcd, pc1, pc2, c, d, cc, dd, p, fp, fout, nip);//陣列賦值初始化
    ShiftIP(in,out,64,ip);//初始置換IP
    SplitLR(out,l,r,64);//分割成L0和R0
    ShiftPC1(k,pc1,kk);//K置換選擇1
    SplitCD(kk,c,d);//子金鑰分割
    int temp[32],r1[48];
    /******計算函式的16輪迭代******/
    for(int i=1; i<=16; ++i)
    {
        if(i==1||i==2||i==9||i==16)//迴圈左移一個位置
            LS(1,c,d,cc,dd);//c和d陣列迴圈左移1個位置後存入cc和dd陣列
        else //迴圈左移兩個位置
            LS(2,c,d,cc,dd);//迴圈左移2個位置
        for(int j=0; j<28; ++j)
            kk[j]=cc[j];
        int u=0;
        for(int j=28; j<56; ++j)
            kk[j]=dd[u++];
        ShiftPC2(kk,pc2,kcd);//K置換選擇2
        cout<<"K["<<i<<"]:";
        for(int j=0; j<48; ++j)
            cout<<kcd[j];
        puts("");
        for(int j=0; j<32; ++j)
            temp[j]=r[j];//臨時儲存R[i-1]
        f(r,e,r1,kcd,fp,p,fout);//輪函式置換
        for(int j=0; j<32; ++j)
        {
            fout[j]^=l[j];//f(R[0],K[1])^L[0]
            r[j]=fout[j];//R[i]=f(R[0],K[1])^L[0]
        }
        for(int j=0; j<32; ++j)
            l[j]=temp[j];//L[i]=R[i-1]
        cout<<"L["<<i<<"]:";
        for(int j=0; j<32; ++j)
            cout<<l[j];
        puts("");
        cout<<"R["<<i<<"]:";
        for(int j=0; j<32; ++j)
            cout<<r[j];
        puts("");
        puts("");
    }
    /**************************/
    int nin[64],nout[64];//逆初始置換前後
    for(int i=0; i<32; ++i)
        nin[i]=r[i];
    int j=32;
    for(int i=0; i<32; ++i)
        nin[j++]=l[i];
    ShiftNIP(nin,nout,64,nip);//逆初始置換IP^(-1)
     cout<<"明文:";
    for(int i=0; i<64; ++i)
        cout<<in[i];
    puts("");
    cout<<"加密:";
    for(int i=0; i<64; ++i)
        cout<<nout[i];
    puts("");
    return 0;
}


 

輸入:

//加密明文
0 0 0 0 0 0 0 1
0 0 1 0 0 0 1 1
0 1 0 0 0 1 0 1
0 1 1 0 0 1 1 1
1 0 0 0 1 0 0 1
1 0 1 0 1 0 1 1
1 1 0 0 1 1 0 1
1 1 1 0 1 1 1 1
//初始置換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
//輪函式置換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
//K0
0 0 0 1 0 0 1 1
0 0 1 1 0 1 0 0
0 1 0 1 0 1 1 1
0 1 1 1 1 0 0 1
1 0 0 1 1 0 1 1
1 0 1 1 1 1 0 0
1 1 0 1 1 1 1 1
1 1 1 1 0 0 0 1
//置換選擇1
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
//置換選擇2
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
//置換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
//逆初始置換IP^(-1)
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
/**S盒***/
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:1100110000000000110011001111111111110000101010101111000010101010
K[1]:000110110000001011101111111111000111000001110010
E(R):011110100001010101010101011110100001010101010101
^K1:011000010001011110111010100001100110010100100111
S盒:01011100100000101011010110010111
f(R[i-1],K[i]):00100011010010101010100110111011
L[1]:11110000101010101111000010101010
R[1]:11101111010010100110010101000100

K[2]:011110011010111011011001110110111100100111100101
E(R):011101011110101001010100001100001010101000001001
^K1:000011000100010010001101111010110110001111101100
S盒:11111000110100000011101010101110
f(R[i-1],K[i]):00111100101010111000011110100011
L[2]:11101111010010100110010101000100
R[2]:11001100000000010111011100001001

K[3]:010101011111110010001010010000101100111110011001
E(R):111001011000000000000010101110101110100001010011
^K1:101100000111110010001000111110000010011111001010
S盒:00100111000100001110000101101111
f(R[i-1],K[i]):01001101000101100110111010110000
L[3]:11001100000000010111011100001001
R[3]:10100010010111000000101111110100

K[4]:011100101010110111010110110110110011010100011101
E(R):010100000100001011111000000001010111111110101001
^K1:001000101110111100101110110111100100101010110100
S盒:00100001111011011001111100111010
f(R[i-1],K[i]):10111011001000110111011101001100
L[4]:10100010010111000000101111110100
R[4]:01110111001000100000000001000101

K[5]:011111001110110000000111111010110101001110101000
E(R):101110101110100100000100000000000000001000001010
^K1:110001100000010100000011111010110101000110100010
S盒:01010000110010000011000111101011
f(R[i-1],K[i]):00101000000100111010110111000011
L[5]:01110111001000100000000001000101
R[5]:10001010010011111010011000110111

K[6]:011000111010010100111110010100000111101100101111
E(R):110001010100001001011111110100001100000110101111
^K1:101001101110011101100001100000001011101010000000
S盒:01000001111100110100110000111101
f(R[i-1],K[i]):10011110010001011100110100101100
L[6]:10001010010011111010011000110111
R[6]:11101001011001111100110101101001

K[7]:111011001000010010110111111101100001100010111100
E(R):111101010010101100001111111001011010101101010011
^K1:000110011010111110111000000100111011001111101111
S盒:00010000011101010100000010101101
f(R[i-1],K[i]):10001100000001010001110000100111
L[7]:11101001011001111100110101101001
R[7]:00000110010010101011101000010000

K[8]:111101111000101000111010110000010011101111111011
E(R):000000001100001001010101010111110100000010100000
^K1:111101110100100001101111100111100111101101011011
S盒:01101100000110000111110010101110
f(R[i-1],K[i]):00111100000011101000011011111001
L[8]:00000110010010101011101000010000
R[8]:11010101011010010100101110010000

K[9]:111000001101101111101011111011011110011110000001
E(R):011010101010101101010010101001010111110010100001
^K1:100010100111000010111001010010001001101100100000
S盒:00010001000011000101011101110111
f(R[i-1],K[i]):00100010001101100111110001101010
L[9]:11010101011010010100101110010000
R[9]:00100100011111001100011001111010

K[10]:101100011111001101000111101110100100011001001111
E(R):000100001000001111111001011000001100001111110100
^K1:101000010111000010111110110110101000010110111011
S盒:11011010000001000101001001110101
f(R[i-1],K[i]):01100010101111001001110000100010
L[10]:00100100011111001100011001111010
R[10]:10110111110101011101011110110010

K[11]:001000010101111111010011110111101101001110000110
E(R):010110101111111010101011111010101111110110100101
^K1:011110111010000101111000001101000010111000100011
S盒:01110011000001011101000100000001
f(R[i-1],K[i]):11100001000001001111101000000010
L[11]:10110111110101011101011110110010
R[11]:11000101011110000011110001111000

K[12]:011101010111000111110101100101000110011111101001
E(R):011000001010101111110000000111111000001111110001
^K1:000101011101101000000101100010111110010000011000
S盒:01111011100010110010011000110101
f(R[i-1],K[i]):11000010011010001100111111101010
L[12]:11000101011110000011110001111000
R[12]:01110101101111010001100001011000

K[13]:100101111100010111010001111110101011101001000001
E(R):001110101011110111111010100011110000001011110000
^K1:101011010111100000101011011101011011100010110001
S盒:10011010110100011000101101001111
f(R[i-1],K[i]):11011101101110110010100100100010
L[13]:01110101101111010001100001011000
R[13]:00011000110000110001010101011010

K[14]:010111110100001110110111111100101110011100111010
E(R):000011110001011000000110100010101010101011110100
^K1:010100000101010110110001011110000100110111001110
S盒:01100100011110011001101011110001
f(R[i-1],K[i]):10110111001100011000111001010101
L[14]:00011000110000110001010101011010
R[14]:11000010100011001001011000001101

K[15]:101111111001000110001101001111010011111100001010
E(R):111000000101010001011001010010101100000001011011
^K1:010111111100010111010100011101111111111101010001
S盒:10110010111010001000110100111100
f(R[i-1],K[i]):01011011100000010010011101101110
L[15]:11000010100011001001011000001101
R[15]:01000011010000100011001000110100

K[16]:110010110011110110001011000011100001011111110101
E(R):001000000110101000000100000110100100000110101000
^K1:111010110101011110001111000101000101011001011101
S盒:10100111100000110010010000101001
f(R[i-1],K[i]):11001000110000000100111110011000
L[16]:01000011010000100011001000110100
R[16]:00001010010011001101100110010101

明文:0000000100100011010001010110011110001001101010111100110111101111
加密:1000010111101000000100110101010000001111000010101011010000000101


 

相關文章