Вы находитесь на странице: 1из 14

FULL VIGENERE using namespace std;

#include <iostream> char teks[1000], cipher[36],


#include <string> enkrip[1000], dekrip[1000];
using namespace std; int key[1000];
string e, d;
class FullVigenere{
public: int findIndex(char ch)
string key; {int x;
FullVigenere(string key) for (int i = 0; i < 26; i++)
{ for (int i = 0; i < key.size(); ++i) {if (cipher[i] == ch)
{ if (key[i] >= 'A' && key[i] <= 'Z') {x = i;
this->key += key[i]; i = 26;}}
else if (key[i] >= 'a' && key[i] <= 'z') return x;}
this->key += key[i] + 'A' - 'a';}}
void table(string k, string s)
string encrypt(string text){ {int i, l = 0, x;
string out; for (i = 0; i < 26; i++){
for (int i = 0, j = 0; i < cipher[l] = i + 97;
text.length(); ++i) cout << cipher[l];
{ char c = text[i]; l++;}
if (c >= 'a' && c <= 'z') cout << endl;
c += 'A' - 'a'; cout << "Key : " << endl;
else if (c < 'A' || c > 'Z') for (i = 0; i < s.size(); i++)
continue; {x = findIndex(k[i]);
out += (c + key[j] - 2 * 'A') % 26 + key[i] = x;
'A'; cout << k[i];}
j = (j + 1) % key.length(); cout << endl;}
} return out;}
int encrypt(string s, string k)
string decrypt(string text) {int i, l = 0, x, y = 0;
{string out; for (i = 0; i < s.size(); i++)
for (int i = 0, j = 0; i < {if (s[i] > 96 && s[i] < 123)
text.length(); ++i) {x = findIndex(s[i]);
{char c = text[i]; enkrip[i] = cipher[(key[y] + x) % 26];
if (c >= 'a' && c <= 'z') y++;
c += 'A' - 'a'; if (y > k.size()){
else if (c < 'A' || c > 'Z') y = 0;}}
continue; e += enkrip[i];
out += (c - key[j] + 26) % 26 + 'A'; cout << enkrip[i];}
j = (j + 1) % key.length(); return i;}
}
return out; void decrypt(char s[], string k, int j)
}}; {int i, l = 0, x, z, y = 0;
for (i = 0; i < j; i++){
int main() if (s[i] > 96 && s[i] < 123)
{FullVigenere {x = findIndex(s[i]);
cipher("FULLVIGENERECIPHER"); z = x - key[y];
if (z < 0)
string original = "Kriptgrafi Kelas A"; {z = 26 + z;}
string encrypted = dekrip[i] = cipher[z];
cipher.encrypt(original); y++;
string decrypted = if (y > k.size())
cipher.decrypt(encrypted); {y = 0;}}
d += dekrip[i];
cout << original << endl; cout << dekrip[i];}
cout << "Encrypted: " << encrypted << return;}
endl;
cout << "Decrypted: " << decrypted << int main()
endl;} {string s, k;
ofstream ciphertext;
AUTO KEY VIGENERE ciphertext.open("ciphertext.txt",
ios::app);
#include <iostream> cout << "=========================
#include <string> KELOMPOK 5 ===========================";
#include <fstream> cout << "\n=================== AUTOKEY
VIGENERE CIPHER ====================";
cout << "\n1. DIMAS CHAERUL EKTY if ((int)key[j] == 32)
SAPUTRA 1600018005"; j = j + 1;
cout << "\n2. HELTY if((int)plaintext[i] == 32)
ELTIANDA i = i + 1;
1600018038";
cout << "\n3. QORI AULIA if((int)plaintext[i] >= 65 &&
ROBIN 1600018049"; (int)plaintext[i] <= 90)
ciphertext << ch = (((int)plaintext[i] - 65) +
"\n\n========================= KELOMPOK ((int)key[j] - 65)) % 26;
5 ==========================="; else
ciphertext << "\n=================== ch = (int)plaintext[i] - 65;
AUTOKEY VIGENERE CIPHER if((int)plaintext[i] != 32)
===================="; ciphertext.append(string(1, (char)(ch +
ciphertext << "\n1. DIMAS CHAERUL EKTY 65)));}}
SAPUTRA 1600018005";
ciphertext << "\n2. HELTY void decrypt_vigenere(const string&
ELTIANDA ciphertext, const string& key ,string&
1600018038"; plaintext){
ciphertext << "\n3. QORI AULIA int i, j, ch;
ROBIN 1600018049"; for(i = 0, j = 0; i <
cout << "\n\nMasukkan Plaintext : "; ciphertext.length(); ++i, j++) {
getline(cin, s); if(j >= key.length())
cout << "Masukkan Key : "; j = 0;
cout << "Masukkan Key : "; if ((int)key[j] == 32)
cin >> k; j = j + 1;
ciphertext << "\nPlaintext : " << s; if((int)ciphertext[i] >= 65 &&
ciphertext << "\nKey : " << k; (int)ciphertext[i] <= 90)
int i, j, f = 0; ch = (((int)ciphertext[i] - 65) + 26 -
for (i = 0; i < s.size(); i++) ((int)key[j] - 65)) % 26;
{if (s[i] > 96 && s[i] < 123) else
{k.append(s, i, 1);}} ch = (int)ciphertext[i] - 65;
table(k, s); if((int)ciphertext[i] != 32)
cout << "\nHasil Enkripsi : "; plaintext.append(string(1, (char)(ch +
j = encrypt(s, k); 65)));}}
ciphertext << "\nEnkripsi : " << e;
cout << "\nHasil Dekripsi : "; int main(int argc, char* argv[]){
decrypt(enkrip, k, j); cout << "Masukan plaintext :\n";
ciphertext << "\nDekripsi : " << d; char plaintext[256] = {0};
ciphertext << char key[256] = {0};
"\n\n=================================== cin.getline (plaintext,256);
=========================="; char pil;
ciphertext.close(); cout << "Text yang akan di encrypt: " <<
return 0;} plaintext << endl;
cin.clear();
cout << "Masukkan kunci :";
RUNNING KEY cin.getline (key, 256);
if(strlen(key)<strlen(plaintext)){
#include <iostream> cout<<"Plaintext tidak boleh lebih dari
#include <string> key";
#include <algorithm> return 0;}
#include <cstring> transform(key, key+strlen(key), key,
#include <cctype> to_upper());
cout << "Kunci : " << key << endl;
using namespace std; string cipher;
transform(plaintext,
struct to_upper { plaintext+strlen(plaintext), plaintext,
int operator() (int ch){ to_upper());
return toupper(ch);}}; encrypt_vigenere(plaintext, key,
cipher);
void encrypt_vigenere(const string& string decrypted;
plaintext, const string& key, string& decrypt_vigenere(cipher, key,
ciphertext){ decrypted);
int i, j, ch, poi; cout << "Vigenere encrypted : " <<
for(i = 0, j = 0; i < cipher << "\ndecrypted: " << decrypted
plaintext.length(); ++i, j++) { << endl;
if(j >= key.length()) return 0;}
j = 0;
PLAYFAIR if(S==0) cout<<grid[R][4];
else cout<<grid[R][S-1];}
#include<bits/stdc++.h> else if(Q==S ) // kolom{
using namespace std; if(P==0) cout<<grid[4][Q];
else cout<<grid[P-1][Q];
char grid[5][5]; // 5x5 matrik untuk if(R==0) cout<<grid[4][S];
enkripsi dan dekripsi else cout<<grid[R-1][S];}
char keyword[26]; // key else{
char msg[100]; // pesan cout<<grid[P][S]<<grid[R][Q];}}
int mark[130],len,r,c; // variabel cout<<endl<<endl;
system("PAUSE");
void createGrid(); menu();}
void showGrid(); void encipher(){
void encipher(); initialize();
void decipher(); createGrid();
void initialize(); cout<<"Pesan yang diekripsi: ";
void menu(){ gets(msg);
system("CLS"); int l=strlen(msg); // menghitung jumlah
int n; string atau karakter
string op[]={"1. Enkripsi","2. char reqText[150];
Dekripsi","3. Exit"}; int in=0,j=0;
cout<<op[0]<<endl<<op[1]<<endl<<op[2]<<e for(int i=0;i<l;i++){
ndl<<"Pilihan: "; j=i+1;
cin>>n; if(msg[i]==' ') //abaikan spasi pada
if(n==1) encipher(); // jika memilih string{
enkripsi i++;
else if(n==2) decipher(); // jika j++;}
memmilih dekripsi if(msg[j]==' ') j++; //abaikan spasi
else exit(1);} if(toupper(msg[i])=='J') msg[i]='i'; //
void initialize(){ abaikan huruf J
memset(mark,0,sizeof(mark)); if(toupper(msg[i])==toupper(msg[j])) //
system("CLS");} jika ada huruf yang sama, tambahkan Z
int main(){ setelah huruf pertama yang sama
menu(); {reqText[in]=toupper(msg[i]);
return 0;} reqText[in+1]='Z';
void decipher(){ in++;}
initialize(); else{
createGrid(); reqText[in]=toupper(msg[i]);}
cout<<"Ciphertext: "; in++;}
char cypText[150]; // ciphertext if(in%2!=0) reqText[in]='Z'; // jika
cin>>cypText; terdapat karakter ganjil maka tambahkan
getchar(); Z diakhir
int l=strlen(cypText); // menghitung
jumlah string atau karakter cout<<"Cypher text: ";
int P,Q,R,S,f1,f2;
cout<<"Dekripsi text: "; char x,y;
int P,Q,R,S,f1,f2; for(int i=0;i<in;i+=2){
char x,y; x=reqText[i];
for(int i=0;i<l;i+=2){ y=reqText[i+1];
x=cypText[i]; f1=f2=0;
y=cypText[i+1]; for(int j=0;j<5;j++){
f1=f2=0; for(int k=0;k<5;k++){
for(int j=0;j<5;j++){ if(x==grid[j][k]){
for(int k=0;k<5;k++){ P=j;
if(x==grid[j][k]){ Q=k;
P=j; f1=1;}
Q=k; if(y==grid[j][k]){
f1=1;} R=j;
if(y==grid[j][k]){ S=k;
R=j; f2=1;}
S=k; if(f1 && f2) break;}
f2=1;} if(f1 && f2) break;}
if(f1 && f2) break;} if(P==R) // baris
if(f1 && f2) break;} {if(Q==4) cout<<grid[P][0];
if(P==R) // baris{ else cout<<grid[P][Q+1];
if(Q==0) cout<<grid[P][4]; if(S==4) cout<<grid[R][0];
else cout<<grid[P][Q-1]; else cout<<grid[R][S+1];}
else if(Q==S ) // kolom if( getCharPos( *ti, c, d ) )
{if(P==4) cout<<grid[0][Q]; {if( a == c ) { ntxt += getChar( a,
else cout<<grid[P+1][Q]; b + dir ); ntxt += getChar( c, d + dir
if(R==4) cout<<grid[0][S]; ); }
else cout<<grid[R+1][S]; else if( b == d ){ ntxt += getChar( a +
}else{ dir, b ); ntxt += getChar( c + dir, d );
cout<<grid[P][S]<<grid[R][Q];} }
}cout<<endl<<endl; else { ntxt += getChar( c, b
system("PAUSE"); ); ntxt += getChar( a, d ); }
menu();} }}
void createGrid(){ _txt = ntxt;
cout<<"Keyword: "; }
cin>>keyword; // kunci
getchar(); void display(){
len=strlen(keyword); //menghitung cout << "\n\n OUTPUT:\n=========" <<
jumlah string atau karakter endl;
mark['J']=1; // abaikan J string::iterator si = _txt.begin(); int
r=0,c=0; //inisialisasi baris dan kolom cnt = 0;
while( si != _txt.end() )
for(int i=0;i<len;i++){ {cout << *si; si++; cout << *si << " ";
if(!mark[toupper(keyword[i])]) // si++;
abaikan duplikat if( ++cnt >= 26 ) cout << endl, cnt = 0;
{mark[toupper(keyword[i])]=1; }cout << endl << endl; }
grid[r][c++]=toupper(keyword[i]);
if(c%5==0){ char getChar( int a, int b )
c=0; {return _m[ (b + 5) % 5 ][ (a + 5) % 5
r++;}}} ]; }

for(int i='A';i<='Z';i++) bool getCharPos( char l, int &a, int &b


{if(mark[i]==0) )
{grid[r][c++]=i; {for( int y = 0; y < 5; y++ )
mark[i]=1; for( int x = 0; x < 5; x++ )
if(c%5==0) if( _m[y][x] == l )
{if(r==4 && c==5) break; { a = x; b = y; return true; }
r++; return false;}
c=0;}}}}
void showGrid(){ void getTextReady( string t, bool ij,
cout<<"5x5 Matrix"<<endl; bool e )
//show grid {for( string::iterator si = t.begin();
for(int i=0;i<5;i++) si != t.end(); si++ )
{for(int j=0;j<5;j++) {*si = toupper( *si ); if( *si < 65 ||
{cout<<grid[i][j]<<" ";} *si > 90 ) continue;
cout<<endl;}} if( *si == 'J' && ij ) *si = 'I';
else if( *si == 'Q' && !ij ) continue;
_txt += *si;
PLAYFAIR }if( e )
{string ntxt = ""; size_t len =
#include <iostream> _txt.length();
#include <string> for( size_t x = 0; x < len; x += 2 )
{ntxt += _txt[x];
using namespace std; if( x + 1 < len )
{if( _txt[x] == _txt[x + 1] ) ntxt +=
class playfair{ 'X';
public: ntxt += _txt[x + 1];}
void doIt( string k, string t, bool ij, }_txt = ntxt;
bool e ){ }if( _txt.length() & 1 ) _txt += 'X';}
createGrid( k, ij ); getTextReady( t,
ij, e ); void createGrid( string k, bool ij )
if( e ) doIt( 1 ); else doIt( -1 ); {if( k.length() < 1 ) k = "KEYWORD";
display();} k += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string nk = "";
private: for( string::iterator si = k.begin(); si
void doIt( int dir ){ != k.end(); si++ )
int a, b, c, d; string ntxt; {*si = toupper( *si ); if( *si < 65 ||
for( string::const_iterator ti = *si > 90 ) continue;
_txt.begin(); ti != _txt.end(); ti++ ) if( ( *si == 'J' && ij ) || ( *si == 'Q'
{if( getCharPos( *ti++, a, b ) ) && !ij ) )continue;
if( nk.find( *si ) == -1 ) nk += *si;} void rotor_tiga(string);
copy( nk.begin(), nk.end(), &_m[0][0]
);} void input_output(string);
string _txt; char _m[5][5];}; void reflektor();
int main( int argc, char* argv[] ) void enkripsi();
{string key, i, txt; bool ij, e;
cout << "(E)ncode or (D)ecode? "; void dekripsi();
getline( cin, i ); e = ( i[0] == 'e' ||
i[0] == 'E' ); void lihat_teks();};
cout << "Enter a en/decryption key: ";
getline( cin, key );
cout << "I <-> J (Y/N): "; getline( cin,
i ); ij = ( i[0] == 'y' || i[0] == 'Y' Enigma::Enigma(){
);
cout << "Enter the text: "; getline( this->posisi = 0;
cin, txt );
playfair pf; pf.doIt( key, txt, ij, e ); this->r_satu = this->r_dua = this->r_tiga = 0;
return 0;}
Sleep(500);

ENIGMA system("color 02");

#include <iostream> cout << "\n\t\tKelompok 14 Enigma : \n" << endl;

#include <fstream> Sleep(600);

#include <stdio.h> system("color 03");

#include <windows.h> cout << "\t1. Muhammad Ridho Rohanda


(1600018010)" << endl;
#include <string>
Sleep(600);
#include <cstdlib>
system("color 05");

cout << "\t2. Agies Dwi Permana


using namespace std; (1600018023)" << endl;
class Enigma{ Sleep(600);
private: system("color 08");
string plainteks; cout << "\t3. Dhian Fendina Hapsari
string cipherteks; (1600018024)" << endl;

string kunci; Sleep(600);

int posisi, nilai_dari_plain; cout << "\nMasukan plainteks tanpa spasi [KAPITAL]:
";
int index_huruf;
cin >> this->plainteks;
int r_satu, r_dua, r_tiga;
cout << "Masukan Kunci [hanya 3 huruf] : ";

cin >> this->kunci;


public:
enkripsi();}
Enigma();

~Enigma();
Enigma::~Enigma(){
void rotor_satu(string);
ofstream ofile;
void rotor_dua(string);
ofile.open("enigma.txt", ios::app);
ofile << "Plainteksnya : " << this->plainteks << endl;

ofile << "Kuncinya : " << this->kunci << endl; void Enigma::reflektor(){

ofile << "Hasil Enkripsi : " << this->cipherteks << endl; string abjad = "ABCDEFGDIJKGMKMIEBFTCVVJAT";

ofile << endl; char huruf = abjad[this->index_huruf];

ofile.close(); for (int i = 0; i < 26; i++){

cout << "\nHasil Enkripsinya : " << this->cipherteks << if (huruf == abjad[i] && i != this->index_huruf){
endl;
this->index_huruf = i;}}}
cout << "Program selesai" << endl;}

void Enigma::rotor_satu(string aksi){


void Enigma::enkripsi(){
int pos;
int i = 0;
int kunci = (int)this->kunci[0] % 65;
while (i < this->plainteks.length()){
string kiri = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
input_output("cari");
string kanan = "EKMFLGDQVZNTOWYHXUSPAIBRCJ";
rotor_tiga("cari");
if (aksi == "cari"){
rotor_dua("cari");
for (int i = 0; i < 26; i++){
rotor_satu("cari");
pos = (kunci + i) % 26;
reflektor();
if (kanan[(this->index_huruf + kunci) % 26] ==
rotor_satu("kembali"); kiri[pos]){

rotor_dua("kembali"); this->index_huruf = i;

rotor_tiga("kembali"); break;}}}

input_output("kembali"); else{

i++;}} for (int i = 0; i < 26; i++){

pos = (kunci + i) % 26;

//void Enigma::dekripsi(){ if (kiri[(this->index_huruf + kunci) % 26] ==


kanan[pos]){
// cout << "Proses dekripsi ..." << endl;
this->index_huruf = i;
//}
break;}}
void Enigma::input_output(string aksi){
if (this->r_satu == 26){
string abjad = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
this->r_satu = 0;}}}
if (aksi == "cari"){

this->nilai_dari_plain = (int)this->plainteks[this-
>posisi] % 65; void Enigma::rotor_dua(string aksi){

this->index_huruf = this->nilai_dari_plain;} int kunci = (int)this->kunci[1] % 65;

else{ kunci += this->r_dua;

this->cipherteks += abjad[this->index_huruf]; string kiri = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

this->posisi++;}} string kanan = "AJDKSIRUXBLHWTMCQGZNPYFVOE";


if (aksi == "cari"){ this->index_huruf = i;

for (int i = 0; i < 26; i++){ break;}}

int pos = (i + kunci) % 26; if (this->r_tiga != 26){

if (kanan[(this->index_huruf + kunci) % 26] == this->r_tiga++;}


kiri[pos]){
else{
this->index_huruf = i;
this->r_tiga = 0;
break;}}}
this->r_dua++;}}}
else{
int main(){
for (int i = 0; i < 26; i++){
Enigma test;
int pos = (i + kunci) % 26;
return 0;}
if (kiri[(this->index_huruf + kunci) % 26] ==
kanan[pos]){
ENIGMA ENKRIP
this->index_huruf = i;

break;}}
//ENKRIPSI TO DEKRIPSI
#include <iostream>
#include <cstring>
if (this->r_dua == 26){ using namespace std;

this->r_dua = 0; char alpa[] =


"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
this->r_satu++;}}} char rotor[3][27] = {
"EKMFLGDQVZNTOWYHXUSPAIBRCJ",
"AJDKSIRUXBLHWTMCQGZNPYFVOE",
"BDFHJLCPRTXVZNYEIWGAKMUSQO"};
void Enigma::rotor_tiga(string aksi){ char reflector[] =
"YRUHQSLDPXNGOKMIEBFZCWVJAT";
int kunci = (int)this->kunci[2] % 65; char kunci[] = "ABC";
kunci += this->r_tiga; long mod26(long a) {
return (a%26+26)%26; }
string kiri = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int li (char l) {
return l - 'A'; }
string kanan = "BDFHJLCPRTXVZNYEIWGAKMUSQO";
int indexof (char* array, int find) {
if (aksi == "cari"){ return strchr(array, find) - array; }
for (int i = 0; i < 26; i++){ string crypt (const char *ct) {
int L = li(kunci[0]);
int pos = (i + kunci) % 26; int M = li(kunci[1]);
int R = li(kunci[2]);
if (kanan[(this->index_huruf + kunci) % 26] ==
kiri[pos]){ string output;
for ( int x = 0; x < strlen(ct) ; x++ )
this->index_huruf = i; {
int ct_letter = li(ct[x]);
break;}}}
R = mod26(R + 1);
else{ char a = rotor[2][mod26(R + ct_letter)];
char b = rotor[1][mod26(M + li(a) - R)];
for (int i = 0; i < 26; i++){ char c = rotor[0][mod26(L + li(b) - M)];
char ref = reflector[mod26(li(c) - L)];
int pos = (i + kunci) % 26; int d = mod26(indexof(rotor[0],
alpa[mod26(li(ref) + L)]) - L);
if (kiri[(this->index_huruf + kunci) % 26] == int e = mod26(indexof(rotor[1],
kanan[pos]){ alpa[mod26(d + M)]) - M);
char f = alpa[mod26(indexof(rotor[2], cout <<"Enkripsi : "<<crypt
alpa[mod26(e + R)]) - R)]; ("THESIMPLESUBSTITUTIONCIPHERISACIPHER")
output += f; }return output;} ;// }
return 0;}
int main ()
{ // for ( int i = 0; i <
cout<<"Kata Enkripsi : AFFINE CIPHER
PZUFWDSASJGQGNRMAEODZJXQQKHSYGVUSGSU"<<e
ndl; #include <iostream>
cout <<"Dekripsi : "<<crypt
("PZUFWDSASJGQGNRMAEODZJXQQKHSYGVUSGSU") #include <algorithm>
;// }
return 0;} #include <numeric>

#include <cmath>
ENIGMA DEKRIP

int gcd(int a, int b);


//ENKRIPSI TO DEKRIPSI
#include <iostream> int modInverse(int a, int b); // from
#include <cstring>
https://rosettacode.org/wiki/Modular_inverse#C.2B.2
using namespace std;
B
char alpa[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int main(){
char rotor[3][27] ={
"EKMFLGDQVZNTOWYHXUSPAIBRCJ", std::string choice;
"AJDKSIRUXBLHWTMCQGZNPYFVOE",
"BDFHJLCPRTXVZNYEIWGAKMUSQO"}; do{
char reflector[] =
"YRUHQSLDPXNGOKMIEBFZCWVJAT"; std::cout << "Enkripsi atau Deskripsi? tekan[e/d] = ";
char kunci[] = "ABC";
long mod26(long a){ std::getline(std::cin, choice);
return (a%26+26)%26;}
int li (char l) std::transform(choice.begin(), choice.end(),
{return l - 'A';} choice.begin(), ::tolower);

int indexof (char* array, int find) } while(choice.length() > 1 || choice != "e" && choice
{return strchr(array, find) - array;} != "d");
string crypt (const char *ct)
{int L = li(kunci[0]);
int M = li(kunci[1]);
int R = li(kunci[2]); std::cout << "\nMasukkan Plainteks: ";
string output;
for ( int x = 0; x < strlen(ct) ; x++ ) std::string input;
{
int ct_letter = li(ct[x]); std::getline(std::cin, input);
R = mod26(R + 1);
char a = rotor[2][mod26(R + ct_letter)];
char b = rotor[1][mod26(M + li(a) - R)];
char c = rotor[0][mod26(L + li(b) - M)]; int a, b;
char ref = reflector[mod26(li(c) - L)];
int d = mod26(indexof(rotor[0], do{
alpa[mod26(li(ref) + L)]) - L);
int e = mod26(indexof(rotor[1], std::cout << "\nm (biulangan relatif prima) and b
alpa[mod26(d + M)]) - M); (pergeseran) \nm = ";
char f = alpa[mod26(indexof(rotor[2],
alpa[mod26(e + R)]) - R)]; std::cin >> a;
output += f;}
return output;} std::cout << "b = ";
int main () std::cin >> b;
{// for ( int i = 0; i < 1000000; i++) {
//inputan enkripsi } while(std::cin.fail() || gcd(a,b) != 1);
cout<<"Kata Dekripsi :
THESIMPLESUBSTITUTIONCIPHERISACIPHER"<<e std::cout << '\n';
ndl;
if(choice == "e"){ #include<math.h>

for(int i = 0; i < input.length(); ++i){ #include <fstream>

if(input[i] >= 'a' && input[i] <= 'z'){

std::cout << (char)((a * (input[i] - 'a') + b) % 26 + 'a'); using namespace std;

} else if(input[i] >= 'A' && input[i] <= 'Z'){ float Enkrip[3][1], Dekrip[3][1], a[3][3], b[3][3],
mes[3][1], c[3][3];
std::cout << (char)((a * (input[i] - 'A') + b) % 26 + 'A');
void Enkripsi(); //mengenkripsi pesan
} else{
void Deskripsi();//mendeskripsi pesan
std::cout << input[i];
void getKeyMessage(); //mendapat kunci dan pesan
}}} else{
dari pengguna
for(int i = 0; i < input.length(); ++i){
void invers(); //menemukan kebalikan dari
if(input[i] >= 'a' && input[i] <= 'z'){ matriks kunci

std::cout << (char)(modInverse(a, 26) * (26 + input[i] - int main() {


'a' - b) % 26 + 'a');
getKeyMessage();
} else if(input[i] >= 'A' && input[i] <= 'Z'){
Enkripsi();
std::cout << (char)(modInverse(a, 26) * (26 + input[i] -
Deskripsi();
'A' - b) % 26 + 'A');
std::ofstream outfile;
} else{
outfile.open("yourfile.txt",
std::cout << input[i];}}}
std::ios_base::app);//std::ios_base::app
std::cout << '\n';
outfile << "your data";
return 0;}
return 0;}
int gcd(int a, int b){

return b == 0 ? a : gcd(b, a % b);}


void Enkripsi() {
int modInverse(int a, int b){
int i, j, k;
int b0 = b, t, q;
for(i = 0; i < 3; i++)
int x0 = 0, x1 = 1;
for(j = 0; j < 1; j++)
if (b == 1) return 1;
for(k = 0; k < 3; k++)
while (a > 1) {
Enkrip[i][j] = Enkrip[i][j] + a[i][k] * mes[k][j];
q = a / b;
cout<<"\nString yang dienkripsi adalah: ";
t = b, b = a % b, a = t;
for(i = 0; i < 3; i++)
t = x0, x0 = x1 - q * x0, x1 = t;}
cout<<(char)(fmod(Enkrip[i][0], 26) + 97);}
if (x1 < 0) x1 += b0;

return x1;}
void Deskripsi() {

int i, j, k;
HILL CIPHER
invers();
#include<iostream>
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++) b[i][j]=0;}

for(k = 0; k < 3; k++) for(k = 0; k < 3; k++) {

Dekrip[i][j] = Dekrip[i][j] + b[i][k] * Enkrip[k][j]; for(i = 0; i < 3; i++) {

cout<<"\nString yang dideskripsi adalah: "; p = c[i][k];

for(i = 0; i < 3; i++) q = c[k][k];

cout<<(char)(fmod(Dekrip[i][0], 26) + 97);

for(j = 0; j < 3; j++) {

cout<<"\n";} if(i != k) {

c[i][j] = c[i][j]*q - p*c[k][j];

void getKeyMessage() { b[i][j] = b[i][j]*q - p*b[k][j];}}}}

int i, j; for(i = 0; i < 3; i++)

char msg[3]; for(j = 0; j < 3; j++)

cout<<"====================================== b[i][j] = b[i][j] / c[i][i];


======================\n";
cout<<"\n\nMatriks Terbalik adalah:\n";
cout<<"Masukkan matriks 3x3 untuk kunci (Ini harus
for(i = 0; i < 3; i++) {
tidak terbalik):\n";
for(j = 0; j < 3; j++)
cout<<"======================================
======================\n"; cout<<b[i][j]<<" ";
for(i = 0; i < 3; i++) cout<<"\n";}}
for(j = 0; j < 3; j++) {

cin>>a[i][j]; HILL CIPHER


c[i][j] = a[i][j];} #include <iostream>
cout<<"\nMasukkan string 3 huruf: "; #include <regex>
cin>>msg;

for(i = 0; i < 3; i++) using namespace std;


mes[i][0] = msg[i] - 97;} int moduloFunc(int a, int b){

int result = a % b;
void invers() { if (result < 0){
int i, j, k; result += b;}
float p, q; return result;}
for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++) { void cipherEncryption(){


if(i == j) string msg;
b[i][j]=1; cout << "Enter message: ";
else getline(cin, msg);
// untuk mengkonfersi huruf kecil ke huruf besar itr3++;}}

for (int i = 0; i < msg.length(); i++){ // mengecek validasi kunci

msg[i] = toupper(msg[i]);} // menemukan determinant

// menghapus spasi dari plainteks int deter = key2D[0][0] * key2D[1][1] - key2D[0][1] *


key2D[1][0];
msg = regex_replace(msg, regex("\\s+"), "");
deter = moduloFunc(deter, 26);
int lenChk = 0;
// membuat matrik inversnya
if(msg.length()%2 != 0){
int mulInv = -1;
msg += "0";
for (int i=0; i<26; i++){
lenChk = 1;}
int tempInv = deter * i;
int msg2D[2][msg.length()/2];
if (moduloFunc(tempInv, 26) == 1){
int itr1 = 0;
mulInv = i;
int itr2 = 0;
break;
for (int i = 0; i < msg.length(); i++){
} else {
if(i%2 == 0){
continue;}}
msg2D[0][itr1] = msg[i] - 65;
if (mulInv == -1){
itr1++;
cout << "invalid key" << endl;
} else {
exit(EXIT_FAILURE);}
msg2D[1][itr2] = msg[i] - 65;
string encrypText = "";
itr2++;}}
int itrCount = msg.length()/2;
cout << "Enter 4 letter key string: ";
if (lenChk == 0){
string key;
for (int i = 0; i < itrCount; i++){
getline(cin, key);
int temp1 = msg2D[0][i] * key2D[0][0] + msg2D[1][i] *
// untuk mengubah huruf kecil ke huruf besar pada
key2D[0][1];
kunci
encrypText += (char)((temp1 % 26) + 65);
for (int i = 0; i < key.length(); i++){
int temp2 = msg2D[0][i] * key2D[1][0] + msg2D[1][i] *
key[i] = toupper(key[i]);}
key2D[1][1];
// menghapus spasi pada key
encrypText += (char)((temp2 % 26) + 65);}
key = regex_replace(key, regex("\\s+"), "");
} else {
// kunci matirk 2*2
for (int i = 0; i < itrCount-1; i++){
int key2D[2][2];
int temp1 = msg2D[0][i] * key2D[0][0] + msg2D[1][i] *
int itr3 = 0; key2D[0][1];

for (int i = 0; i < 2; i++){ encrypText += (char)((temp1 % 26) + 65);

for (int j = 0; j<2; j++){ int temp2 = msg2D[0][i] * key2D[1][0] + msg2D[1][i] *


key2D[1][1];
key2D[i][j] = key[itr3]-65;
encrypText += (char)((temp2 % 26) + 65);}}
cout << endl << "Encrypted text: " << encrypText << // kunci matrik 2*2
endl;}
int key2D[2][2];
void cipherDecryption(){
int itr3 = 0;
string msg;
for (int i = 0; i < 2; i++){
cout << "Enter message: ";
for (int j = 0; j<2; j++){
getline(cin, msg);
key2D[i][j] = key[itr3]-65;
// untuk mengkonfersi huruf kecil ke huruf besar
itr3++;}}
for (int i = 0; i < msg.length(); i++){
// untuk menemukan determinant
msg[i] = toupper(msg[i]);}
int deter = key2D[0][0] * key2D[1][1] - key2D[0][1] *
// menghapus spasi dari chiperteks key2D[1][0];

msg = regex_replace(msg, regex("\\s+"), ""); deter = moduloFunc(deter, 26);

int lenChk = 0; // untuk menemukan inversnya

if(msg.length()%2 != 0){ int mulInv = -1;

msg += "0"; for (int i=0; i<26; i++){

lenChk = 1;} int tempInv = deter * i;

int msg2D[2][msg.length()/2]; if (moduloFunc(tempInv, 26) == 1){

int itr1 = 0; mulInv = i;

int itr2 = 0; break;

for (int i = 0; i < msg.length(); i++){ } else {

if(i%2 == 0){ continue;}}

msg2D[0][itr1] = msg[i] - 65; swap(key2D[0][0],key2D[1][1]);

itr1++; key2D[0][1] *= -1;

} else { key2D[1][0] *= -1;

msg2D[1][itr2] = msg[i] - 65; key2D[0][1] = moduloFunc(key2D[0][1], 26);

itr2++;}} key2D[1][0] = moduloFunc(key2D[1][0], 26);

cout << "Enter 4 letter key string: "; for (int i = 0; i < 2; i++){

string key; for (int j = 0; j < 2; j++){

getline(cin, key); key2D[i][j] *= mulInv;}}

// untuk mengubah huruf kecil ke huruf besar pada for (int i=0; i<2;i++){
kunci
for(int j =0; j <2; j++){
for (int i = 0; i < key.length(); i++){
key2D[i][j] = moduloFunc(key2D[i][j], 26);}}
key[i] = toupper(key[i]);}
// chiperteks ke plainteks

string decrypText = "";


// menghapus spasi dari kunci
int itrCount = msg.length()/2;
key = regex_replace(key, regex("\\s+"), "");
if (lenChk == 0){

for (int i = 0; i < itrCount; i++){ HILL CIPHER

int temp1 = msg2D[0][i] * key2D[0][0] + msg2D[1][i] * #include<iostream>


key2D[0][1];
#include<stdio.h>
decrypText += (char)((temp1 % 26) + 65);
using namespace std;
int temp2 = msg2D[0][i] * key2D[1][0] + msg2D[1][i] *
class hill
key2D[1][1];
{public:
decrypText += (char)((temp2 % 26) + 65);}
string msg,k;
} else {
int *enc;
for (int i = 0; i < itrCount-1; i++){
int key[3][3],inv[3][3];
int temp1 = msg2D[0][i] * key2D[0][0] + msg2D[1][i] *
key2D[0][1]; hill(string a, string b)
decrypText += (char)((temp1 % 26) + 65); {msg = a;
int temp2 = msg2D[0][i] * key2D[1][0] + msg2D[1][i] * k = b;
key2D[1][1];
int i,j,l=0;
decrypText += (char)((temp2 % 26) + 65);
cout<<"\nMatriks Kunci : "<<endl;
}}
for(i=0;i<3;i++)
cout << endl << "Decrypted text: " << decrypText <<
endl;} {for(j=0;j<3;j++)

int main() {if(l>=k.size())

{cout << "WELCOME TO PROGRAM HILL CHIPER" << {key[i][j] = 'x' - 'a' + 1;
endl; }else{
cout << "1. Encryption\n2. Decryption" << endl; key[i][j] = k[l]-'a' + 1;}
cout << "Silahkan Pilih METODE : " ; cout<<key[i][j]<<" ";
int choice; l++;}
cin >> choice; cout<<endl;}
cin.ignore(); cout<<endl;
if (choice == 1){ while(msg.size()%3!=0)
cout << endl << "---Encryption---" << endl; {msg = msg + 'x';}
cipherEncryption(); cout<<"Extended Message : ";
} else if (choice == 2){ cout<<msg<<endl;
cout << endl << "---Decryption---" << endl; enc = new int[msg.size()];
cipherDecryption(); }void encrypt(){
} else { int i,j,l,m;
cout << endl << "PILIHAN SALAH" << endl; for(l=0;l<msg.size();)
}return 0;} {m = l;
for(i=0;i<3;i++)

{enc[l] = (key[i][0]*(msg[m]-'a' + 1) +
key[i][1]*(msg[m+1]-'a' + 1) +key[i][2]*(msg[m+2]-'a' +
1))%26 + 'a'-1;

cout<<(char)enc[l];

l++;}}}};

int main()

{string s,k;

cout<<"Masukkan Plainteks"<<endl;

fflush(stdin);

getline(cin,s);

cout<<"Masukkan Kunci"<<endl;

fflush(stdin);

getline(cin,k);

hill h(s,k);

cout<<endl;

cout<<"Enkripsi : ";

h.encrypt();

cout<<endl;

return 0;

Вам также может понравиться