我想用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();
}
答案 0 :(得分:0)
一段时间后,我找到了解决方案。我改变了文件阅读:
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();
}