使用C++中的字符矢量来编程凯撒密码

Using a vector of characters in C++ to program a Caesar Cipher

本文关键字:编程 凯撒 密码 C++ 字符 使用      更新时间:2023-10-16

我一直在使用Caesar密码对消息进行加密。我尽量保持简单。

/* 
 * File:   main.cpp
 * Author: Skylar Croy
 *
 * Created on February 10, 2015, 11:50 AM
 * 
 * Purpose: To encrypt a message using the Caesar Cipher 
 */
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
    char letters[] = 
    {'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'}; // Make a vector, letter, which equals [a b c ... z]
    int i, k; // i used in two loops, k used for shift 3 and mod calc
    for (i=0; i<=25; i++) { // Run 26 times (1 for each letter)
        k = (i + 3) % 26; // Performs mod arth. with shift of 3
        letters[i+26] = letters[k]; // letter[i+26] = [d e f g ... c]
        // Note that letters = [a b c ... z d e f g....c]
             /* cout << letters[i]; // This code shows that the operation in this for loop is functioning correctly
                cout << " ";
                cout << letters[i+26];
                cout << " n"; */          
    }
    std:: string x; // declare string x
    cin >> x; 
        int string_length = x.size();
    for (i=0; i<=(string_length-1); i++){ // individual characters of string x can be referenced by x[0] etc
        for (k=0; k<=25; k++){ 
            if (x[i] == letters[k]) {
                            cout << letters[k+26];
            }
        }
    } 
return 0;
}

出于某种原因,当您尝试用w、x、y或z加密消息时,它不起作用,并打印随机垃圾。不确定出了什么问题。任何建议都会有帮助。谢谢

其他答案已经指出了您最初尝试中的错误。我只想说你的目标可以在两条线内实现:

for (auto& each : x)    // where x is your std::string
    each = (each - 'a' + 3) % 26 + 'a';

我还没有读完你的代码,但下面一行肯定超出了范围:

letters[i+26] = letters[k]; // letter[i+26] = [d e f g ... c]

您的letters数组只有26长,但您正在访问第i+26个元素。也许试试:

letters[(i+26)%26] = letters[k]; // letter[i+26] = [d e f g ... c]

您绝对应该NOT尝试分配超出数组范围的值!

同样,当你在中打印你的信件时

cout << letters[k+26];

您将超出数组的界限,这意味着您将打印垃圾。。。

这可能不是所有的错误,但这是一个开始。。。

还有一个提示(除了上面提到的在数组之外写入)-你不能只做这样的替换:

letters[i] = letters[k];

因为您将覆盖现有项目。也许更简单更好的选择是使用另一个数组来创建修改后的字母表?

char new_letters = new char[26]; for (int i=0; i<26; i++) new_letters[i] = letters[(i+3) % 26];

并使用新阵列进行编码:

if ('a' - x[i] < 26) cout << new_letters['a'-x[i]];

它将仅替换小写字母

@nonsensickle是正确的。只需为加密向量制作另一个长度为26的向量即可修复它。谢谢!

以下代码有效:

/* 
 * File:   main.cpp
 * Author: Skylar Croy
 *
 * Created on February 10, 2015, 11:50 AM
 * 
 * Purpose: To encrypt a message using the Caesar Cipher 
 */
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
    char letters[] = 
    {'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'}; // Make a vector, letter, which equals [a b c ... z]
    int i, k; // i used in two loops, k used for shift 3 and mod calc
    char letters_cipher[25];
    for (i=0; i<=25; i++) { // Run 26 times (1 for each letter)
        k = (i + 3) % 26; // Performs mod arth. with shift of 3
        letters_cipher[i] = letters[k]; // letter[i+26] = [d e f g ... c]
        // Note that letters = [a b c ... z d e f g....c]
        /*
        cout << letters[i]; // This code shows that the operation in this for loop is functioning correctly
        cout << " ";
        cout << letters[i+26];
        cout << " n";
        */          
    }
    std:: string x; // declare string x
    cin >> x; 
    int string_length = x.size();
    for (i=0; i<=(string_length-1); i++){ // individual characters of string x can be referenced by x[0] etc
        for (k=0; k<=25; k++){ 
            if (x[i] == letters[k]) {
                cout << letters_cipher[k];
            }
        }
    }
    return 0;
}