【问题标题】:How do I get alphabet from the user如何从用户那里获取字母表
【发布时间】:2018-12-14 21:01:23
【问题描述】:

我有这个问题。我为“vigenere cipher”编写了一个代码。

我有文字和钥匙。但我想从用户那里得到字母表。用户想要什么等:“abcdfgh”或“sdgfjdgkfdsgs”正是用户想要的。

所以,但我做不到。

如何从用户那里获得字母表?

首先,我想从用户那里获取字母表。 之后,我希望它输入单词并对其进行加密。但是字母表是用户的字母表。

Here is the codes:
 #include <iostream>
 #include <string>
 using namespace std;

 // Bu fonksiyon bir key oluşturur.
  string generateKey(string str, string key) 
 {
 int x = str.size();

 for (int i = 0; ; i++)
  {
      if (x == i)
        i = 0;
      if (key.size() == str.size()) // eğer oluşturulan key boyutu girilen 
    metnin boyutuna eşitse fonksiyonu durdur.
        break;
    key.push_back(key[i]);
}
return key;
    }
    string cipherText(string str, string key) // "/Bu fonksiyon orjinal metni şifreler \"
      {
    string cipher_text;

   for (int i = 0; i < str.size(); i++)
  {
    // converting in range 0-25 
    int x = (str[i] + key[i]) % 26;

    // alfabeyi ASCII kodlarina dönüştür:
    x += 'A';

    cipher_text.push_back(x);
 }
       return cipher_text;
  }

    //  "/Bu fonksiyon şifreli metni orjinal hale getirir\"

      string originalText(string cipher_text, string key)
    {
string orig_text;

for (int i = 0; i < cipher_text.size(); i++)
{
    // converting in range 0-25 
    int x = (cipher_text[i] - key[i] + 26) % 26;

    // convert into alphabets(ASCII) 
    x += 'A';
    orig_text.push_back(x);
}
return orig_text;
       }
        int main()
          {
cout << " Sifrelenmesini istediginiz kelimeyi/cumleyi giriniz" << endl;
string str;
getline(cin, str);

//string str = "METINBUGRA";
cout << "Anahtar kelimeyi giriniz." << endl;
string keyword;
getline(cin, keyword);
//string keyword = "ABC";

string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);

cout << "Sifrelenmis Kelime : "
    << cipher_text << "\n";

cout << "Cozumlenmis kelime : "
    << originalText(cipher_text, key);
system("pause");
return 0;
   }

【问题讨论】:

    标签: c++ encryption vigenere


    【解决方案1】:

    如果我正确理解了您的问题,您想使用自定义字母而不是英文字母。例如,您可以添加数字。

    1. 您必须对数字进行运算,而不是实际字母:0, 1, 2, ... N-1,其中N 是字母的大小。对于英文字母,这意味着您必须使用0 而不是A (0x41)、1 而不是B (0x42)、... 25 而不是Z

    2. 如果密钥大小为M,则i位置处字母的加密算法为:

      ( L[i] + K[i mod M] ) mod N

      一旦您有了对数字进行运算的函数式算法,您所要做的就是将输入从字母映射到数字,然后将输出从数字映射到字母

    3. 将数字映射到字母很容易;您只需要将字母表存储到一个字符串中 - 这回答了您的问题

      string n_to_letter; // alphabet
      //...
      int main()
      {
        //...
        cin >> n_to_letter; // read the alphabet
        //...
      
    4. 将字母映射到数字可能超出您目前的知识范围;您必须使用 map:

      #include <map>
      //...
      string n_to_letter; // alphabet
      map< char, int > letter_to_n;
      
      void init_letter_to_n() //...
      

      如果您不知道如何使用地图,有一个解决方法:只需搜索字母字符串中的字母,或使用 256 个字符的向量/字符串。

    DEMO

    【讨论】:

      猜你喜欢
      • 2022-10-07
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多