2

我正在查看这段代码以读取我桌面上的一个文件,该文件解密了一个 cesar 密码,我一直试图弄清楚如何在这个程序中计算移位。

据我所知,Max e's 是频率最高的移位字母。由于 e 是英语中最常见的字母,因此程序试图将密码中出现频率最高的字符设置为英语中的“e”。就目前而言,这很好,但是有很多短语 e 不是最常见的字母,然后它就会落在它的脸上。

那么,我怎么能告诉程序猜测最常见的密码字母是纯文本中的 e 但万一不是,然后继续尝试将 e 转移到文本中第二个最常见的字母,等等上,直到我找到它?

一位朋友帮助了这部分,但英语很差,所以他很难向我解释。有人可以详细说明吗?非常感谢您的帮助!让我知道你的想法:

#include <iostream>
#include <string>
#include <cctype> // isalpha, islower, isupper, functions
#include <fstream>
using namespace std;

string caesarShift(string text, int shift);
int main()
{
    int maxEs = 0;       // # of e's in maxString
    int currentEs = 0;    // # of e'sin currentString
    string maxString;       // decrypted caesar shift with most e's
    string currentString;    //decrypted caesar shift

    string cipher;       // Stores cipher text
    char ch;    // Stores currentcharacter for reading
    ifstream fin("/Users/jasonrodriguez/Desktop/encrypted.txt");   //opens "encrypted.txt" file
    while( fin.get(ch) )    // readseach char into the cipher till EOF
    {
        cipher += ch;
    }
    fin.close();    // be safe andclose file

    for(int i=0; i < 26; i++)
    {
        currentEs =0;    // Reset counter
        currentString =caesarShift(cipher, i);    // get shifted text
        for(unsigned int x=0; x <currentString.size(); x++) // check each character of stringarray
        {
            if(currentString[x] == 'e' || currentString[x] == 'E') // check fore's
            {
                currentEs++;    // increment Ecounter
            }
        }
        if(currentEs > maxEs) //if currentEs is greater than maxEs, replace max with current
        {
            maxEs =currentEs;
            maxString= currentString;
        }
    }
    cout << maxString << endl;
    return 0;
}

/**
 string caesarShift(string text, int shift)
 Decrypts Caesar Shift using text and shift
 */
string caesarShift(string text, int shift)
{
    shift = shift % 26;    // Morethan 26 is redundant and unneeded
    char ch = 0; // holds current character
    char chs = 0;    // holds shiftedcharacter
    for(unsigned int i=0; i < text.size();i++)
    {
        ch = text[i];
        if( isalpha(ch) )
        {
            chs = ch -shift; // reverse shifting
            if( (islower(ch) && chs < 'a' )  // If is lowercase andshifted value is lower than 'a'
               ||
               ( isupper(ch) && chs < 'A' ) ) // Ifis uppercase and shifted value is lower than 'A'
            {
                chs += 26;    // Add 26(number ofletters) to get back to the correct place in alphabet
            }
            text[i] =chs;    // Set character to shifted character
        }
    }
    return text;
}

问题:

  1. 据我所知,Max e's 是频率最高的移位字母。由于 e 是英语中最常见的字母,因此程序试图将密码中出现频率最高的字符设置为英语中的“e”。就目前而言,这很好,但是有很多短语 e 不是最常见的字母,然后它就会落在它的脸上。那么,我怎么能告诉程序猜测最常见的密码字母是纯文本中的 e 但万一不是,然后继续尝试将 e 转移到文本中第二个最常见的字母,等等上,直到我找到它?

  2. 我认为如果我将一个字符移动一个移位量,该字符可能会或可能不会超出范围。'a' + 3 是 'd' 可以,'x' + 3 是 '{' 不行。因此,如果字符在“z”之上,则删除 26,如果在“a”之下,则添加 26。可以是一个可用的功能。但是,您能否向我解释一下如何在程序中计算移位并将其应用于文件?它让我完全难过:(

4

1 回答 1

0

计算班次:

首先,您需要将字母“A”到“Z”(假设只有大写字母)映射到整数 0 到 25。C++ 让您通过减法轻松做到这一点:

n = c - 'A';

现在您可以使用模算术执行移位:

n = (n + shift) % 26;

最后映射回一封信:

p = n + 'A';

(请注意,您需要对这些示例中使用的变量进行适当的声明。我建议您使用比我使用的单字母变量名称更有意义的名称。)

于 2013-03-12T22:57:32.103 回答