网络安全课程用C++实现简单的替代密码和置换密码

网络安全替代密码和置换密码
代码写的有点乱且没有注释,开销也大,整体感觉并不理想.
2020.10.25(发)
替代密码
替代密码算法的原理是使用替代法进行加密,就是将明文中的字符用其它字符替
代后形成密文。例如:明文字母 a,b,c,d ,用 D,E,F,G 做对应替换后形成密文。
替代密码包括多种类型,如单表替代密码,多明码替代密码,多字母替代密码,多
表替代密码等。下面我们介绍一种典型的单表替代密码,恺撒(caesar)密码,又叫循环
移位密码。它的加密方法,就是将明文中的每个字母用此字符在字母表中后面第 k 个字
母替代。它的加密过程可以表示为下面的函数:
E(m)=(m+k) mod n
其中:m 为明文字母在字母表中的位置数;n 为字母表中的字母个数;k 为密钥;E(m)
为密文字母在字母表中对应的位置数。
例如,对于明文字母 H,其在字母表中的位置数为 8,设 k=4,则按照上式计算出
来的密文为 L:
E(8) = (m+k) mod n = (8+4) mod 26 = 12 = L
代码:

 #include<iostream>
using namespace std;
class Encryption {
public:
    void set_encryption(string text1=nullptr) {
        unencrypted_text = text1;
    }
    string get_encryption(int k) {
        for (char ch : unencrypted_text) {//加密
            if (ch >= 'A' && ch <= 'Z') {
                ch = ((int)ch - 65 + k) % 26;
                ch = ch + 'A';
                cout << ch;
                encrypted_text += ch;
            }
        }
        cout << endl;
        return encrypted_text;
    }
private:
    string unencrypted_text;
    string encrypted_text;
};
class Decode {
public:
    void set_decode(string text2 = nullptr) {
        undecrypted_text = text2;
    }
    string get_decode(int k) {
        for (char ch : undecrypted_text) {//解密
            if (ch >= 'A' && ch <= 'Z') {
                ch = ((int)ch - 65 - k) % 26;
                if (ch < 0) ch += 25;
                ch = ch + 'A';
                cout << ch;
                decrypted_text += ch;
            }
        }
        cout << endl;
        return decrypted_text;
    }
private:
    string undecrypted_text;
    string decrypted_text;
};
int main() {
    Encryption A;
    int flag = 0;
    cout << "功能选择\n1 加密明文\n2 解密密文\n0 退出" << endl;
    cin >> flag;
    while (flag != 0) {
        if (flag == 1) {
            string text3;
            cout << "输入明文" << endl;
            cin >> text3;
            A.set_encryption(text3);
            int k;
            cout << "输入加密密钥" << endl;
            cin >> k;
            cout << "加密后的密文" << endl;
            string text4 = A.get_encryption(k);
        }
        else if (flag == 2) {
            Decode B;
            string text5;
            cout << "输入密文" << endl;
            cin >> text5;
            B.set_decode(text5);
            cout << "输入解密密钥" << endl;
            int k;
            cin >> k;
            cout << "解密后的密文" << endl;
            B.get_decode(k);
        }
        cout << endl;
        cout << "功能选择\n1 加密明文\n2 解密密文\n0 退出" << endl;
        cin >> flag;
    }
    system("pause");
    return 0;
}

在这里插入图片描述

替换密码:
置换密码算法的原理是不改变明文字符,只将字符在明文中的排列顺序改变,从而
实现明文信息的加密。置换密码有时又称为换位密码。

替换密码快速理解
加密好理解,解密算法首先就是将密文转换成加密后的哈希表,然后在按顺序输出.
复杂度较高,开销也大.

代码:

#include<iostream>
#include<stack>
#include<vector>
#include <algorithm>
#include <functional>
#include<unordered_map>
#include<string>
using namespace std;
class Encryption {
public:
    void setKey(string key) {
        secret_key = key;
   }
    void set_encryption(string text) {
        unencrypted_text = text;
    }
    void get_encryption() {
        unordered_map<char, string> text_map;
        int n = secret_key.size();
        int i = 0;
        for (char ch : secret_key) {
            text_map[ch];
        }
        for (char ch : unencrypted_text) {
            if (ch == ' ') continue;
            i = i % n;
            text_map[secret_key[i]] += ch;
            i++;
        }
        sort(secret_key.begin(), secret_key.end());
        for (char ch : secret_key) {
            cout << text_map[ch];
        }
        cout << endl;
    }
private:
    string unencrypted_text;
    string encrypted_text;
    string secret_key;
};
class Decode {
public:
    void set_decode(string text) {
        undecrypted_text = text;
    }
    void setKey(string key) {
        secret_key = key;
    }
    void get_decode() {
        unordered_map<char, string> text_map;
        string key1 = secret_key;
        sort(key1.begin(), key1.end());
        int row = undecrypted_text.size() / secret_key.size();
        int n = 0;
        int i = 0;
        for (char ch : undecrypted_text) {
            text_map[key1[i]] += ch;
            n++;
            if (n == row ) {
                i++;
                n = 0;
            }
        }
        for (i = 0; i < row ; i++) {
            for (int k = 0; k <(int)secret_key.size(); k++) {
                cout << text_map[secret_key[k]].at(i);
            }
            cout << endl;
        }
    }
private:
    string undecrypted_text;
    string decrypted_text;
    string secret_key;
};
int main() {
    int flag = 0;
    cout << "功能选择\n1 加密明文\n2 解密密文\n0 退出" << endl;
    cin >> flag;
    while (flag != 0) {
        if (flag == 1) {
            string text1;
            string text2;
            Encryption A;
            cout << "输入明文" << endl;
            cin >> text1;
            A.set_encryption(text1);
            cout << "输入密钥" << endl;
            cin >> text2;
            A.setKey(text2);
            cout << "加密后的密文为: " << endl;
            A.get_encryption();
        }
        else{
            string text3;
            string text4;
            Decode B;
            cout << "输入密文" << endl;
            cin >> text3;
            B.set_decode(text3);
            cout << "输入密钥" << endl;
            cin >> text4;
            B.setKey(text4);
            cout << "解密后的明文为: " << endl;
            B.get_decode();
        }
        cout << "功能选择\n1 加密明文\n2 解密密文\n0 退出" << endl;
        cin >> flag;
    }
    system("pause");
    return 0;
}

在这里插入图片描述

  • 8
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值