【问题标题】:C++ Caesar Encryption Project (ASCII)C++ 凯撒加密项目 (ASCII)
【发布时间】:2015-05-11 21:35:53
【问题描述】:

对于课堂,我必须用 C++ 开发一个 Caesar 加密项目,该项目接受一个字符串,并将所有 ascii 代码移动一个随机整数。我的项目运行良好,代码如下:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;
int main(int argc, const char * argv[]) {

    string sentence = "";
    int shifter, counter = 0, position = 0;
    srand(time(NULL));
    shifter = (rand() % 25) + 1;
    cout << "Enter a sentence, and this program will encrypt it: " << endl;
    getline(cin, sentence);
    cout << "\n\n\n";
    for (int i = 0; i < sentence.length(); i++) {
        sentence[i] = sentence[i] + shifter;
        cout << sentence[i];
    }

    return 0;
}

我唯一的问题是我的老师希望他们只改成字母。如果字母是“z”并且移位是“2”,她希望输出是“b”。我不确定如何做到这一点。

这个问题的简单程序可行吗?如果有,怎么做?

【问题讨论】:

  • 你熟悉取模运算吗?
  • @iProgramIt 好的,那么 28 mod 26 是什么? 28 是 z + 2。
  • 2 就是答案。
  • @iProgramIt 以及如何将2 映射到字母表的第三个字母(我们假设我们从0 开始)?
  • 请,请,请在询问之前搜索:Possible duplicates of Caesar Cipher。我也回答了几个。

标签: c++ string encryption caesar-cipher


【解决方案1】:

第一步是要意识到字母需要映射到数字,以某种方式旋转,然后再映射回字母。

@vsoftco 已经尝试通过他的 cmets 让你走上正轨,所以请仔细阅读它们,因为他给你的提示非常好。

我会尝试填补@vsoftco 遗漏的一些空白。

以下是将字母映射到数字的方法:

char letter = 'B';
int number = letter - 'A';

字母只是计算机知道用字母表示的数字。事实上,ASCII 中的字母“B”是 66。所以如果我们在 C 中这样做:

char letter = 'B';
int number = 100 - 'B'; // <- this is equal to 100 - 66 which is 34

number 变为 34。

这很好,但我们希望我们的数字在 0 到 25 之间(以便轻松应用模运算)。

考虑一下。如果将 5 加到 0 到 25 之间的未知数上,如何确保结果小于 26?使用模数,它会为您包装运算。

但是回到将字母转换为数字的问题。要将任何大写字母(注意小写字母有不同的数字)转换为 0 到 25 之间的数字,您可以像这样减去“A”:

char letter = 'C';
int number = letter - 'A'; // <- this is 'C' - 'A' = 67 - 65 = 2 ( which is the 3rd number if we start counting from zero )

要将数字转换回字母,只需添加“A”。

int number = 5; // This is the 6th letter since we start counting from zero
char letter = 'A' + number; // Now letter is 'A' + 5 = 65 + 5 = 70 which is 'F'...

通过这种转换和 cmets 中描述的 @vsoftco 模运算,您应该能够自己制作凯撒算法。

【讨论】:

    【解决方案2】:

    愿这能解决你的问题:) http://ideone.com/6AzY2i

    #include <bits/stdc++.h>
    #define ll long long int
    #define MOD 1000000007
    
    using namespace std;
    char st[10000000], s[1000000];
    
    int main(){
        cout << "Enter your Ceaser Chipher to decrypt:\n";
        cin >> s;
        ll t;
        t = strlen(s);
        ll n;
        strcpy(st,s);
        /*cout << "Shift of how many characters:\n";
        cin >> n;*/
        cout << "Shift in which direction (r(right) or l(left)) :\n";
        char c;
        cin >> c;
        srand(time(NULL));
        n = (rand() % 25) + 1;
        cout << "Your String is shifted by:" << n <<endl;
        ll f;
        if(c=='r'){
            for(int i=0;i<strlen(st);i++){
                f = st[i] + n;
                if(f>122)
                    f = f - 26;
                st[i] = f;
            }
        }
        else if(c=='l'){
            for(int i=0;i<strlen(st);i++){
                f = st[i] - n;
                if(f<97)
                    f = f + 26;
                st[i] = f;
            }
        }
        cout <<"Your encrypted String :-> "<< st <<endl;
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多