UVa10896 - Known Plaintext Attack(字符处理)

本文详细介绍了古代罗马帝国时期的凯撒密码加密系统,并通过已知明文进行攻击来揭示可能的加密密钥。通过实例演示,展示了如何在特定条件下找出加密所用的字母偏移量,进而破解加密信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B: Known Plaintext Attack 

One of the earliest encrypting systems is attributed to Julius Caesar: if the letter to be encrypted is the $m$-th letter in the alphabet, replace it with the $(m+k)$-th where $k$ is some fixed integer. Caesar used $k=3$, or a $\rightarrow$ d, as the key to the encryption. That is, ``a" would be encrypted to ``d", ``b" would be ``e", and so on until ``z" would be ``c". For example, for a $\rightarrow$ k, the sentence ``this is a test" would become ``drsc sc k docd".

Now you have an encrypted sentence, and you know a decrypted word in that sentence. Your task is to find all the possible keys to the encryption.

Input 

The input starts with a line containing a number, $n$, followed by $n$ sets of encrypted sentence/decrypted word pairs. Each set has two lines, the first line is the encrypted sentence which contains only words separated by a space with no punctuation, the second line contains a word which is one of the decrypted word of the sentence above it. All words are in lowercase letters. You may assume a word is at most 16 characters long, and a line has no more than 70 characters.

Output 

The output has $n$ lines of character(s), corresponding to the $n$ sets of encrypted sentence/decrypted word pairs. The line consists of lowercase letter(s) of possible key(s). That is, if the plaintext ``a" is encrypted to ``x", then ``x" would be in the line. Each line of characters must be sorted in alphabetical order.

Sample Input 

2
drsc sc k docd
test
dl ruvd doha hp pz
we

Sample Output 

k
hl
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 100;

vector<string> vstr;
string s;

void input()
{
	char buf[N], tmp[N];
	fgets(buf, N, stdin);
	buf[strlen(buf) - 1] = 0;
	
	for (int i = 0, len = strlen(buf); i < len; i++) {
		if (i == 0) {
			sscanf(buf, "%s", tmp);
			vstr.push_back((string)tmp);
		} else if (buf[i] == ' ') {
			sscanf(&buf[i + 1], "%s", tmp);
			vstr.push_back((string)tmp);
		}
	}
	
	fgets(buf, N, stdin);
	buf[strlen(buf) - 1] = 0;
	s = buf;
}

bool check(string &s1, string &s2)
{
	int diff = (s1[0]  - s2[0] + 26) % 26;
	
	for (size_t i = 1, size = s1.length(); i < size; i++) {
		if (diff != (s1[i] - s2[i] + 26) % 26) return false;
	}
	
	return true;
}

void solve()
{
	string ans;
	
	for (size_t i = 0, size = vstr.size(); i < size; i++) {
		if (vstr[i].length() == s.length()) {
			if (check(vstr[i], s)) {
				int diff = (vstr[i][0] - s[0] + 26) % 26;
				ans += ('a' + diff);
			}
		}
	}
	
	sort(ans.begin(), ans.end());
	printf("%s\n", ans.c_str());
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("d:\\OJ\\uva_in.txt", "r", stdin);
	#endif
	
	char buf[N];
	int t;
	fgets(buf, N, stdin);
	sscanf(buf, "%d", &t);
	while (t--) {
		vstr.clear();
		input();
		solve();
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值