【问题标题】:vigenere cypher in c++c ++中的vigenere密码
【发布时间】:2016-10-24 06:33:24
【问题描述】:

我尝试创建一个 c++ 程序,它接受一些输入并使用 vignere 密码对其进行加密。

我的输入是:

迅捷的棕狐跳过懒狗

给定键“hello”,输出如下:

alpdvpjemqvayqnenfxozsgpqalpwgcozfg

其中解密,拼写如下:

theshiftbcownfzxjumasovectelsvkous

50% 以上的密码是正确的,我不知道我是如何做到这一点的。

我的代码如下:

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <sstream>
#include <streambuf>
#include <locale>

class Passcode {
private:
    std::string passcode;
public:
    void set_passcode(const std::string pc){
        passcode = pc;
    }
    char get_char(std::size_t index){ 
        return passcode[index];
    }
    std::size_t get_size(){ 
        return passcode.length();
    }
};

int find_index(char* arr, const std::size_t SIZE, const char flag){
    for(std::size_t i=0; i<SIZE; ++i){
        if (arr[i]==flag) return i;
    }
    return -1;
}

int main() {
  char alphabet[26] = {'a','b','c','d','e','f','g','h',
               'i','j','k','l','m','n','o','p',
               'q','r','s','t','u','v','w','x',
               'y','z'};

  //grab input
  std::ifstream input("input.txt");
  std::stringstream buf;
  buf << input.rdbuf();
  std::string str = buf.str();

  //codify string 
  //no spaces, no caps, etc.
  std::locale loc;
  std::string uncyphered_text;
  for(std::size_t i=0; i<str.length(); ++i){
    if(str[i]!=' ') uncyphered_text+=std::tolower(str[i],loc);
  }

  //grab key
  std::cout << "Enter your passcode: "; 
  std::string in; std::cin >> in;
  Passcode pass; pass.set_passcode(in);

  //encypher text
  std::string encyphered_text;
  for(std::size_t i=1; i<=uncyphered_text.length(); ++i){
    char current_char = pass.get_char(i%pass.get_size()-1);
    int current_char_index = find_index(alphabet, 26, current_char);
    int current_cypher_index = find_index(alphabet, 26, uncyphered_text[i-1]);
    encyphered_text+=alphabet[(current_char_index+current_cypher_index)%26];
  }

  std::cout << encyphered_text << std::endl;

  return 0;
}

我觉得我可能在模数运算符上做错了。

【问题讨论】:

  • 你知道加密文本应该是什么吗?您确定这是您的加密问题而不是解密问题吗?您是否尝试过在调试器中逐行执行代码?
  • @Joachim 下面是它应该如何加密,大写正确加密,我的小写尝试失败:(对不起,我在格式化方面很烂) ALPDK PJEMF VAYQC ENFXD ZSGPF ALPWO GCOZU alpdv pjemq vayqn enfxo zsgpq alpwg cozfg
  • 好吧,-1 至少不是必需的,mod 运算符的结果是 [0..n-1]。您的 for 循环应以 0 开头,使用 i++,因为 ++i 可能会使读者感到困惑。
  • @MaartenBodewes 我觉得自己像个白痴,我做了你的建议,现在它似乎奏效了!我混淆了 n%0=undefined 与 0%n 不是未定义的原则。非常感谢!

标签: c++11 encryption cryptography vigenere modular-arithmetic


【解决方案1】:

问题:模数使用不当和索引不当。

  //encypher text
  std::string encyphered_text;
  for(std::size_t i=0; i<uncyphered_text.length(); ++i){
    char current_char = pass.get_char(i%pass.get_size());
    int current_char_index = find_index(alphabet, 26, current_char);
    int current_cypher_index = find_index(alphabet, 26, uncyphered_text[i]);
    encyphered_text+=alphabet[(current_char_index+current_cypher_index)%26];
  }

这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多