【问题标题】:Running Key cipher decryption knowing key?运行密钥密码解密知道密钥?
【发布时间】:2019-12-11 08:15:21
【问题描述】:

我正在处理这个任务,我正在获取一个用户输入字符串,一个相同或更大长度的密钥,并使用它来执行运行密钥密码来加密和解密文本。加密有效,但解密无效。

手动查看运行密钥表,我发现密钥为“did”的“ice”会加密为“lkh”,并且可以检查出来。回头看表,发现“lkh”要变成“ice”,关键是要改成“xsx”,一时以为很简单,因为我误以为是“sxs” " 即每个字母向前移动 15 个字母时的 "did"。这实际上比“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;
}

【问题讨论】:

标签: c++ encryption


【解决方案1】:

getEncryptedText 可以简化为:

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

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

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

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

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

【讨论】:

  • 成功了,非常感谢!我试过了,这解决了我的问题。
猜你喜欢
  • 2020-04-23
  • 1970-01-01
  • 2013-05-23
  • 2011-05-09
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 2017-09-01
  • 1970-01-01
相关资源
最近更新 更多