gpt4 book ai didi

c++ - 运行 key 密码解密知道 key ?

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:20 28 4
gpt4 key购买 nike

我正在处理此作业,其中我使用用户输入字符串、相同或更长长度的 key ,并使用它来执行运行 key 密码来加密和解密文本。加密有效,但解密无效。

手动查看运行 key 表,我发现 key 为“did”的“ice”会加密为“lkh”并进行检查。回头看表,发现“lkh”要变成“ice”,key就得换成“xsx”,一时觉得容易,因为误以为是“sxs” "是 "did",每个字母向前移动 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';
}

我已将魔数(Magic Number)替换为实际字符值,以使代码更易于阅读。

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

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

+26 是一个确保值为正的 fiddle ,因为模不会为负数产生正确的结果。

关于c++ - 运行 key 密码解密知道 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59281383/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com