• 仿射加密与S-DES加密算法的实现


    仿射加密

    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    char letter[30];
    char _letter[30];
    char ans_letter[30];
    int num[30],s_num[100],k1,k2;const int mod=27;
    int exgcd(int a,int b,int &x,int &y){
        if(b==0){
            x=1;y=0;return a;
        }
        int d=exgcd(b,a%b,x,y),t;
        t=x;x=y;y=t-a/b*y;
        return d;
    }
    int decode(int key){
        int rev,y;
        int d=exgcd(k1,mod,rev,y);
        return ((((key-k2+mod)%mod)*rev)%mod+mod)%mod;
    }
    char text[100];
    int main(){
        for(int i=0;i<26;++i){
            letter[i]='A'+i;
        }
        letter[26]=' ';
        letter[27]='';char c;int cc=0;
        while((c=getchar())!='
    '){
            text[cc++]=c;
        }
        int len=cc;text[len]='';
        for(int i=0;i<len;++i){
            if(text[i]==' ') s_num[i]=26;
            else              s_num[i]=text[i]-'A';
        }
        while(~scanf("%d%d",&k1,&k2)){
            int x,y;
            int d=exgcd(k1,k2,x,y);
            if(d!=1) {
                printf("k1 and k2 Not coprime!
    ");
                continue;
            }
            for(int i=0;i<27;++i){
                _letter[i]=letter[num[i]=(k1*i+k2)%mod];
            }
            _letter[27]='';
            printf("AFTER LOCK::
    ");
            for(int i=0;i<len;++i){
                printf("%d,",num[s_num[i]]);
            }
            printf("
    ");
            printf("TRY SIMPLE DECODE::
    ");
            for(int i=0;i<len;++i){
                printf("%c",letter[num[s_num[i]]]);
            }
            printf("
    ");
            printf("AFT GET THE MODNUM AND DECODE::
    ");
            for(int i=0;i<len;++i){
                printf("%c",letter[decode(num[s_num[i]])]);
            }
            printf("
    ");
            printf("ith number represent the res which number i  after LOCK::
    ");
            for(int i=0;i<27;++i){
                ans_letter[i]=letter[decode(num[i])];
            }
            ans_letter[27]='';
            for(int i=0;i<27;++i) printf("%d, ",num[i]);printf("
    ");
            printf("letter:
    %s
    ",letter);
            printf("_letter:
    %s
    ",_letter);
            printf("ans_letter
    %s
    ",ans_letter);
        }
        return 0;
    }

    S-DES

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    int p8[]={6,3,7,4,8,5,10,9};
    int p10[]={3,5,2,7,4,10,1,9,8,6};
    int p4[]={2,4,3,1};
    int ep[]={4,1,2,3,2,3,4,1};
    int ip[]={2,6,3,1,4,8,5,7};
    int ip_rev[]={4,1,3,5,7,2,8,6};
    int S0[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}};
    int S1[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
    int SourceKey[11]={10};
    int K1[9]={8};
    int K2[9]={8};
    int SOURCE_STR[9]={8};
    int LOCK_STR[9]={8};
    int UNLOCKED_STR[9]={8};
    void prt(int a[],int a_len){
        for(int i=1;i<=a_len;++i) printf("%d, ",a[i]);printf("
    ");
    }
    void shift(int a[],int len,int offset){
        int b[len];
        for(int i=0;i<len;++i){
            b[i]=a[i+1];
        }
        for(int i=1;i<=len;++i){
            a[i]=b[(i-1+offset)%len];
        }
    }
    void P8(int a[],int &len){
        //抽取8位
        int b[len+1];
        for(int i=1;i<=len;++i){
            b[i]=a[i];
        }
        len=8;
        for(int i=1;i<=len;++i){
            a[i]=b[p8[i-1]];
        }
    }
    void P10(int a[],int len){
        int b[len+1];
        for(int i=1;i<=len;++i){
            b[i]=a[i];
        }
        for(int i=1;i<=len;++i){
            a[i]=b[p10[i-1]];
        }
    }
    void P4(int a[],int len){
        int b[len+1];
        for(int i=1;i<=len;++i){
            b[i]=a[i];
        }
        for(int i=1;i<=len;++i){
            a[i]=b[p4[i-1]];
        }
    }
    void IP(int a[],int len){
        if(len!=8) printf("IP::len must be 8!
    ");
        int b[len+1];
        for(int i=1;i<=len;++i){
            b[i]=a[i];
        }
        for(int i=1;i<=len;++i){
            a[i]=b[ip[i-1]];
        }
    }
    void IP_REV(int a[],int len){
        if(len!=8) printf("IP_REV:len must be 8!
    ");
        int b[len+1];
        for(int i=1;i<=len;++i){
            b[i]=a[i];
        }
        for(int i=1;i<=len;++i){
            a[i]=b[ip_rev[i-1]];
        }
    }
    void EP(int a[],int &len){
        if(len!=4) printf("EP::the len must be 4!
    ");
        int b[len+1];
        for(int i=1;i<=len;++i){
            b[i]=a[i];
        }
        len*=2;
        for(int i=1;i<=len;++i){
            a[i]=b[ep[i-1]];
        }
    }
    void SW(int a[],int a_len,int b[],int b_len){
        if(a_len!=b_len) printf("SW::the a_len must be equal to b_len!
    ");
        int c[a_len+1];
        for(int i=1;i<=a_len;++i){
            c[i]=a[i];
        }
        for(int i=1;i<=a_len;++i){
            a[i]=b[i];
        }
        for(int i=1;i<=a_len;++i){
            b[i]=c[i];
        }
    }
    void convert(int a[],int offset,int key){
        //从小到大,从高位到低位
        if(key==0){
            a[offset]=0;a[offset+1]=0;
        }
        else if(key==1){
            a[offset]=0;a[offset+1]=1;
        }
        else if(key==2){
            a[offset]=1;a[offset+1]=0;
        }
        else if(key==3){
            a[offset]=1;a[offset+1]=1;
        }
    }
    void fk(int ming[],int m_len,int K[],int left[],int right[]){
        int front[9]={4};
        for(int i=1;i<=front[0];++i){
            front[i]=ming[i];
            left[i]=ming[i];
        }
        int back[9]={4};
        int __EP[9]={4};
        for(int i=1;i<=back[0];++i){
            back[i]=ming[i+4];
            __EP[i]=ming[i+4];
            right[i]=ming[i+4];
        }
        EP(__EP,__EP[0]);
        //printf("__EP:: ");prt(__EP,__EP[0]);
        //yihuo
        if(K[0]!=__EP[0]) printf("fk::len err!
    ");
        for(int i=1;i<=__EP[0];++i){
            __EP[i]=__EP[i]^K[i];
        }
        //printf("AFT XOR __EP:: ");prt(__EP,__EP[0]);
        int __EP1[9]={4},__EP2[9]={4};
        for(int i=1;i<=4;++i){
            __EP1[i]=__EP[i];
        }
        for(int i=1;i<=4;++i){
            __EP2[i]=__EP[i+4];
        }
        //printf("__EP1:: ");prt(__EP1,__EP1[0]);
        //printf("__EP2:: ");prt(__EP2,__EP2[0]);
        int L=S0[(__EP1[1]*2+__EP1[4]*1)][(__EP1[2]*2+__EP1[3]*1)];
        int R=S1[(__EP2[1]*2+__EP2[4]*1)][(__EP2[2]*2+__EP2[3]*1)];
        //printf("L:%d R:%d
    ",L,R);
        int cc=0;int _P4[5]={4};
        convert(_P4,1,L);convert(_P4,3,R);
        //printf("_P4 AFT UNION:: ");prt(_P4,_P4[0]);
        P4(_P4,_P4[0]);
        //printf("_P4 AFT P4:: ");prt(_P4,_P4[0]);
        for(int i=1;i<=4;++i){
            front[i]=front[i]^_P4[i];
            left[i]=front[i];
        }
        //printf("left:: ");prt(left,left[0]);
        //printf("right:: ");prt(right,right[0]);
    }
    
    void init_K1_and_K2(int SK[],int K1[],int K2[]){
        //sourcekey
        int _SK[11]={10};
        for(int i=1;i<=10;++i) _SK[i]=SK[i];
        P10(_SK,_SK[0]);//forgot
        int SKL[6]={5},SKR[6]={5};
        for(int i=1;i<=5;++i){
            SKL[i]=_SK[i];
        }
        for(int i=1;i<=5;++i){
            SKR[i]=_SK[i+5];
        }
        shift(SKL,SKL[0],1);shift(SKR,SKR[0],1);
        //prt(SKL,SKL[0]);prt(SKR,SKR[0]);
        int t_SK[11]={10};
        for(int i=1;i<=5;++i) t_SK[i]=SKL[i];
        for(int i=6;i<=10;++i) t_SK[i]=SKR[i-5];
        //prt(t_SK,t_SK[0]);
        P8(t_SK,t_SK[0]);
        //prt(t_SK,t_SK[0]);
        for(int i=1;i<=8;++i) K1[i]=t_SK[i];
        t_SK[0]=10;
        shift(SKL,SKL[0],2);shift(SKR,SKR[0],2);
        //prt(SKL,SKL[0]);prt(SKR,SKR[0]);
        for(int i=1;i<=5;++i) t_SK[i]=SKL[i];
        for(int i=6;i<=10;++i) t_SK[i]=SKR[i-5];
        //prt(t_SK,t_SK[0]);
        P8(t_SK,t_SK[0]);
        //prt(t_SK,t_SK[0]);
        for(int i=1;i<=8;++i) K2[i]=t_SK[i];
    }
    void LOCK(int S_STR[],int SK[],int K1[],int K2[]){
        init_K1_and_K2(SK,K1,K2);
        int T_STR[9]={8};
        for(int i=1;i<=8;++i) T_STR[i]=S_STR[i];
        IP(T_STR,T_STR[0]);
        int left[5]={4},right[5]={4};
        fk(T_STR,T_STR[0],K1,left,right);
        SW(left,left[0],right,right[0]);
        fk(T_STR,T_STR[0],K2,left,right);
        int C_STR[9]={8};
        for(int i=1;i<=4;++i) C_STR[i]=left[i];
        for(int i=5;i<=8;++i) C_STR[i]=right[i-4];
        IP_REV(C_STR,C_STR[0]);
        for(int i=1;i<=8;++i) LOCK_STR[i]=C_STR[i];
    }
    void UNLOCK(int S_STR[],int SK[],int K1[],int K2[]){
        init_K1_and_K2(SK,K1,K2);
        int T_STR[9]={8};
        for(int i=1;i<=8;++i) T_STR[i]=S_STR[i];
        IP(T_STR,T_STR[0]);
        int left[5]={4},right[5]={4};
        fk(T_STR,T_STR[0],K2,left,right);//K2
        SW(left,left[0],right,right[0]);
        fk(T_STR,T_STR[0],K1,left,right);//K1
        int C_STR[9]={8};
        for(int i=1;i<=4;++i) C_STR[i]=left[i];
        for(int i=5;i<=8;++i) C_STR[i]=right[i-4];
        IP_REV(C_STR,C_STR[0]);
        for(int i=1;i<=8;++i) UNLOCKED_STR[i]=C_STR[i];
    }
    int main(){
        //testShift
        // int a[]={0,1,2,3,4};
        // shift(a,4,1);
        // for(int i=1;i<=4;++i) printf("%d, ",a[i]);printf("
    ");
        //test p8
        // int a[]={10,0,1,2,3,4,5,6,7,8,9};
        // P8(a,a[0]);
        // for(int i=1;i<=a[0];++i) printf("%d, ",a[i]);printf("
    ");
        //test SW
        // int a[]={4,1,2,3,4};int b[]={4,4,3,2,1};
        // int a_len=4,b_len=4;
        // SW(a,a_len,b,b_len);
        // prt(a,a_len);prt(b,b_len);
        //test EP 
        // int a[9]={4,1,2,3,4};
        // EP(a,a[0]);
        // prt(a,a[0]);
        int i;char s[11];s[0]='#';
        printf("input Source_Key:
    ");
        scanf("%s",s+1);
        for(i=1;i<=10;++i) SourceKey[i]=s[i]-'0';
        init_K1_and_K2(SourceKey,K1,K2);
        prt(K1,K1[0]);prt(K2,K2[0]);
        printf("input Mod:
    ");
        scanf("%s",s+1);
        for(i=1;i<=8;++i) SOURCE_STR[i]=s[i]-'0';
        // int left[5]={4},right[5]={4};
        // fk(SOURCE_STR,8,K1,left,right);
        // prt(left,left[0]);prt(right,right[0]);
        LOCK(SOURCE_STR,SourceKey,K1,K2);
        printf("LOCK_STR:: ");prt(LOCK_STR,LOCK_STR[0]);
        UNLOCK(LOCK_STR,SourceKey,K1,K2);
        printf("UNLOCKED_STR:: ");prt(UNLOCKED_STR,UNLOCKED_STR[0]);
        return 0;
    }
  • 相关阅读:
    ThinkPHP---框架介绍
    (独孤九剑)--cURL
    浅谈Session与Cookie的区别与联系
    (独孤九剑)--会话管理和控制
    PHP数据乱码
    (独孤九剑)---PHP操作MySQL数据库
    错误宝典
    变量、常量、注释、PEP8开发规范、数据类型、Python2和Python3的区别、用户输入、流程控制语句
    什么是编程语言
    CSS
  • 原文地址:https://www.cnblogs.com/linkzijun/p/6796756.html
Copyright © 2020-2023  润新知