c++ - 用C++加密字符串

标签 c++

我非常需要C++程序的帮助,该C++程序应该对用户输入的字符串(source)进行加密,并将加密后的字符串保存到另一个字符串(destination)。

在对字符串进行加密之前,它会通过lowerCaseString()函数运行,该函数会将source转换为所有小写字母。此功能运行良好。

您可以帮助我的程序正确显示加密的消息吗?我是C++的新手。

我的错误:

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 0) >= this->size() (which is 0)
#include <iostream>
#include <string>
#include <ostream>
#include <fstream>
#include <sstream>
#include <cctype>
#include <vector>

using namespace std;

string lowerCaseString(string &source);
bool substitution(string &source, string cipherKey, string &destination);

int main()
{
    string source;
    string cipherKey = "qwertyuiopasdfghjklzxcvbnm";
    string destination;
    ifstream inFile;

    cout << "Please enter a string: " << endl;
    cin >> source;

//eventually has to write to a file, but I want to get the cipher part working first
//cin >> cipherKey;
//    inFile.open("C:/Users/ellio/OneDrive/Desktop/Freshman Semester 2/ECE 1080C/ECE Labs/otherlabfiles/small-file.txt"); /*make necessary change for
//                                           file access path*/
//        if (!inFile){
//            cout << "Input file cannot be opened" << endl;
//               return 0;
//        }
//        stringstream buffer;
//        buffer << inFile.rdbuf();
//        //change to file_string
//        source = buffer.str();

    lowerCaseString(source);
    substitution(source, cipherKey, destination);

    cout << destination << endl;

    return 0;
}

//converts all letters that are upper case to lower case in source
string lowerCaseString(string &source)
{
    unsigned i;
    for(i = 0; i < source.size(); ++i)
    {
        if(isupper(source.at(i)))
        {
            source.at(i) = tolower(source.at(i));
        }
    }
    return source;
}

//encrypts the source string based on the cipher key, then writes the encrypted string to string destination
bool substitution(string & source, string cipherKey, string &destination)
{
    //the boolean function type is irrelevant to my error (I tried to run it with void type), I just have to return true or false if the
    //string source is empty

    //alphabet is used to compare to each value in source
    string alphabet = "abcdefghijklmnopqrstuvwxyz";

    unsigned i;
    unsigned j;

    //this for loop is probably unnecessary but I did it for the purpose of potential debugging
    for(i = 0; i < source.size(); ++i)
    {
        destination.at(i) = source.at(i);
    }

    //if the string is empty
    if(source.size() == 0)
    {
        return 0;
    }

    //if the string isn't empty
    else
    {
        for(i = 0; i < source.size(); ++i)
        {
            for(j = 0; alphabet.at(j) == 'z'; ++j)
            {
                //if the current character in source is equal to a certain letter in the
                //alphabet, write the corresponding value from the cipher key into destination
                if(source.at(i) == alphabet.at(j))
                {
                    destination.at(i) = cipherKey.at(j);
                }
            }
        }
        //changed this maybe change back
        return 1;
    }    
}

最佳答案

这是您的替代方法。目标字符串为空,导致错误:

bool substitution(string & source, string cipherKey, string &destination)
{
    string alphabet = "abcdefghijklmnopqrstuvwxyz";

    destination = source; // To make sure the size of destination is the same as source

    if(source.size() == 0)
    {
        return false;
    }
    else
    {
        for(unsigned int i = 0; i < source.size(); ++i) // You can declare i and j in the loop if you want (in c++ not in c)
        {
            for(unsigned int j = 0; j < alphabet.size(); ++j) // way easier to use size instead of finding 'z'
            {
                if(source.at(i) == alphabet.at(j))
                {
                    destination.at(i) = cipherKey.at(j);
                }
            }
        }
        return true;
    }


}

请注意,字符查找位可以简化为:
bool substitution(string & source, string cipherKey, string &destination)
{
    string alphabet = "abcdefghijklmnopqrstuvwxyz";

    destination = source; // To make sure the size of destination is the same as source

    if(source.size() == 0)
        return false;
    else
    {
        for(unsigned int i = 0; i < source.size(); ++i)
            destination.at(i) = cipherKey.at(alphabet.find(source.at(i)));
        return true;
    }
}

关于c++ - 用C++加密字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60858570/

相关文章:

c++ - 没有 ->Release() 的 DirectX

c++ - 在 C++ 中漂亮地打印一个表

c++ - 字符串连接错误

c++ - 如何在循环中更改 int 变量,如果可以,是否有更有效的方法?

c++ - 如何访问指向映射的指针的元素?

c++ - 在 Optimus 笔记本电脑中以编程方式强制使用 NVIDIA GPU

C++ 析构函数作为虚函数?

c++ - (C++ 内存管理)如果我们有一个 int x = 1234,并且 int &y = x... 那么 y 在堆栈上的地址是什么?

c++ - 如何创建用于创建循环的新样式?

c++ - 为什么这个 cppreference 演示程序会导致段错误?