Algoritma :
{ Mengenskripsi karakter yang diinput dan mendeskripsikannya kembali }
Deklarasi
key : integer
plain, chipper, teks : char
Deskripsi
read(key,plain)
for i <= 0 to strlen(plain) do
plain[i](int (plain[i]))
chipper[i] = (plain[i] + key) % 128
for i<= 0 to strlen(chiper) do
teks[i] = (chiper[i] – key) % 128
endfor
endfor
write(chipper,teks)
Berikut kode programnya :
#include <cstdlib>
#include <iostream>
using namespace std;
class geser{
public:
friend istream& operator>>(istream&, geser&);
friend ostream& operator<<(ostream&, geser&);
void enkripsi();
void deskripsi();
private:
char plain[128], chiper[128], teks[26];
int key;
};
istream& operator>>(istream& in, geser& a){
cout<<"Masukkan key = ";
in>>a.key;
cout<<"Masukkan karakter = ";
in>>a.plain;
return in;
}
void geser::enkripsi(){
for(int i=0; i<strlen(plain); i++){
cout<<plain[i]<<"("<<int(plain[i])<<")";
chiper[i]=(plain[i]+key)%128;
}
}
void geser::deskripsi(){
for (int i=0; i<strlen(chiper); i++){
teks[i]=(chiper[i]-key)%128;
}
}
ostream& operator<<(ostream& out, geser& b){
out<<"Hasil Enkripsi :"<<endl;
for(int i=0; i<strlen(b.plain); i++){
out<<b.chiper[i];
}
out<<endl;
out<<"Hasil Deskripsi :"<<endl;
for(int j=0; j<strlen(b.chiper); j++){
cout<<b.teks[j];
}
return out;
}
int main(int argc, char *argv[])
{
geser x;
cin>>x;
x.enkripsi();
x.deskripsi();
cout<<endl;
cout<<x;
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
In the above program is a program that is used for encryption and description of a character. With this program we can use it mengenkrips code that we make. Which one has the following formula:
Enskripsi: cipher = (plain + key)% 128
Deskripsi: teks = (cipher - key)% 128
In the above program using this type of for loop, to input the key and the characters will be encrypted and to describe again.
Enskripsi: cipher = (plain + key)% 128
Deskripsi: teks = (cipher - key)% 128
In the above program using this type of for loop, to input the key and the characters will be encrypted and to describe again.
0 komentar:
Posting Komentar