解码没有密钥的消息

时间:2014-09-14 04:42:00

标签: c++

这是我应该做的 制作一个程序,读取包含加密消息的文本文件并破解它,它有点接近替换密码,我应该将每个字母交换回另一个含义,如果将其移动一个将其移回A,并尝试比较通过一些常用的单词移动单词,以查找是否在移位的单词中找到了2个常用单词

ex:将单词换成一个 检查你转移它是否有2个常见的单词? 没有继续 是的意思是它停止转移

现在这个问题对我来说很困难,因为我没有钥匙可以输入,如果我拥有它会很棒。

我现在遇到的3个问题,在我的无效功能中它不会打印任何东西, 第二个问题是,即使我修复了我的问题(我知道这是因为在我的函数中我添加了一些东西将字符串转换为c_string)问题是它不会移动直到它找到我正在查找的单词我已声明string" Common []" 第三个问题是每当我编译时我得到一个错误无效的转换从const char **到char比较有符号和无符号整数表达式

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

void makeshift (string encrypt, int key)
{
    for (int i = 0 ; i<encrypt.size();i++)
    {
        cout<<encrypt<<endl; // to see what is in file way to debug
        const char *encryptedc; // this is to convert string to cstring
        encryptedc = encrypt.c_str();
        encryptedc-=key;
        cout<<"decrypted message is "<<encryptedc<<endl;
    }
}


int main (int argc, char* argv[])
{
    // this will make me compare between encrypted message to check if it
    // contains this words or not!
    const char* common[]{"the","and","in","he","with","that","as","at","do"};

    string encrypted; // The encrypted message gotten from file

    ifstream message(argv[1]); // get file name typed by user
    if(message.is_open()) // check if file is open
    {
        while (!message.eof()) // check if we reached end of file
        {
            // get the whole line in file
            getline(message,encrypted);

            ///loop throgh it to store numbers in declared varibles
            for (int i = 0 ; i < encrypted.size();i++)
            {
                makeshift(encrypted,i);

                // here is where the error occurs which is "invalid conversion
                // from const char to char
                if(encrypted.find(common) != -1)
                {
                    cout<<"found common"<<endl;
                    cout<<encrypted<<endl;
                    break;
                }
            }
        }
    }
}

我知道你们很难对待人们,也不想问任何问题,但这一次不要对我不好。我试图自己解决这些问题并做了我能做的一切。我实际上希望我可以解决问题,而不会有人为我指点,但没有你,我将不会是一个优秀的程序员。非常感谢帮助。

2 个答案:

答案 0 :(得分:2)

编译错误

首先,您说您在此行上收到编译错误:

if(encrypted.find(common) != -1)

原因是因为find()期望其参数为字符串,但common是字符串的数组。换句话说,find()一次只能搜索一个字。它无法搜索整个单词列表。

要解决这个问题,您需要编写循环并在每次迭代时检查一个单词。


转换功能

接下来是makeshift功能。那里有几点建议。

  1. 没有必要致电c_str()。您可以通过修改循环中的每个encrypted直接更改encrypted[i]
  2. 需要返回结果。如果您修改encrypted但未退回,则来电者不会看到结果。
  3. 打印输出应该在循环之外。大概你只是想在你解密整个字符串之后看到前后结果。
  4. 以下是解决这些问题的样子:

    string makeshift (string encrypt, int key)
    {
        cout << encrypt << endl; // to see what is in file way to debug
    
        for (int i = 0 ; i<encrypt.size();i++)
        {
            encrypted[i] -= key;
        }
    
        cout << "decrypted message is " << encryptedc << endl;
        return encrypted;
    }
    

    然后你会把它称为:

    string decrypted = makeshift(encrypted, i);
    

    顺便说一下,我不确定encrypted[i] -= key;行是否完全正确。我的猜测是你需要处理环绕声。就像你从字母中减去3&#34; A&#34;你应该回到&#34; X&#34;,对吗?如果是这样,我会把它作为TODO留给你。


    I / O错误检查

    最后,我们来谈谈I / O.具体来说,这段代码:

    ifstream message(argv[1]); // get file name typed by user
    
    if(message.is_open()) // check if file is open
    {
        while (!message.eof()) // check if we reached end of file
        {
            // get the whole line in file
            getline(message,encrypted);
    
            ...
        }
    }
    

    C ++中的一个好习惯是检查I / O操作的结果(例如getline)。读完一行后,您需要检查读取是否有效。如果getline()失败(例如,因为它遇到了文件末尾),您就不想继续。

    方便的是,如果你写while (getline(...)),那么一次性完成一大堆事情 - 它会检查文件是否打开,如果它是在EOF ,它会读取行并告诉您读取是否成功。这意味着你可以用一个循环替换上面的东西:

    ifstream message(argv[1]); // get file name typed by user
    
    while (getline(message, encrypted))
    {
        ...
    }
    

答案 1 :(得分:1)

你将加密的值传递给临时搭建,所以无论你在makehift函数中做什么都不会改变main函数中的加密 你有2个解决方案:

你可以将加密指针传递给临时函数

  void makeshift (string* encrypt, int key){
    for (int i = 0 ; i<encrypt.size();i++)
    {   cout<<encrypt->c_str()<<endl; // to see what is in file way to debug
        const char *encryptedc; // this is to convert string to cstring
        encryptedc = encrypt->c_str();
        encryptedc-=key;
        cout<<"decrypted message is "<<encryptedc<<endl;
    }
  }  

  int main (int argc, char* argv[]){
       ...
       makeshift(&encrypted, i);
       ...
  }

或者您可以返回encryptedc的值并将其分配回主函数中的加密

  string makeshift (string encrypt, int key){
    for (int i = 0 ; i<encrypt.size();i++)
    {   cout<<encrypt.c_str()<<endl; // to see what is in file way to debug
        const char *encryptedc; // this is to convert string to cstring
        encryptedc = encrypt.c_str();
        encryptedc-=key;
        cout<<"decrypted message is "<<encryptedc<<endl;
        return encryptedc; //you can assign const char* to string
    }
  }  

  int main (int argc, char* argv[]){
       ...
       encrypted = makeshift(encrypted, i);
       ...
  }