c++ - c++ 中的 Vigenere 密码与 txt 文件输入/输出
问题描述
我必须在 C++ 中创建一个 Vigenere 密码,从 txt 文件中输入纯文本/密钥,对其进行密码/解密并将密文输出到文件中。我对编码完全陌生,但我设法写了这个:
char alphabet[32][32] = { 'A','Ą','B','C','D','E','F','G','H','I','J','K','L','Ł','M','N','O','Ó','P','Q','R','S','Ś','T','U','V','W','X','Y','Z','Ź','Ż' };
std::vector<std::string> readPlain(const std::string& filename)
{
std::string line;
std::vector<std::string> to_table;
std::ifstream plain (filename);
if (plain.is_open())
{
while ( getline (plain, line))
{
to_table.push_back(line);
}
plain.close();
}
else std::cout << "cant open file" << std::endl;
return to_table;
}
std::vector<std::string> readKey(const std::string& key)
{
std::string line;
std::vector<std::string> to_table;
std::ifstream plain(key);
if (plain.is_open())
{
while (getline(plain, line))
{
to_table.push_back(line);
}
plain.close();
}
else std::cout << "cant open file" << std::endl;
return to_table;
}
void encipher(std::vector<std::string> plaintext , std::vector<std::string> key)
{
}
在网上我找到了这段代码,我想我可以把它缝在一起让它工作
int tableArr[26][26];
void createVigenereTable() {
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
int temp;
if ((i + 65) + j > 90) {
temp = ((i + 65) + j) - 26;
//adding ASCII of alphabet letter in table index position
tableArr[i][j] = temp;
}
else {
temp = (i + 65) + j;
//adding ASCII of alphabet letter in table index position
tableArr[i][j] = temp;
}
} // for j loop
} // for i loop
void cipherEncryption(string message, string mappedKey) {
createVigenereTable();
string encryptedText = "";
for (int i = 0; i < message.length(); i++) {
if (message[i] == 32 && mappedKey[i] == 32) {
encryptedText += " ";
}
else {
int x = (int)message[i] - 65;
int y = (int)mappedKey[i] - 65;
encryptedText += (char)tableArr[x][y];
}
}
cout << "Encrypted Text: " << encryptedText;
}
我怎样才能让它像这样工作,但是在向量上?或者我该怎么做才能将向量转换为整数才能工作?也许我应该使用不同的东西从头开始?任何帮助都会得到很大的帮助。谢谢
解决方案
推荐阅读
- javascript - 反应生命周期方法中的横切概念
- flutter - 迁移到空安全:参数类型“对象?” 不能分配给参数类型“列表”
' - python - 用于循环打印数组的行数,然后根据数组的长度重复数据,如何在 Python 中只打印 1 行数据?
- javascript - 使用大于 ( > ) 比较 date.toUTCString() 总是返回 false,无论其他日期是否更早或更早
- r - R从数据框中提取嵌套累积
- python - Scrapy初学者:无法从css选择器获取文本表单中的数据,得到空数组
- android - 在 Jetpack Compose 中通过手势关闭“Modal Drawer”时发出事件
- revit-api - Revit 2020 中的项目基点和测量点
- c# - 我的数据库无法在 VS Code 中打开,该文件未显示在编辑器中,因为它是二进制文件
- c# - 设置 HTTP/1.1 或 HTTP/2.0 协议