运行密钥密码解密知道密钥?

Running Key cipher decryption knowing key?

本文关键字:密钥 解密 密码 运行      更新时间:2023-10-16

我正在做这个作业,我正在接受一个用户输入字符串,一个相同或更大长度的密钥,并使用它来执行运行密钥密码来加密和解密文本。 加密有效,但解密无效。

手动查看运行键表,我发现键为"did"的"ice"将加密为"lkh"并检查出来。 回头看表,我发现要把"lkh"变成"冰",键必须改成"xsx",有一会儿,认为这很容易,因为我错误地认为是"sxs",每个字母向前移动 15 个字母。 实际上,它比"d"移动20个字母和"i"仅移动10个字母以构成"xsx"更随意。

我不确定在我的 decrypt((、getDecryptedText(( 或 decryptionKey(( 函数中放什么来完成这项工作,或者我是否在尝试移动字母的正确轨道上。 我想我一定是,但我目前的想法是,可能需要某种循环来确定键中的每个字符应该向前移动多少个字母。

#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
#include <cctype>
// Running Key Cipher Explanation
// http://practicalcryptography.com/ciphers/classical-era/running-key/
void encrypt(std::string&, std::string&, std::string&);
void decrypt(std::string&, std::string&, std::string&);
char getEncryptedText(char p, char k);
char getDecryptedText(char p, char k);
std::string decryptionKey(std::string&);
void getKeyIndex(int &i, std::string &key);
int main() {
// Initialization
std::string input;
std::string key;
std::string encryptedText;
std::string decryptedText;
// Assignment
std::cout << "Enter secret message: ";
std::getline(std::cin, input);
std::cout << "Enter key (longer than message): ";
std::getline(std::cin, key);
// Remove spaces
input.erase(remove_if(input.begin(), input.end(), isspace), input.end());
key.erase(remove_if(key.begin(), key.end(), isspace), key.end());
// Exit if key length < secret text
if (key.length() < input.length()) {
std::cout << "The encryption key must be longer than the message." 
<< std::endl;
return 1;
}
// Encrypt the text
encrypt(input, key, encryptedText);  
// Display encrypted text to user
std::cout << "nThe encrypted text is:" << std::endl;  
std::cout << encryptedText << std::endl;
// Decrypt the text
decrypt(encryptedText, key, decryptedText);
// Display decrypted text to user
std::cout << "nThe decrypted text is:" << std::endl;  
std::cout << decryptedText << std::endl;
return 0;
}
void encrypt(std::string &input, std::string &key, std::string &encryptedText) {
std::string::iterator i;
std::string::iterator j;
// Contains the encrypted version of the text
encryptedText = "";
// Encrypt every character in the input string
for(i = key.begin(), j = input.begin(); j < input.end();) {
// Remove non-letters from text
if (!isalpha(*j)) {
j++;
continue;
}
// get encrypted char
encryptedText += getEncryptedText(tolower(*j),tolower(*i));
i++;
j++;
}
}
char getEncryptedText(char p, char k) {
// Number to be converted into the nth letter in the alphabet
int encryptedText;
encryptedText = p + k;
if (encryptedText >= 219) {
return (char) (encryptedText - 123);
}
return (char)(encryptedText - 97);
}
void decrypt(std::string &encryptedText, std::string &key, std::string &decryptedText) {
std::string::iterator i;
std::string::iterator j;
// Contains the decrypted version of the text
decryptedText = "";
// Change key to decrypt
key = decryptionKey(key);
// Decrypt every character in the input string
for(i = key.begin(), j = encryptedText.begin(); j < encryptedText.end();) {
// get decrypted char
decryptedText += getDecryptedText(tolower(*j),tolower(*i));
i++;
j++;
}
}
char getDecryptedText(char p, char k) {
// Number to be converted into the nth letter in the alphabet
int decryptedText;
decryptedText = p + k;
// If it gets passed z, go back to a
if (decryptedText >= 219) {
return (char) (decryptedText - 123);
}
return (char)(decryptedText - 98);
}
std::string decryptionKey(std::string &key) {
for (int i = 0; i < key.length(); i++) {
// Store integer ASCII value of char
int asc = key[i];
int rem = asc - (26 - (key[i] - 'a'));
int m = rem % 26;
key[i] = (char)(key[i] + 15);
}
// Decryption Key cout for testing
std::cout << "Altered key: " << key; 
return key;
}

getEncryptedText可以简化为:

char getEncryptedText( char p, char k )
{
return ( ( p - 'a' ) + ( k - 'a' ) ) % 26 + 'a';
}

我已将幻数替换为实际字符值,以使代码更易于阅读。

如果我们确保getDecryptedTextgetEncryptedText完全相反,则无需修改密钥。

char getDecryptedText( char p, char k )
{
return ( ( p - 'a' ) - ( k - 'a' ) + 26 ) % 26 + 'a';
}

+26是确保值为正的小提琴,因为模数不会为负数产生正确的结果。