You are on page 1of 4

Dida Prasetyo Rahmat

05311940000019
Teknologi Informasi

Melakukan Enkripsi dan Dekripsi


Menggunakan Bahasa pemrograman C++, saya menemukan program untuk melakukan
enkripsi dan dekripsi Caesar cipher. Dengan memasukkan kata yang ingin di
dekripsi/enkripsi

Code
#include<iostream>
#include<string.h>
using namespace std;

int main() {
a:
cout<<"Enter the message:\n";
char msg[100];
cin >> msg;
int i, j, length,choice,key;
cout << "Enter key: ";
cin >> key; //take the key as input
length = strlen(msg);
b:
cout<<"\nMenu \n1. Encryption \n2. Decryption \n3. Masukkan kata lain \n4. Keluar
\nMasukkan pilihan anda : ";
cin>>choice;
if (choice==1){
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
//encrypt for lowercase letter
if (ch >= 'a' && ch <= 'z'){
ch = ch + key;
if (ch > 'z') {
ch = ch - 'z' + 'a' - 1;
}
msg[i] = ch;
}
//encrypt for uppercase letter
else if (ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if (ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
msg[i] = ch;
}
}
printf("\n\nEncrypted message: %s\n", msg);
goto b;
}
else if (choice == 2) { //for decryption
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
//decrypt for lowercase letter
if(ch >= 'a' && ch <= 'z') {
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
msg[i] = ch;
}
//decrypt for uppercase letter
else if(ch >= 'A' && ch <= 'Z') {
ch = ch - key;
if(ch < 'A') {
ch = ch + 'Z' - 'A' + 1;
}
msg[i] = ch;
}
}
cout << "\n\nDecrypted message: " << msg << "\n";
goto b;
}
else if (choice == 3){
system("cls");
goto a;
}
else if (choice == 4){
return 0;
}
}

You might also like