置换、代换、RC4算法代码

#include<bits/stdc++.h>
using namespace std;
class shift_cipher {
public:
    int k;
    shift_cipher(const int k) {
        this->k=k;
    }
    int encrypt(int x) {
        return x+k;
    }
    int decrypy(int y) {
        return y-k; 
    }
};
class transposition_cipher {    
public:
    /**
     * @data: 需要加密的内容
     * @key: 密钥
     */
    transposition_cipher() {}
    string encrypt(string& data, vector<int>& key) {
        int len=data.size(), m=key.size(), n=len/m;
        if (len%m) n++;
        char g[n][m]; //将data存到二维数组g中
        for (int i=0; i<n; i++)
        for (int j=0; j<m; j++) {
            int idx=i*m+j;
            cout<<data[idx];
            if (idx<len) g[i][j]=data[idx];
            else g[i][j]='/';
        }
        string ans;
        for (int j=0; j<m; j++) {
            int col_idx=key[j];
            for (int i=0; i<n; i++)
                ans+=g[i][col_idx];
        }
        return ans;
    }
    /**
     * @data: 需要加密的内容
     * @key: 密钥
     */
    string decrypy(string& data, vector<int>& key) {
        int len=data.size(), m=key.size(), n=len/m;
        if (len%m) n++;
        char g[n][m]; //将data存到二维数组g中
        for (int i=0; i<n; i++)
        for (int j=0; j<m; j++) {
            int idx=i*m+j;
            if (idx<len) g[i][j]=data[idx];
            else g[i][j]='/';
        }
        //按照密钥的格式取出矩阵g的内容
        string ans;
        for (int j=0; j<m; j++) {
            int col_idx=key[j];
            for (int i=0; i<n; i++)
                ans+=g[i][col_idx];
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};
 
class RC4 {
public:
    static const int N=256;
    RC4() {}
    void init_S(vector<int>& S, vector<int>& K, vector<int>& key){
        int n=key.size();
        for (int i=0; i<N; i++) {
            S.push_back(i);
            K.push_back(key[i%n]);
        }
    }
    void step2(vector<int>& S, vector<int>& K) {
        for (int i=0,j=0; i<N; i++) {
            j=(j+S[i]+K[i])%N;
            swap(S[i], S[j]);
        }
    }
    string encrypt(string data, vector<int>& S, vector<int>& K, vector<int>& key) {
        int n=data.size(),i=0,j=0;
        string ans;
        for (int p=0; p<n; p++) {
            i=(i+1)%256;
            j=(j+S[i])%256;
            swap(S[i], S[j]);
            int t=(S[i]+S[j])%N, k=S[t];
            ans+=((char)(((int)data[p])^k));
        }
        return ans;
    }
};
int main() {
//     int k; scanf("%d", &k);
//     shift_cipher sc(k);
//     int x; scanf("%d", &x);
//     int y=sc.encrypt(x);
//     printf("明文%d加密后的密文是:%d
", x,y);
//     x=sc.decrypy(y);
//     printf("密文%d解密后的明文是:%d
", y,x);
//     printf("
");
 
//     printf("=====================================================
");
 
//     transposition_cipher* tc = new transposition_cipher();
//     vector<int> key={2,0,1};
//     string data; cin>>data;
//     string miwen=tc->encrypt(data, key);
//     printf("加密后的密文:%s
", miwen.c_str());
//     printf("解密后的明文:%s", tc->decrypy(miwen, key).c_str());
 
//     printf("=====================================================
");
    RC4* rc4 = new RC4();
    vector<int> S, K, key={1,2,3,4,5,6};
    rc4->init_S(S,K,key);
    rc4->step2(S,K);
    string data="123456";
    auto ans=rc4->encrypt(data, S, K, key);
    printf("%s的密文是:%s", data.c_str(), ans.c_str());
    return 0;
} 

原文地址:https://www.cnblogs.com/wdt1/p/14199910.html