RSS
content bg

Hubungan antara posisi numerik karakter ciphertext (C) dan karakter plaintext (P) dapat diekspresikan sebagai berikut C = (P + 5)mod 26

#include <cstdlib>
#include <iostream>
using namespace std;
void Check(char &);
void FindKey(char, const char[],int &);
void FormCipher(const char[],char[],int);
void Print(const char[],const char[]);
int main(){
char PLAINTEXT[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'};
char CIPHERTEXT[26], key;
int key_location;
cout << "Masukan Alphabet huruf besar: ";
Halaman | 20
cin >> key;
Check(key);
FindKey(key, PLAINTEXT, key_location);
FormCipher(PLAINTEXT, CIPHERTEXT, key_location);
Print(PLAINTEXT, CIPHERTEXT);
system("PAUSE");
return EXIT_SUCCESS;
}
void Check(char &key){
int key_value;
bool error=false;
do{
key_value=static_cast<int>(key);
if((key_value<65)||(key_value>90))
{
cerr<<"\nMohon masukan alphabet kapital besar (A-Z):";
cin>>key;
}else{
error=true;
}
}while(!(error));
key=static_cast<char>(key_value);
cout<<"\nKey: "<<key<<endl;
return;
}
void FindKey(char key,const char PLAINTEXT[],int &key_location){
for(int x=0;x<26;x++){
if(key==PLAINTEXT[x]){
key_location=x+1;
break;
}
}
return;
}
void FormCipher(const char PLAINTEXT[],char CIPHERTEXT[],int loc){
for(int x=0;x<26;x++){
CIPHERTEXT[x]=PLAINTEXT[(x+loc)%26];
}
return;
}
void Print(const char PLAINTEXT[],const char CIPHERTEXT[]){
cout<<"PLAINTEXT : ";
for(int i=0;i<26;i++)
cout<<PLAINTEXT[i]<<" ";
cout<<"\nCIPHERTEXT: ";
for(int i=0;i<26;i++)
cout<<CIPHERTEXT[i]<<" ";
cout<<endl;
return;
}
Masukan Alphabet huruf besar: k
Mohon masukan alphabet kapital besar (A-Z):K
Key: K
PLAINTEXT : 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
CIPHERTEXT: L M N O P Q R S T U V W X Y Z A B C D E F G H I J K

0 komentar:

Posting Komentar