为什么我的代码打印错误的密文

Why is my code printing the wrong ciphertext?

本文关键字:密文 错误 打印 我的 代码 为什么      更新时间:2023-10-16

我试图通过在每个字母前进行十个字母来制作一个程序,该程序将字符串变成加密。https://gyazo.com/86F9D708C2F02CF2D70DBC1CD9FA9A06我正在做第2部分。请帮忙!这将很快到期!

我试图用for循环弄乱,但没有帮助。

#include <iostream>
using namespace std;
int main()
{
    //Input Message
    cout << "Enter a message" << endl;
    string message;
    getline(cin, message);
    //Convert Message to Numbers
    int numMess[message.length()];
    for (int i = 0; i<message.length(); i++) {
        numMess[i] = (int)message[i];
    }
    cout << numMess << endl;
    //Encrypt Number Message by adding ten to each one
    int encryptNumMess[message.length()];
    for (int a = 0; a < message.length(); a++){
        encryptNumMess[a] = numMess[a] + 10;
        if (encryptNumMess[a] > 122) {
            encryptNumMess[a] = 97;
        }
    }
    cout << encryptNumMess << endl;
    //Convert Encrypted Number Message to letters
    string encryption[message.length()];
    for (int b = 0; b<message.length(); b++) {
        encryption[b] = (char)encryptNumMess[b];
    }
    cout << encryption << endl;
    return 0;
}

我希望当我键入" helloworld"时,最终产品将是" rovvygybvn"

如果您愿意删除手工编码的循环,则可以使用stl算法(例如std :: transform(来完成此操作:

,但首先,您应该做一些事情:

不要使用诸如122、97等的魔法数字。而不是使用实际字符常数,即 ab等。但是,如果我们假设ASCII,则字母字符代码是连续的,您的特定程序可以简单地简单使用常数字符串表示字母,然后使用简单的索引挑选字符。

const char *alphabet = "abcdefghijklmnopqrstuvwxyz";

然后获得字母a,获得索引所需的简单减法是:

char ch = 'b';
int index = ch - 'a'; // same as 'b' - 'a' == 98 - 97 == 1
std::cout << alphabet[index]; // will print 'b'

鉴于此,接下来的事情是弄清楚如果将10个值添加到该值,则将其添加10个字符,如果大于26,则将其缠绕到字母的开头。这可以使用模量(在除法之后的其余部分(完成

char ch = 'x';
int index = (ch - 'a' + 10) % 26; // Same as ('x' - 'a' + 10) % 26 == (120 - 97 + 10) % 26  == 33 % 26 == 7
std::cout << alphabet[index]; // will print 'h'

接下来的事情是弄清楚相反的情况,在给定一个加密字符的情况下,您必须通过减去10个来找到未加密的字符10。在这里,这与相反的方式包装,因此需要做更多的工作(未显示,但是代码样本反映了所做的(。

将所有这些放在一起,使用std::transform和Lambdas,我们得到以下小程序:

#include <iostream>
#include <algorithm>
#include <string>
#include <iterator>
#include <cmath>
int main()
{
    //Input Message
    const char *alphabet="abcdefghijklmnopqrstuvwxyz";
    std::string message = "helloworld";
    std::string result;
    // set the encrypted string using the formula above and std::transform
    std::transform(message.begin(), message.end(), std::back_inserter(result),
                  [&](char ch) { return alphabet[(ch - 'a' + 10) % 26]; });
    std::cout << "Encrypted: " << result << 'n';  
    // convert back to unencrypted using the above formula and std::transform
    std::string result2;
    std::transform(result.begin(), result.end(), std::back_inserter(result2),
                  [&](char ch) 
                  { int index = ch - 'a' - 10;  index = index < 0?26 - (abs(index) % 26):index % 26; return alphabet[index];});
    std::cout << "Unencrypted: " << result2;                  
}

输出:

 Encrypted: rovvygybvn
 Unencrypted: helloworld

此代码适用于加密,如果要解密,则应该newAlphabetoldAlphabet

我在代码中评论newAlphabetoldAlphabet用于加密且用于解密的代码

#include <windows.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
    // For Encrypt
    string newAlphabet = "abcdefghijklmnopqrstuvwxyz";
    string oldAlphabet = "klmnopqrstuvwxyzabcdefghij";
    // For Decrypt
    //string newAlphabet = "klmnopqrstuvwxyzabcdefghij";
    //string oldAlphabet = "abcdefghijklmnopqrstuvwxyz";

    string input = "";
    string output = "";
    getline(cin, input);
    int inputLen = input.size();
    if (oldAlphabet.size() != newAlphabet.size())
        return false;
    for (int i = 0; i < inputLen; ++i)
    {
        int oldCharIndex = oldAlphabet.find(tolower(input[i]));
        if (oldCharIndex >= 0)
            output += isupper(input[i]) ? toupper(newAlphabet[oldCharIndex]) : newAlphabet[oldCharIndex];
        else
            output += input[i];
    }
    cout << output << endl;
    return 0;
}

正如其他人已经提到的int numMess[message.length()];无效的C 。如果它对您有用,那么您正在使用编译器扩展程序,您真的不应该依靠。正确的方法是:

std::vector <int> numMess(message.length());

查找std ::向量参考以获取更多信息。

接下来,int encryptNumMess[100];创建C数组样式数组。encryptNumMess是阵列的基本指针。尝试std::cout << encryptNumMess时,它将输出指针值,而不是数组。您需要一个用于这样做的循环,例如:

for(int i = 0; i < 100; ++i)
    std::cout << encryptNumMess[i] << " ";
std::cout << endl;

当您像使用NumMess一样将其转换为向量时,以上也有效,而在这种情况下,std::cout << encryptNumMess甚至不会编译。

第三,string encryption[100]创建了100个字符串的数组!不是100号尺寸的字符串。要这样做:

std::string foo(message.length(), '');

我们必须指定要填充字符串的字符。因此美国''。现在,要输出字符串,您可以使用std::cout << foo

最后,由于char上允许算术,因此整个程序可能仅缩短为此

#include <iostream>
int main()
{
    // Input Message
    std::cout << "Enter a message" << std::endl;
    std::string message, encryption;
    getline(std::cin, message);
    // Resize encryption string to the desired length
    encryption.resize(message.length());
    // Do the encryption
    for(size_t i = 0; i < message.length(); ++i) {
        encryption[i] = message[i] + 10;
        if (encryption[i] > 122) {
            encryption[i] = 97;
        }
    }
    // Output the string
    std::cout << encryption << std::endl;
    return 0;
}

当然,您的加密算法仍然按照说明不正确,但是我会留下来供您弄清楚。我相信@paulmckenzie已经告诉您大多数如何修复它,也不使用魔法数字。