변형 시저

언어/c++2016. 2. 8. 22:58
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <ctime>
using namespace std;

typedef int KEY;
typedef string CIPHER_TEXT;

class CEASAR{
public:
string plaintext;
string ciphertext;
int secretKey_upper;
int secretKey_lower;
public:
CEASAR(); //[1]
void INPUT_PLAINTEXT(); //[2]
KEY SECRET_CEASAR_KEY_UPPER(); //[3]
KEY SECRET_CEASAR_KEY_LOWER(); //[4]
CIPHER_TEXT ENCRYPT_CEASAR(string, int, int); //[5]

};
CEASAR::CEASAR() { //[1]
this->plaintext = " ";
this->ciphertext = " ";
this->secretKey_upper = 0;
this->secretKey_lower = 0;
}
void CEASAR::INPUT_PLAINTEXT() { //[2]
cout << " [1] plaint text is ";
getline(cin, this->plaintext);
}
KEY CEASAR::SECRET_CEASAR_KEY_UPPER() { //[3]
cout << " [2] secret_key_upper_return "<<endl;
int temp_key = 0;
temp_key = rand()%25 + 1; // key range: 1,2,3,4,5,...,25
return temp_key;
}
KEY CEASAR::SECRET_CEASAR_KEY_LOWER() { //[4]
cout << " [3] secret_key_lower_return "<<endl;
int temp_key = 0;
temp_key = rand()%25 + 1; // key range: 1,2,3,4,5,...,25
return temp_key;
}
CIPHER_TEXT CEASAR::ENCRYPT_CEASAR(string plaintext, int secret_key_u, int secret_key_l) { //[5]
cout << " [4] ciphter text is ";
CIPHER_TEXT temp_cipher_text=" ";
int increase_upper = 1;
int increase_lower = 1;

for(int index = 0; index<plaintext.length(); index++){
if (isupper(plaintext[index])) {
// case1: A, B, C, D, E, ..., Z
temp_cipher_text += ((((plaintext[index] - 65) + secret_key_u + increase_upper) % 26) + 65);
increase_upper++;
}
else if (islower(plaintext[index])) {
// case2: a, b, c, d, e, ..., z
temp_cipher_text += ((((plaintext[index] - 97) + secret_key_l + increase_lower) % 26) + 97);
increase_lower++;
}
else {
// case3:
temp_cipher_text += plaintext[index];
}
}
return temp_cipher_text;
}
int main(void)
{
srand((unsigned int)time(NULL));
cout<<"---------------------------------------"<<endl;
cout<<"a b c d e ... z"<<endl;
cout<<"int('a') is "<<int('a')<<endl;
cout<<"..."<<endl;
cout<<"int('z') is "<<int('z')<<endl;
cout<<"---------------------------------------"<<endl;
cout<<"A B C D E ... Z"<<endl;
cout<<"int('A') is "<<int('A')<<endl;
cout<<"..."<<endl;
cout<<"int('Z') is "<<int('Z')<<endl;
cout<<"---------------------------------------"<<endl;
CEASAR cea;

//[1] plainText
cea.INPUT_PLAINTEXT();
cout << "plaintext is "<<"[ "<<cea.plaintext<<" ]"<<endl;

//[2] secretKey
cea.secretKey_upper = cea.SECRET_CEASAR_KEY_UPPER();
cout << "secretKey_upper is "<<"[ "<<cea.secretKey_upper<<" ]"<<endl;
cea.secretKey_lower = cea.SECRET_CEASAR_KEY_LOWER();
cout << "secretKey_lower is "<<"[ "<<cea.secretKey_lower<<" ]"<<endl;

//[3] cipherText
cea.ciphertext = cea.ENCRYPT_CEASAR(cea.plaintext, cea.secretKey_upper, cea.secretKey_lower);
cout <<"[ "<<cea.ciphertext<<" ]"<<endl;
return 0;
}


'언어 > c++' 카테고리의 다른 글

c++ 문자열 파싱, substr  (0) 2017.07.02
c++ 동적할당  (0) 2017.07.01
c++ 행렬  (0) 2016.05.21
각 자리 숫자 더하기  (0) 2016.03.23
stu1  (0) 2016.02.25