Offer

S-DES算法实现(C++版本)

  密码学实验二:

  

/**
 
                                                                       :;LaEaHKEEGpPXU7;,
                                                                  .:75pKH11252U252XapZgRQgD6XJscLr;,.
                                                               :LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
                                                           .r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r                                       .
                                                        ,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXULr7rrirrJXRBp;                                   ;B
                                                     ,J6MRZH6EgEEZE6E6EZZPZXXwSSGQXr::aPpP5USUHaHaKa5Lvrr7ri;rLHBB2:                                Kc
                                                   rpQDOpPPOGGZOGOZG6GEOEOEDPPGBa.  .PaSSUXSUUUaUSaKXKS177r7rrrirSBBR7                             .O,
                                                :UBQOKPK6ZOOOEDEO6GZE6EpEpDgDBR:   UBpXHa5aSaUS5SUS5XapPHJc7rrv7rr7sgBBs                           .g.
                                              ;gBMPXpO6GEOEOEOEGEOEE6EZEEDRGBB    EB5pKSXpHKaHSX552S5aUHHEX17c7vr7777s5RBS:                        .R;
                                           .sQBPXpDZOODOgODGOGgEDEOEOGgGgOOMB:   LBKKSXSHa52aaKXHXKaa5aSaaHXSJLcL7vcc777JDBBg2;.                    Qi
                                         ;2ggp2EDDOggGEDGgDDOgGGZDOOZOGg6gEBX    vBZaHUKaaUXXXSXXKXpXHXH5wwaa52U1wssLsLJccv1UDQBQ67.                O7
                                       :ZZUU5PROOEOZOZGGOODZOGgODZOOgOggRgRB; ;:..R6XaKKpP6PGppKPHpHpPX5aU21UUa5Sw52UwUJJv77L77sSpQMDU;            ;B5
                                     ,SRJ7sSHGggEOZOEG6OODpOZgggOQQBBBBBQBBQ.,;;. LBOgOgDRDDZODMgQRgRDaa552a252UUa25w5UaU2sLvccs7r7sJZBBMr       ,XQJ:
                                    LQHr77J6RGOZDZOEGEDGgDRORQBBBQRDPU1Jscwa7.,::.:J7r;::::.  ..,:;i7UOgRRRgDPH5SUSU52U2HHa1JJJLJLccLr71RBB,    7R2,
                                  :RZv;77JSgGOZEODEDGOEggQBBBMS7;:,:,,.,.,.:7L;:,;.:  ,: . .....       .:;rJU6GgGRggEZHPaKXX2S221Js1Lc7r:7QB. .XX:
                                 7g1;;7rcXG6gpGDZGgZOOQBBQpr.   ::::;:;:::Jr::sr;::;:.:vs,:::::::::::,,         ,:7L5HGOggRgZUUU5wSUaJLc7r7BOiDr  ...
                               .XX;;;irLHGKpZZZEKgDRBBB6i    ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,.         .,;7ZRQgO6KUUJsLwsJ7KBM. .....
                               JZ:;rrrc5EPHp6XgpRBBBE;     :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,,            .;sORQRGX21wsXU:  .... .
                              .Br;iir72EHPHZ6EgBBR7.   ..;ii:;;;;;;;;;:71r::.:7,  .::7;:;H;.::,:j::::::,:::::,,,::,,,              ,7wEDRZBMr  .......
                              1D;:r;rwOPXPKH6BBX,   .:;:;;;:::;:;;;;;::Ls,;ss..r.  ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::.                  .:.rP:.......
                              D2:i;rJpKHXHXgg7    .,,::::;:::::;:;:;;:;SL7sS2, :.   ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:,                    ;L:. .. ...
                             :Qc;i7LGZPa6gBM,  ...,,::::::::::::;;::;.JJ;ic:         ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,..                :2wr.  .......
                             sRr:rrwZGgBQR7.  .,,::::::::::::::;::;:::Hr:7i          ,;;:::U: .,,,:r.,,:,:,:,,,:,,,.....             7K2:. ..........
                             OX:irsXgQZ:.   .,,:::,,:::,::::::::;;;::r5;r7:           :;;;:7L ..,.,;:,:,:,,,:,:,,,,......        .rU6w;..............
                            .BJ71EK5;.   .,,::,:,:,,,:,:,::::::::;:;.Ls;r7             :;:::s, ..,,,r:.:.,,,.:, .,..... .    .;s5XJr,..,.............
                            1Mv::.     .:,:,,,,,,,:::::::,;:::;:;::..J7;c:          ,. ,rri:27  , .,:;. ,:,:,,:..,....    .rpPL;:.. ... .............
                       ..7Ls:        .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr,  .,:;;;::::..:7r;r1    .: :E:..,,:,...,,.., ..XBQ7, ................... ..
                  ,;7v7r7:,         ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:..        ,:::L:  .  .7RJ .,.:.,......,,:MBs   ..............,........
            .:;vJs7i,              ,,,., ..,,:,,,:,,,:,:.,rs,,.:,,  ;J;c,                .,::;;     Lr.E: .,...,....:,:1Z:  .........,.,...........:,.
   .;,,:r7J1wv;,.               ....,.:. .,.,,:,,,:,,::::,sS;.:::,  cLrr.     .       .,. .,..:    ;;  r5 ........,,:;s7. ....,.,.,...........,.,.,,.
   ,BBs:::                   . ........,.,,,,,.:,,,:,:,,.;s7r,,,,,  cc7;         ,.,,.      .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
     rZL.                   ....... ..,,,.,,,..,,,.,;,...7J:s: ..  .wr7,        ...    .rJpQBBBBBBBQgKP77s  ..  .,7S2,,....,....,.,.,......,,.,. .:cX2
       ;SH7,               . . . .. .,.,.,.,,,...,..r, ::Ur;7L .   .57;.           .rPBBQBBBPws:;r::::.,:P.    .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
          r7r;:             . ... ..........,...,.. 7L;,rS;;ivr .   1r:       .  .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
             .:7L7:,            . ,r,..,.,,,...,..:iLL  7s;r;cv:    U7.         :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
                .:rvw1JsL7;,      ,;..........,.:ir :r .ELr77v:,.   Pr          ..      .  Ls    ,w.   rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
                      .:;7JRQpX:   ; .........,i:  .,J ;gri;7r  :   1;                     .X:    .   ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
                           ;J,XQB7,:    . ...,:   .:7Z.7Zi7;r:   ,  v,                      :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
                           J7  rEBBg..,. ..... .  ,L1PLKr:,:;r;;::, :,                       :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
                           JL .:sXBB:... ., .    ,OH777;,rZRBBBQL ..r,                     ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL    UK2UUS1aw2wU25J
                           U7 .r71BB;  . :.. .. :QJ;.;1pBBBBBGRBRi  i:                   .      .w7.   ..,,:,,,,,:,:,:::::::,.,2BR:   .DSaUSUUS525w5U1
                           2r :7rJBQ7   .;. .   KL::JGBBBgE6Hp6XMQ;     .                    ... ,2s    ,,,,:::::,:::::::,,.:sQQB2    ;DHa5U52S2U25USJ
                           a: :7;1BB2   .v,    sL;LPQgDBPH6KPQBpGBG    .                   ... ..  aP:. .. ,::,:::::,,,,.;rSgBQDa;    :gSSwSUUU525US2U
                           5; ;;r2QBB,  .s:  :a7:PBZ2,:BEaZPKgBZOgB,                        .   ...:LEHri::,:.,,.,,,::rsXRBgGKEJi,    2GH252SUS5S11wH5
                           U: ;:rwDBBr  .c, rB6r 167.,,RQPpEP6OpKEBc      .,                 .   : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; ..  ;RZUPSSU525USSpK2
                           s; ;::aBg,   .; .Gv,H::,,::,;BQDOGHPXpKBs      .,.                 . .,.  .r:;:6gOEBQRRRDggQgOHgMGL,    .;PEHHXaaKaPXPKPaXS
                           s7.:,sBa     :..S,  vE::::;  7BBMgZH6pQBr             .             ,:,   ,.,r2RHSSpMZPKRRZpgggav,    ;UpEZaaaKHHUHPPaUJHGO
                           a;.775:    .::rU.    gR:,:.   :;:BBBBQar                         .  ,,   ,.,7rggJwU6DDGMgOGgXc:,   ,rPGOpKSXSXSKUKHU1UHgOEP
                          1i :SZJrLXpBBMRB:  .v  Ba,.       :sv:                             .        irsBUSpEEGPPpg65r:..,;sKZDZHSX5XaHU55K2wUGMRZ6Z6
                         :K  :7asvwc2MgEQB, :gg.:iB7  .,,,:.                      ...,.   .     ..   .:iM6GEpSSXZOEs:...;spZpKKXXSX5Xa5Ja5wwKGQDEpp6OZ
                         w; ,LXLr77sLwL5aR. gRgQgaQBi ,vJwvi,.                 .;;:;7:   .   .  ,...  rRDRJUSXpgas:,.:;U66SaHa2X5aUa552X12SgQgpZpEpGOD
                         H: ;XJ;77rv7rJUaQ;rgaPgXXHBB;        . .              .ri:;;          ,: . :6QZEKHXpXXL:,::r1PKXaa2XSS5a5S5UJ22aOBgEpOppZOOgG
                         iwsJsr7rsLrrLcJ2gQGXHwa1SUHBB:  . ... .                 ..,           :   1BRKpOEGp2Jr;::rsXP1Uaa2U5a5aUa5SsSpgQRZGZEEZZOOgDg
                           ,cs5aXaP552ssLwRSHXaSS5asXBB.      . .                             .  LBBgGgZHsr;vrr;rcXpP5UUS5X5S2XS5SS1URBggpEZgDDGOGggRD
                              ,;irrs2KgQRPJJJSUXKXSHJpBM   . . .                                ZBBOUsr;:.  ,,,rHgZPUaSaUSUSSXaUPXsSBB6ZZp6EEGOOGDEggO
                                       ;BBQQZaSJ5J15SJDQ7                                    .iEBL:.  . .,:,:;SRQGZXPXXSHaS2UUSUSswQB6OZgG6pZZGODEOGDE
                                      JgUri1aGEpEpXSS5LBQ,                              .rLap2Jr     ;712OO6ZggRDZXHXpKK2211LLLc7wgBa2PODRGE6OZgDgGDGD
                                    7QB; ,Jc76DaZXOZgDPEBBX,                       :c1HEZ1c;:,.,:;cHDgQQggMZgGOZO6OEGpXsLLsJXHPOBBBc;vss5EMggZDODOgED6
                                   LZJ::iwrrr72EPgXU5OBBBBBBBp7:               .i5P6wL;,   .:LS6DMgMgZKgEOPOGGDDGEXULvLSOBBBBBQBgDQDJ5rrr7JOQQDDZGEgDD
                                   wp rR5,       .HQRX7r72RQBBBBBQBMEK6Uc7sc7cHOU:,     .:LHgOQg6ZOPZpZGGEDERZPwcr7LKDDaJr:.      rBX;;:r:ir1DBQMgRgDp
                                   7D,sw:      ,   Z.      .LgBBQBBOsJaQBBBBBBXrr.  .:rwZBBQgROgDgDOERRMgROOScrLsPP5r,             7Qs,::::::rUgRgZOG6
                                   ,Q57:r  ,:  ,r  U:         .;;   .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:.                  iQK...:,:::;LLJJws
                                    OBHs;.  :;;,v  H:             :6X;       rpL;7pSrrr7r;::,....,,:;iirrvvvr,                       :BBPs;;:,,,,::;::
                                    ::rPDEL:..7:c;7r              pK  .:      :KL.:r:..     ..:icsS5sr:,.                              JgGgBQDOSJss77r
                                         :r;:::  ,                PH r,       ,;5: ,::::;7Ls7Lc7;:                                         ,:7JP17rJUs
                                                                  .:J: :;  .,:K6BQS7:.,.,.
                                                                    :r7Ji;r7;::;;.
                                                                        .                                                                             **/
                                                                        
#include<bits/stdc++.h>
using namespace std;

#define FFI(a, b) for(int i = a; i < b; i++)
#define FFJ(a, b) for(int j = a; j < b; j++)
#define FFK(a, b) for(int k = a; k < b; k++)
#define RR(a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define SCC(a, b) scanf("%d%d", &a, &b)
#define SCCC(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define PR(x) printf("%d\n", x)
#define PRR(a, b) printf("%d%d\n", a, b);
#define INF 0x3f3f3f3f
#define MAX 50
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
/*******************************************************************************************************************************************************/
int k1 = 0;int  k2 = 0;
static int P10MAX = 10;
static int P8MAX = 10;
static int P4MAX = 4;
static int IPMAX = 8;
static int IPIMAX = 8;
static int EPMAX = 4;
static int P10[] = {3, 5, 2, 7, 4, 10, 1, 9, 8, 6, '\0'};
static int P8[] = {6, 3, 7, 4, 8, 5, 10, 9, '\0'};
static int P4[] = {2, 4, 3, 1, '\0'};
static int IP[] = {2, 6, 3, 1, 4, 8, 5, 7, '\0'};
static int IPI[] = {4, 1, 3, 5, 7, 2, 8, 6, '\0'};
static int EP[] = {4, 1, 2, 3, 2, 3, 4, 1, '\0'};
static int S0[4][4] = {
    {1, 0, 3, 2},
    {3, 2, 1, 0},
    {0, 2, 1, 3},
    {3, 1, 3, 2},
};
static int S1[4][4] = {
    {0, 1, 2, 3},
    {2, 0, 1, 3},
    {3, 0, 1, 0},
    {2, 1, 0, 3},
};
//将二进制变成数字 
int BinaryToDecimal(string binary){
    int result = 0;
    FFI(0, binary.length())result = 2 * result + (binary[i] - '0');
    return result;
}
//数组的长度 
int Getlength(int p[]){
    int i = 0;
    for(; p[i] != '\0'; i++);
    return i;
}
//置换函数 
int Permute(int inum, int p[], int pmax){
    int result = 0, length = Getlength(p);
    FFI(0, length){
        result <<= 1;
        result |= (inum >> (pmax - p[i])) & 1;
    }
    return result;
}
void SDES(int k){
    int t1 ,t2;
    t1 = t2 = 0;
    k = Permute(k, P10, P10MAX);
    //保留最低5位
    t1 = (k >> 5) & 0x1f; 
    t2 = k & 0x1f;
    t1 = ((t1 & 0xf) << 1) | ((t1 & 0x10) >> 4);
    t2 = ((t2 & 0xf) << 1) | ((t2 & 0x10) >> 4);
    k1 = Permute((t1 << 5) | t2, P8, P8MAX);
    
    t1 = ((t1 & 0x07) << 2) | ((t1 & 0x18) >> 3);
    t2 = ((t2 & 0x07) << 2) | ((t2 & 0x18) >> 3);
    k2 = Permute((t1 << 5) | t2, P8, P8MAX);
    cout<<"K1: "<<endl;
    cout<<"K2: "<<endl; 
}
//fk里面的F函数
int F(int R, int K){
    int t = Permute(R, EP, EPMAX) ^ K;
    int t0 = (t >> 4) & 0xf;
    int t1 = t & 0xf;
    t0 = S0[((t0 & 0x8) >> 2) | (t0 & 1)][(t0 >> 1 & 0x3)];
    t1 = S1[((t1 & 0x8) >> 2) | (t1 & 1)][(t1 >> 1 & 0x3)];
    t = Permute((t0 << 2) | t1, P4, P4MAX);
    return t;
}
 
//最低4位 
int fk(int m, int k){
    int l = (m >> 4) & 0xf;
    int r = m & 0xf;
    return ((l ^ F(r, k)) << 4) | r;
}
int SW(int x){
    return ((x & 0xf) << 4) | ((x >> 4) & 0xf);
}
//加密 
int encrypt(int m){
    m = Permute(m, IP, IPMAX);
    m = fk(m, k1);
    m = SW(m);
    m = fk(m, k2);
    m = Permute(m, IPI, IPIMAX);
    return m;
}
//解密 
int decrypt(int m){
    m = Permute(m, IP, IPMAX);
    m = fk(m, k2);
    m = SW(m);
    m = fk(m, k1);
    m = Permute(m, IPI, IPIMAX);
    return m;
}

void printBin(int x, int n){
    int mask = 1 << (n - 1);
    while(mask > 0){
        ((x & mask) == 0) ? printf("0") : printf("1");
        mask >>= 1;
    }
    cout<<endl;
}
int main(void){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int index;string plaintext, ciphertext, key;
    printf("**************************************************************************\n");
    printf("*****************!!!欢迎使用S-DES加载助手!!!************************\n");
    printf("*****************作者:Empirefree                    *********************\n");
    printf("*****************工具: DEV-C++                       *********************\n");
    printf("*****************时间: 2018/10/29 9:08               *********************\n");
    printf("**************************************************************************\n");
    cout<<"***************    输入均为二进制    ***************"<<endl;
    loop: cout<<"***************请选择:加密为1 解密为0***************"<<endl;
    cin>>index;
    if(!index){
        cout<<"***************解密:***************"<<endl;
         cout<<"***************请输入密文:***************"<<endl;
        cin>>plaintext;
        cout<<"***************请输入密钥:***************"<<endl;
         cin>>key;
         SDES(BinaryToDecimal(key));
         cout<<"***************得到明文:***************"<<endl;
         printBin(encrypt(BinaryToDecimal(plaintext)), 8);
        cout<<"***************是否跳出 跳出为0 继续为1***************"<<endl;
        cin>>index;
        switch(index){
             case 0:{
                cout<<"***************欢迎使用s-des程序***************"<<endl;
                cout<<"***************hyq ***************";
                return 0;
            }
            case 1: goto loop;
        }
    }
    else{
        cout<<"***************加密:***************"<<endl;
        cout<<"***************请输入明文:***************"<<endl;
        cin>>ciphertext;
        cout<<"***************请输入密钥:***************"<<endl;
        cin>>key;
        SDES(BinaryToDecimal(key));
        cout<<"***************得到密文:***************"<<endl;
        printBin(decrypt(BinaryToDecimal(ciphertext)),8);
        cout<<"***************是否跳出 跳出为0 继续为1***************"<<endl;
        cin>>index;
        switch(index){
             case 0:{
                cout<<"***************欢迎使用s-des程序***************"<<endl;
                cout<<"***************hyq ***************";
                return 0;
            }
            case 1: goto loop;
        }
    }
    return EXIT_SUCCESS;
}

 

posted @ 2018-10-29 09:22  Empirefree  阅读(685)  评论(0编辑  收藏  举报