【问题标题】:Affine cipher decryption with bruteforce for keys使用暴力破解密钥的仿射密码解密
【发布时间】:2018-04-21 20:28:06
【问题描述】:

我想用 affine_algorithm 解密消息,但从一开始就不知道密钥,我需要暴力破解它们以找到正确的组合。在下面的代码中,解密的消息是不正确的,为了理解它们,它们没有任何意义。我认为仿射方程有问题,我看到了一些其他带有a_inverse 的代码,但我不知道如何在不知道密钥的情况下进行暴力破解。

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

int main(int argc, char **argv)
{
    ifstream myfile("C:\\encr_affine.txt");
    string Ciphertext;

    while (myfile>>Ciphertext)
    {
        getline(myfile, Ciphertext);
    }

    for (int b = 1; b <= 25; b++)
    {

        for (int a = 1; a <= 25; a = a + 2)
        {
            if (a == 13)
                a = 15;

            string Msg = "";

            for (int i = 0; i < Ciphertext.length(); i++)
            {

                if (Ciphertext[i] != ' ')
                    Msg = Msg + (char)(((a * ((Ciphertext[i] - 'A' + b)) % 26)) + 'A');
                else
                    Msg += Ciphertext[i];
            }

            cout << "\n Key is : " << a << ", " << b << endl;
            cout << "Decr. message is : " << Msg << endl;

        }
    }
myfile.close();

}

【问题讨论】:

    标签: c++ encryption brute-force


    【解决方案1】:

    经过一段时间,我找到了解决方案。我更改了文件读取:

    while (getline(myfile, Ciphertext))
        {
                    //reading the ciphertext from the file
        }
    

    我添加了 a_inverse 方程:

    for (int b = 1; b <= 25; b++)
        {
    
            for (int a = 1; a <= 25; a = a + 2)
            {
                if (a == 13)    //codition for only 12 a_keys
                    a = 15;
    
                string Msg = "";
                int a_inv = 0;
                int flag = 0;
    
                for (int i = 0; i < 26; i++)
                {
                    flag = (a * i) % 26;
                    //Applying decryption formula a^-1
                    //Check if (a*i)%26 == 1 ,then i will be the multiplicative inverse of a
                    if (flag == 1)
                    {
                        a_inv = i;
                    }
                }
    
                for (int i = 0; i < Ciphertext.length(); i++)
                {
                    toupper(Ciphertext[i]);
    
                    if (Ciphertext[i] != ' ')       //if its "space" do your job!
                        Msg = Msg + (char)(((a_inv * ((Ciphertext[i] + 'A' - b)) % 26)) + 'A'); //affine equation 
                    else
                        Msg += Ciphertext[i];       //if its "space" , let it!
                }
    
                cout << "\n Key is : " << a << ", " << b << endl;            //print keys and decrepted message
                cout << "Decr. message is : " << Msg << endl;
    
            }
        }
    
    myfile.close();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-12
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      相关资源
      最近更新 更多