凯撒密码面向对象编程
Caesar cipher object oriented programming
我一直在使用面向对象编程实现凯撒密码。问题是,当我调用加密函数时,我得到与用户输入相同的消息...消息未正确加密。
例如,如果我写 "abc" 而不是 "bcd",我会得到 "abc"。我在 C++ 中尝试过,代码可以正常工作(逻辑很好),但我尝试在 C++ 中使用面向对象的编程并创建了三个文件来分隔代码。
任何人都可以帮助或指出我可能犯的任何错误吗?
可执行测试文件(main
函数):
#include <iostream>
#include <string>
#include "CyclicShift.h"
using namespace std;
int main()
{
string text;//the string that holds the user input
int key;//key holds the number by which the user wants the alphabets to be shifted
cout << "Enter your phrase: " << endl;
getline(cin, text);//gets the user input ( including spaces and saves it to the variable text)
cout << "Please choose a number(key) for which you wants the alphabets to be shifted: " << endl;
/*Note: the key can either be positive (forward shifting), negative (backward shifting) or zero (no shifting)*/
cin >> key;//User input for number by which alphabets are going to be shifted
const CyclicShift aShift;
cout << "Encrypted Message : " << aShift.Encrypt(text, key) << endl;
//system("Pause");
return 0;
}
头文件.h
:
#pragma once
#include <iostream>
#include<string>
//Note: No using "namespace std;" in header files
class CyclicShift
{
private:
char fUpperCase[26];//A-Z
char fLowerCase[26];//a-z
public:
CyclicShift();
std::string& Encrypt(std::string& aOriginalMessage, int &aKey) const;// Function Prototype. This declares Encrypt to be a function that needs one string and one integer variables as arguments. Reference operator & in prototype
};
源文件.cpp
:
#include "CyclicShift.h"
#include<iostream>
#include<string>
using namespace std;
CyclicShift::CyclicShift()
{
char fUpperCase[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'};//Initialization of class member
char fLowerCase[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'};//Initialization of class member
}
string& CyclicShift::Encrypt(string& aOriginalMessage, int & aKey) const
{
int z;//z holds the value (int) of the length of the user input ( including spaces)
z = (int)aOriginalMessage.length(); /*give the variable z the value of the user input length and length is normally an unsigned long integer hence
we have to typecast it*/
/*counter that makes it keep looping until it "encrypts" all of the user input (that's why it keeps looping while its less than z)*/
for (int i = 0; i < z; i++)
{
for (int j = 0; j < 26; j++)
{
if (aOriginalMessage[i] == fLowerCase[j] || aOriginalMessage[i] == fUpperCase[j])
{
if (aKey > 0)
{
//another counter that loops forwards key times by incrementing method
for (int counter = 0; counter < aKey; counter++)
{
/*it checks if the letter text[x] is 'z' and if it is 'z' it will make it 'a'*/
if (aOriginalMessage[i] == 'z')
{
aOriginalMessage[i] = 'a';
}
else if (aOriginalMessage[i] == 'Z')
{
aOriginalMessage[i] = 'A';
}
else
{
aOriginalMessage[i]++;
}
}
}
else if (aKey < 0)
{
//another counter that loops backwards key times by decrementing method
for (int counter = 0; counter < abs(aKey); counter++)
{
/*it checks if the letter text[x] is 'a' and if it is 'a' it will make it 'z'*/
if (aOriginalMessage[i] == 'a')
{
aOriginalMessage[i] = 'z';
}
else if (aOriginalMessage[i] == 'A')
{
aOriginalMessage[i] = 'Z';
}
else
{
aOriginalMessage[i]--;
}
}
}
else
{
aOriginalMessage[i];//No alphabet shifts
}
}
else {
continue;
}
break;
}
}
return aOriginalMessage;
}
以上是我的面向对象实现的三个代码文件:Testing(main)、Header和source .cpp文件。
对我来说很突出的一个问题是您检查消息的当前字符
if (aOriginalMessage[i] == fLowerCase[j] || aOriginalMessage[i] == fUpperCase[j])
可能在您的轮班更改后。
aOriginalMessage[i]++;
因为您正在遍历所有可能的字符。您可能希望将加密的消息保存在不同的变量中。
另外,先移位整个字母,然后直接为加密消息分配移位的字符可能更有效。
希望对您有所帮助。
下面是我测试过的 C++ 代码,我再次重复它的加密和解密工作....意味着逻辑没问题...
#include<iostream>
#include<string>
using namespace std;
string Encrypt(string&, int &);// Function Prototype. This declares Encrypt to be a function that needs one variable as string and another as interger as arguments. Reference operator & in prototype
string Decrypt(string&, int &);//Function Prototype. This declares Decrypt to be a function that needs one variable as string and another as interger as arguments. Reference operator & in prototype
int main()
{
string text;//the string that holds the user input
int key;//key holds the number by which the user wants the alphabets to be shifted
cout << "Enter your phrase: " << endl;
getline(cin, text);//gets the user input ( including spaces and saves it to the variable text)
cout << "Please choose a number(key) for which you wants the alphabets to be shifted: " << endl;
/*Note: the key can either be positive (forward shifting), negative (backward shifting) or zero (no shifting)*/
cin >> key;//User input for number by which alphabets are going to be shifted
string kolo = Encrypt(text, key);
cout << "Encrypted Message: " << kolo << endl;//Function call to display encrypted message
//Decrypt(Encrypt(text, key), key);
cout << "Decrypted Message: " << Decrypt(kolo, key)<< endl;//Function call to display decrypted message
//system("Pause");
return 0;
}
string Encrypt(string& Source, int& c)
{
char fUpperCase[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' };//Initialization of class member
char fLowerCase[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' };//Initialization of class member
int z;//z holds the value (int) of the length of the user input ( including spaces)
z = (int)Source.length(); /*give the variable z the value of the user input length and length is normally an unsigned long integer hence
we have to typecast it*/
/*counter that makes it keep looping until it "encrypts" all of the user input (that's why it keeps looping while its less than z)*/
for (int i = 0; i < z; i++)
{
for (int j = 0; j < 26; j++)
{
if (Source[i] == fLowerCase[j] || Source[i] == fUpperCase[j])
{
//text[i] = fLowerCase[j];
if (c > 0)
{
//another counter that loops forwards key times by incrementing method
for (int counter = 0; counter < c; counter++)
{
/*it checks if the letter text[x] is 'z' and if it is 'z' it will make it 'a'*/
if (Source[i] == 'z')
{
Source[i] = 'a';
}
else if (Source[i] == 'Z')
{
Source[i] = 'A';
}
else
{
Source[i]++;
}
}
}
else if (c < 0)
{
//another counter that loops backwards key times by decrementing method
for (int counter = 0; counter < abs(c); counter++)
{
/*it checks if the letter text[x] is 'a' and if it is 'a' it will make it 'z'*/
if (Source[i] == 'a')
{
Source[i] = 'z';
}
else if (Source[i] == 'A')
{
Source[i] = 'Z';
}
else
{
Source[i]--;
}
}
}
else
{
Source[i];//No alphabet shifts
}
}
else {
continue;
}
break;
}
}
return Source;
}
string Decrypt(string& EncryptedMessage, int& c)
{
int y;//z holds the value (int) of the length of the user input ( including spaces)
y = (int)EncryptedMessage.length(); /*give the variable z the value of the user input length and length is normally an unsigned long integer hence
we have to typecast it*/
for (int i = 0; i < y; i++)
{
if (isalpha(EncryptedMessage[i]))//verify if the all members of the encrypted message is alphabet if not the same special character is return whereas if it is alphabet it is eitther incremented or decremented
{
if (c > 0)
{
//another counter that loops forwards key times by incrementing method
for (int counter = 0; counter < c; counter++)
{
/*it checks if the letter text[x] is 'a' and if it is 'z' it will make it 'a'*/
if (EncryptedMessage[i] == 'a')
{
EncryptedMessage[i] = 'z';
}
else if (EncryptedMessage[i] == 'A')
{
EncryptedMessage[i] = 'Z';
}
else
{
EncryptedMessage[i]--;
}
}
}
else if (c < 0)
{
//another counter that loops backwards key times by decrementing method
for (int counter = 0; counter < abs(c); counter++)
{
//it checks if the letter text[x] is 'a' and if it is 'a' it will make it 'z'
if (EncryptedMessage[i] == 'z')
{
EncryptedMessage[i] = 'a';
}
else if (EncryptedMessage[i] == 'Z')
{
EncryptedMessage[i] = 'A';
}
else
{
EncryptedMessage[i]++;
}
}
}
else
{
EncryptedMessage[i];//No alphabet shifts
}
}
//cout << "Decrypted Message: " << EncryptedMessage << endl;//Function call to display decrypted message
}
return EncryptedMessage;
}
将构造函数代码更改为:
CyclicShift::CyclicShift()
{
static char const fUpper[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' };//Initialization of class member
static char const fLower[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' };//Initialization of class member
strcpy_s(fUpperCase, fUpper);
strcpy_s(fLowerCase, fLower);
}
为了避免缓冲区溢出,我将 char 数组的长度更改为 27。
char fUpperCase[27];//A-Z
char fLowerCase[27];//a-z
您应该将fUpperCase
和fLowerCase
声明为static
并在头文件中初始化它们。在您的代码中,您初始化的变量是构造函数的本地变量,而私有成员保持不变。
编辑
如果你必须在构造函数中初始化它们,那么,你可以这样做:
class CyclicShift
{
private:
char fUpperCase[26]; // A-Z
char fLowerCase[26]; // a-z
// ... etc. ...
}
CyclicShift::CyclicShift()
{
for ( int i = 0; i < 26; ++i) {
fUpperCase[i] = 'A' + i;
fLowerCase[i] = 'a' + i;
}
}
我一直在使用面向对象编程实现凯撒密码。问题是,当我调用加密函数时,我得到与用户输入相同的消息...消息未正确加密。
例如,如果我写 "abc" 而不是 "bcd",我会得到 "abc"。我在 C++ 中尝试过,代码可以正常工作(逻辑很好),但我尝试在 C++ 中使用面向对象的编程并创建了三个文件来分隔代码。
任何人都可以帮助或指出我可能犯的任何错误吗?
可执行测试文件(main
函数):
#include <iostream>
#include <string>
#include "CyclicShift.h"
using namespace std;
int main()
{
string text;//the string that holds the user input
int key;//key holds the number by which the user wants the alphabets to be shifted
cout << "Enter your phrase: " << endl;
getline(cin, text);//gets the user input ( including spaces and saves it to the variable text)
cout << "Please choose a number(key) for which you wants the alphabets to be shifted: " << endl;
/*Note: the key can either be positive (forward shifting), negative (backward shifting) or zero (no shifting)*/
cin >> key;//User input for number by which alphabets are going to be shifted
const CyclicShift aShift;
cout << "Encrypted Message : " << aShift.Encrypt(text, key) << endl;
//system("Pause");
return 0;
}
头文件.h
:
#pragma once
#include <iostream>
#include<string>
//Note: No using "namespace std;" in header files
class CyclicShift
{
private:
char fUpperCase[26];//A-Z
char fLowerCase[26];//a-z
public:
CyclicShift();
std::string& Encrypt(std::string& aOriginalMessage, int &aKey) const;// Function Prototype. This declares Encrypt to be a function that needs one string and one integer variables as arguments. Reference operator & in prototype
};
源文件.cpp
:
#include "CyclicShift.h"
#include<iostream>
#include<string>
using namespace std;
CyclicShift::CyclicShift()
{
char fUpperCase[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'};//Initialization of class member
char fLowerCase[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'};//Initialization of class member
}
string& CyclicShift::Encrypt(string& aOriginalMessage, int & aKey) const
{
int z;//z holds the value (int) of the length of the user input ( including spaces)
z = (int)aOriginalMessage.length(); /*give the variable z the value of the user input length and length is normally an unsigned long integer hence
we have to typecast it*/
/*counter that makes it keep looping until it "encrypts" all of the user input (that's why it keeps looping while its less than z)*/
for (int i = 0; i < z; i++)
{
for (int j = 0; j < 26; j++)
{
if (aOriginalMessage[i] == fLowerCase[j] || aOriginalMessage[i] == fUpperCase[j])
{
if (aKey > 0)
{
//another counter that loops forwards key times by incrementing method
for (int counter = 0; counter < aKey; counter++)
{
/*it checks if the letter text[x] is 'z' and if it is 'z' it will make it 'a'*/
if (aOriginalMessage[i] == 'z')
{
aOriginalMessage[i] = 'a';
}
else if (aOriginalMessage[i] == 'Z')
{
aOriginalMessage[i] = 'A';
}
else
{
aOriginalMessage[i]++;
}
}
}
else if (aKey < 0)
{
//another counter that loops backwards key times by decrementing method
for (int counter = 0; counter < abs(aKey); counter++)
{
/*it checks if the letter text[x] is 'a' and if it is 'a' it will make it 'z'*/
if (aOriginalMessage[i] == 'a')
{
aOriginalMessage[i] = 'z';
}
else if (aOriginalMessage[i] == 'A')
{
aOriginalMessage[i] = 'Z';
}
else
{
aOriginalMessage[i]--;
}
}
}
else
{
aOriginalMessage[i];//No alphabet shifts
}
}
else {
continue;
}
break;
}
}
return aOriginalMessage;
}
以上是我的面向对象实现的三个代码文件:Testing(main)、Header和source .cpp文件。
对我来说很突出的一个问题是您检查消息的当前字符
if (aOriginalMessage[i] == fLowerCase[j] || aOriginalMessage[i] == fUpperCase[j])
可能在您的轮班更改后。
aOriginalMessage[i]++;
因为您正在遍历所有可能的字符。您可能希望将加密的消息保存在不同的变量中。
另外,先移位整个字母,然后直接为加密消息分配移位的字符可能更有效。
希望对您有所帮助。
下面是我测试过的 C++ 代码,我再次重复它的加密和解密工作....意味着逻辑没问题...
#include<iostream>
#include<string>
using namespace std;
string Encrypt(string&, int &);// Function Prototype. This declares Encrypt to be a function that needs one variable as string and another as interger as arguments. Reference operator & in prototype
string Decrypt(string&, int &);//Function Prototype. This declares Decrypt to be a function that needs one variable as string and another as interger as arguments. Reference operator & in prototype
int main()
{
string text;//the string that holds the user input
int key;//key holds the number by which the user wants the alphabets to be shifted
cout << "Enter your phrase: " << endl;
getline(cin, text);//gets the user input ( including spaces and saves it to the variable text)
cout << "Please choose a number(key) for which you wants the alphabets to be shifted: " << endl;
/*Note: the key can either be positive (forward shifting), negative (backward shifting) or zero (no shifting)*/
cin >> key;//User input for number by which alphabets are going to be shifted
string kolo = Encrypt(text, key);
cout << "Encrypted Message: " << kolo << endl;//Function call to display encrypted message
//Decrypt(Encrypt(text, key), key);
cout << "Decrypted Message: " << Decrypt(kolo, key)<< endl;//Function call to display decrypted message
//system("Pause");
return 0;
}
string Encrypt(string& Source, int& c)
{
char fUpperCase[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' };//Initialization of class member
char fLowerCase[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' };//Initialization of class member
int z;//z holds the value (int) of the length of the user input ( including spaces)
z = (int)Source.length(); /*give the variable z the value of the user input length and length is normally an unsigned long integer hence
we have to typecast it*/
/*counter that makes it keep looping until it "encrypts" all of the user input (that's why it keeps looping while its less than z)*/
for (int i = 0; i < z; i++)
{
for (int j = 0; j < 26; j++)
{
if (Source[i] == fLowerCase[j] || Source[i] == fUpperCase[j])
{
//text[i] = fLowerCase[j];
if (c > 0)
{
//another counter that loops forwards key times by incrementing method
for (int counter = 0; counter < c; counter++)
{
/*it checks if the letter text[x] is 'z' and if it is 'z' it will make it 'a'*/
if (Source[i] == 'z')
{
Source[i] = 'a';
}
else if (Source[i] == 'Z')
{
Source[i] = 'A';
}
else
{
Source[i]++;
}
}
}
else if (c < 0)
{
//another counter that loops backwards key times by decrementing method
for (int counter = 0; counter < abs(c); counter++)
{
/*it checks if the letter text[x] is 'a' and if it is 'a' it will make it 'z'*/
if (Source[i] == 'a')
{
Source[i] = 'z';
}
else if (Source[i] == 'A')
{
Source[i] = 'Z';
}
else
{
Source[i]--;
}
}
}
else
{
Source[i];//No alphabet shifts
}
}
else {
continue;
}
break;
}
}
return Source;
}
string Decrypt(string& EncryptedMessage, int& c)
{
int y;//z holds the value (int) of the length of the user input ( including spaces)
y = (int)EncryptedMessage.length(); /*give the variable z the value of the user input length and length is normally an unsigned long integer hence
we have to typecast it*/
for (int i = 0; i < y; i++)
{
if (isalpha(EncryptedMessage[i]))//verify if the all members of the encrypted message is alphabet if not the same special character is return whereas if it is alphabet it is eitther incremented or decremented
{
if (c > 0)
{
//another counter that loops forwards key times by incrementing method
for (int counter = 0; counter < c; counter++)
{
/*it checks if the letter text[x] is 'a' and if it is 'z' it will make it 'a'*/
if (EncryptedMessage[i] == 'a')
{
EncryptedMessage[i] = 'z';
}
else if (EncryptedMessage[i] == 'A')
{
EncryptedMessage[i] = 'Z';
}
else
{
EncryptedMessage[i]--;
}
}
}
else if (c < 0)
{
//another counter that loops backwards key times by decrementing method
for (int counter = 0; counter < abs(c); counter++)
{
//it checks if the letter text[x] is 'a' and if it is 'a' it will make it 'z'
if (EncryptedMessage[i] == 'z')
{
EncryptedMessage[i] = 'a';
}
else if (EncryptedMessage[i] == 'Z')
{
EncryptedMessage[i] = 'A';
}
else
{
EncryptedMessage[i]++;
}
}
}
else
{
EncryptedMessage[i];//No alphabet shifts
}
}
//cout << "Decrypted Message: " << EncryptedMessage << endl;//Function call to display decrypted message
}
return EncryptedMessage;
}
将构造函数代码更改为:
CyclicShift::CyclicShift()
{
static char const fUpper[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' };//Initialization of class member
static char const fLower[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' };//Initialization of class member
strcpy_s(fUpperCase, fUpper);
strcpy_s(fLowerCase, fLower);
}
为了避免缓冲区溢出,我将 char 数组的长度更改为 27。
char fUpperCase[27];//A-Z
char fLowerCase[27];//a-z
您应该将fUpperCase
和fLowerCase
声明为static
并在头文件中初始化它们。在您的代码中,您初始化的变量是构造函数的本地变量,而私有成员保持不变。
编辑
如果你必须在构造函数中初始化它们,那么,你可以这样做:
class CyclicShift
{
private:
char fUpperCase[26]; // A-Z
char fLowerCase[26]; // a-z
// ... etc. ...
}
CyclicShift::CyclicShift()
{
for ( int i = 0; i < 26; ++i) {
fUpperCase[i] = 'A' + i;
fLowerCase[i] = 'a' + i;
}
}