从一个文件中读取明密对照表,翻译密文(注释部分为c++primer标准答案,非注释部分自己写的)

本文介绍如何利用C++编程,结合map数据结构,从transfile.txt文件中读取明密对照表,对inputfile.txt中的密文进行翻译。通过对字符串的映射关系,实现对加密文本的解码过程。
摘要由CSDN通过智能技术生成
// stringconvert.cpp : 定义控制台应用程序的入口点。
//
#include <exception>
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <sstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
//	string key,value;
//	map<string,string> trans_map;
//	//if (argc != 3)
//	//	throw runtime_error("Wrong number of arguments!");
//
//	ifstream map_file("transfile.txt");
//	//if (!map_file(argv[2])) {
//	//	throw runtime_error("No transformation file!");
//	//}
//	while (map_file >> key >> value ) {
//		trans_map.insert(make_pair(key,value));
//	}
//	map_file.close();
//
//	ifstream input("inputfile.txt");
//	//if (!input(argv[2])) {
//	//	throw runtime_error("No input file!");
//	//}
//	
//
//	string line;
//	while (getline(input,line)) {
//		istringstream stream(line);
//		string word;
//		bool firstWord = true;
//		while (stream >> word) {
//			map<string,string>::const_iterator mapIt = trans_map.find(word);
//			if (mapIt != trans_map.end()) {
//				word = mapIt->second;
//			}
//			if (firstWord) {
//				firstWord = false;
//			} else {
//				cout<<ends;
//			}
//			cout<<word;
//		}
//		cout<<endl;
//	}
	map_file.close();
//	input.close();
//	return 0;

//注释部分为C++ primer标准答案
	map<string,string> mapList;
	string key,value;
	ifstream trans_file("transfile.txt");
	while (trans_file >> key >> value) {
		mapList.insert(make_pair(key,value));
	}
	trans_file.clear();
	trans_file.close();

	ifstream input_file("inputfile.txt");
	string input;
	while (getline(input_file,input)) {

		string word;
		istringstream inputWord(input);
		while (inputWord >> word) {
			map<string,string>::const_iterator mapIter = mapList.find(word);
			if (mapIter != mapList.end()) {
				word = mapIter->second;
			}
			cout<<word<<ends;
		}
		cout<<endl;

	}
	input_file.clear();
	input_file.close();
	return 0;
}


 

 

明密对照表:transfile.txt

'em them
cuz because
gratz grateful
i I
nah no
pos supposed
sez said
tanx thanks
wuz was

 

加密后的文件inputfile.txt

nah i sez tanx cuz i wuz pos to
not cuz i wuz gratz

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了你的要求。为了实现维吉尼亚密码,我们需要进行多表代换。多表代换是一种加密方法,它将明文的每个字符都替换为密文对应的字符。在维吉尼亚密码,我们使用一组密钥来生成多个代换表,这样可以增强加密的安全性。 下面是一个简单的 Python 实现,它从一个文件读取明文并加密,然后将密文保存到另一个文件: ```python def encrypt_vigenere(plaintext, key): """使用维吉尼亚密码加密明文""" ciphertext = "" key_index = 0 for c in plaintext: if c.isalpha(): # 将字母转换为数字,A 对应 0,B 对应 1,依此类推 c = c.upper() c_num = ord(c) - ord('A') key_num = ord(key[key_index]) - ord('A') # 使用密钥的字母对明文的字母进行代换 c_num = (c_num + key_num) % 26 c = chr(c_num + ord('A')) key_index = (key_index + 1) % len(key) ciphertext += c return ciphertext # 从文件读取明文 with open("plaintext.txt", "r") as f: plaintext = f.read() # 使用密钥加密明文 key = "SECRET" ciphertext = encrypt_vigenere(plaintext, key) # 将密文保存到文件 with open("ciphertext.txt", "w") as f: f.write(ciphertext) ``` 在这个实现,我们定义了一个 `encrypt_vigenere` 函数,它接受明文和密钥作为参数,并返回加密后的密文。函数首先将明文的每个字母转换为数字,然后使用密钥的字母对它们进行代换。最后,函数将加密后的密文作为字符串返回。 我们从文件读取明文,然后使用密钥加密它,并将密文保存到另一个文件。你可以根据需要修改文件名和密钥。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值