4种古典密码算法的设计与实现——C++

4种古典密码算法的设计与实现——C++

本文将介绍四种古典密码算法并进行代码实现:

  1. 使用 C++ 实现4种古典密码算法
  2. 明文:I am learning coding

4种古典密码

1.仿射密码

加密算法:c = a*m + b(mod n)
加密过程:

  1. 获取a,b,n;(若未知)
  2. 获取明文字符串;
  3. 将每一个明文字符转换成对应的数字;
  4. 将明文数字带入公式c = a*m + b(mod n),获取密文对应数字;
  5. 将密文数字转换成对应的密文字符。

代码

Affine password.h

#ifndef Affine_password_h
#define Affine_password_h

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int affine(string Inform)
{
    int n = 36;
    string c;
    int buff[32], len = 0, key1 = 0, key2 = 0;
    cout << "输入key1:";
    cin >> key1;
    cout << "输入key2:";
    cin >> key2;
    //把明文转化成10进制整数,0-9,a-z分别代表十进制0-36
    for (int i = 0; Inform[i] != '\0'; i++)
    {
        //字母转10进制整数
        if (Inform[i] > '9')
            buff[i] = Inform[i] - 87;
        else
            buff[i] = Inform[i] - 48;//0ASCII为48
        len++;
    }
    //加密运算,C=k1*m+k2 mod n;
    for (int i = 0; i < len; i++)
    {
        buff[i] = (buff[i] * key1 + key2) % n;
    }
    //把数字对应为密文空间内的字符
    for (int i = 0; i < len; i++)
    {
        if (buff[i] < 10)
            Inform[i] = buff[i] + 48;
        else
            Inform[i] = buff[i] + 87;
        
    }
    cout << "密文:" << endl;
    for (int i = 0; i<len; i++)
    {
        cout << Inform[i];
    }
    cout << endl;
    return 0;
}

#endif /* Affine_password_h */

2.置换密码

置换密码算法的原理是不改变明文字符,而是按照某一规则重新排列消息中的比特或字符顺序,才而实现明文信息的加密。加密过程:将明文中的字母按照给定的顺序安排在一个矩阵中,然后用根据密钥提供的顺序重新组合矩阵中的字母,从而形成密文。

代码

Replacement password.h

#ifndef Replacement_password_h
#define Replacement_password_h

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int replacement(string Inform)
{
    for (int i = 0; i < Inform.size(); i += 7)
    {
        swap(Inform[i], Inform[i + 2]);
        swap(Inform[i], Inform[i + 6]);
        swap(Inform[i], Inform[i + 3]);
        swap(Inform[i], Inform[i + 0]);
        swap(Inform[i], Inform[i + 5]);
        swap(Inform[i], Inform[i + 1]);
        swap(Inform[i], Inform[i + 4]);
    }
    
    cout << "密文:" << endl;
    for (int i = 0; i<Inform.size(); i++)
    {
        if (i != 0 && i % 7 == 0)
            cout << endl;
        cout << Inform[i];
    }
    cout << endl;
    return 0;
}

#endif /* Replacement_password_h */

3.凯撒密码

凯撒加密(Caesar cipher)是一种简单的消息编码方式:它根据字母表将消息中的每个字母移动常量位k1
《4种古典密码算法的设计与实现——C++》

代码

Caesar password.h

#ifndef Caesar_password_h
#define Caesar_password_h

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

void Encry(string Inform,int numB);

int caesar(string Inform)
{
    int numB;
    cout << "请输入该密码算法的偏移数量:";
    cin >> numB;
    Encry(Inform,numB);
    return 0;
}

void Encry(string Inform,int numB)
{
        for(int i=0; i < Inform.size(); i++)
        {
            if(Inform[i] >= 'A' && Inform[i] <= 'Z')
            {
                Inform[i] = ((Inform[i]-'A')+numB)%26+'A';
            }
            else if(Inform[i] >= 'a' && Inform[i] <= 'z')
            {
                Inform[i] = ((Inform[i]-'a')+numB)%26+'a';
            }
        }
        cout << "密文:" << Inform << endl;
}

#endif /* Caesar_password_h */

4.维吉尼亚密码

第一行代表明文字母,第一列代表密钥字母,它的明码表后有26个密码表2,每个表相对前一个发生一次移位。如果只用其中某一个进行加密,那么只是简单的恺撒移位密码。但用方阵中不同的行加密不同的字母,它就是一种强大的密码了。加密者可用第7行来加密第一个字母,再用第25行来加密第二个字母,然后根据第8行来加密第三个字母等。
《4种古典密码算法的设计与实现——C++》

代码

Virginia password.h

#ifndef Caesar_password_h
#define Caesar_password_h

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

void Encry(string Inform,int numB);

int caesar(string Inform)
{
    int numB;
    cout << "请输入该密码算法的偏移数量:";
    cin >> numB;
    Encry(Inform,numB);
    return 0;
}

void Encry(string Inform,int numB)
{
        for(int i=0; i< Inform.size(); i++)
        {
            if(Inform[i] >= 'A' && Inform[i] <= 'Z')
            {
                Inform[i] = ((Inform[i]-'A')+numB)%26+'A';
            }
            else if(Inform[i] >= 'a' && Inform[i] <= 'z')
            {
                Inform[i] = ((Inform[i]-'a')+numB)%26+'a';
            }
        }
        cout<<"密文:"<< Inform << endl;
}

#endif /* Caesar_password_h */

主函数代码

main.cpp

#include <iostream>
#include "Affine password.h"
#include "Replacement password.h"
#include "Caesar password.h"
#include "Virginia password.h"
using namespace std;

int main()
{
    char Information[50] = "I am learning coding";
    cout << "明文:" << Information << endl << endl;
    cout << "======仿射加密======" << endl;
    affine(Information);
    cout << "=====仿射加密结束=====\n" << endl;
    cout << "======置换加密======" << endl;
    replacement(Information);
    cout << "=====置换加密结束=====\n" << endl;
    cout << "======凯撒加密======" << endl;
    caesar(Information);
    cout << "=====凯撒加密结束=====\n" << endl;
    cout << "=====维吉尼亚加密=====" << endl;
    virginia(Information);
    cout << "====维吉尼亚加密结束====\n" << endl;
    return 0;
}

实现结果

《4种古典密码算法的设计与实现——C++》

  1. 凯撒密码 ↩︎

  2. 维吉尼亚密码 ↩︎

    原文作者:维吉尼亚加密问题
    原文地址: https://blog.csdn.net/Anudar/article/details/88648731
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞