0% found this document useful (0 votes)
140 views6 pages

Hill Cipher

The C++ program implements Hill cipher encryption and decryption. It contains four main functions: encryption() for encrypting plaintext, decryption() for decrypting ciphertext, getKeyMessage() for getting the key matrix, and inverse() for calculating the inverse of the key matrix. The program flow is: input the 3x3 key matrix, input the plaintext, output the encryption result, decryption result, and inverse of the key matrix.

Uploaded by

Fadlan Fasya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views6 pages

Hill Cipher

The C++ program implements Hill cipher encryption and decryption. It contains four main functions: encryption() for encrypting plaintext, decryption() for decrypting ciphertext, getKeyMessage() for getting the key matrix, and inverse() for calculating the inverse of the key matrix. The program flow is: input the 3x3 key matrix, input the plaintext, output the encryption result, decryption result, and inverse of the key matrix.

Uploaded by

Fadlan Fasya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Muhammad Fadlan Fasya

140810190006

Pembahasan Kode

#include <iostream>
#include <math.h>

using namespace std;

float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];

void encryption(); //mengenkripsi pesan


void decryption(); //mendekripsi pesan
void getKeyMessage(); //get key dan message
void inverse(); //mencari inverse dari key matriks

int main()
{
getKeyMessage();
encryption();
decryption();
}

void encryption()
{
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];
cout<<"\nHasil Enkripsi : ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(encrypt[i][0], 26) + 97);
}

void decryption()
{
int i, j, k;
inverse();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];
cout<<"\nHasil Dekripsi : ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(decrypt[i][0], 26) + 97);
cout<<"\n";
}

void getKeyMessage()
{
int i, j;
char msg[3];

cout<<"Masukan key matrix :\n";


for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
cin>>a[i][j];
c[i][j] = a[i][j];
}
cout<<"\nMasukan tiga kata untuk plaintext: ";
cin>>msg;
for(i = 0; i < 3; i++)
mes[i][0] = msg[i] - 97;
}

void inverse() {
int i, j, k;
float p, q;

for(i = 0; i < 3; i++)


for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}
for(k = 0; k < 3; k++) {
for(i = 0; i < 3; i++) {
p = c[i][k];
q = c[k][k];
for(j = 0; j < 3; j++) {
if(i != k) {
c[i][j] = c[i][j]*q - p*c[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / c[i][i];
cout<<"\n\nInverse Matrix is:\n";
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
cout<<b[i][j]<<" ";
cout<<"\n";
}
}

Program Hill Cipher Enkripsi dan Dekripsi diatas ditulis menggunakan C++, terdapat empat
fungsi utama yaitu :
● encryption(), digunakan untuk mengenkripsi plaintext.
● decryption(), digunakan untuk mendekripsi ciphertext
● getKeyMessage(), digunakan untuk mengambil key matriks
● Inverse(), digunakan untuk mencari invers dari key matriks

Untuk alur aplikasinya, diawal akan diminta untuk memasukan key matriks 3x3. Lalu setelah
memasukan key, akan diminta memasukan plaintext. Lalu akan keluar hasil enkripsi, dekripsi,
dan invers matriks key.

#include <iostream>
#include <bits/stdc++.h>

using namespace std ;

int key[3][3] ;

int mod26(int x)
{
return x >= 0 ? (x%26) : 26-(abs(x)%26) ;
}

int findDet(int m[3][3] , int n)


{
int det;
if(n == 2)
{
det = m[0][0] * m[1][1] - m[0][1]*m[1][0] ;
}
else det = 0 ;
return mod26(det);
}

int findDetInverse(int R , int D = 26)


{
int i = 0 ;
int p[100] = {0,1};
int q[100] = {0} ;

while(R!=0)
{
q[i] = D/R ;
int oldD = D ;
D=R;
R = oldD%R ;
if(i>1)
{
p[i] = mod26(p[i-2] - p[i-1]*q[i-2]) ;
}
i++ ;
}
if (i == 1) return 1;
else return p[i] = mod26(p[i-2] - p[i-1]*q[i-2]) ;
}

int gcd(int m, int n){


if (n > m)
swap(m,n);

do{
int temp = m % n;
m = n;
n = temp;
} while (n != 0);

return m;
}

void findKey(){
string plainteks,cipherteks;
int key[2][2],det,detInv,adj[2][2],plainteksInv[2][2],plainMatrix[2][2],cipMatrix[2][2],counter;
int p,c;
int transpose[2][2];

cout << "Masukkan Plainteks : ";


cin.ignore();
getline(cin,plainteks);
counter = 0;
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
p = toupper(plainteks[counter]) - 65;
plainMatrix[i][j] = p;
counter++;
}
}

cout << "Masukkan Cipherteks : ";


getline(cin,cipherteks);

counter = 0;
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
c = toupper(cipherteks[counter]) - 65;
cipMatrix[i][j] = c;
counter++;
}
}

det = (plainMatrix[0][0] * plainMatrix[1][1]) - (plainMatrix[0][1] * plainMatrix[1][0]);


if(gcd(det,26)==1){
detInv = findDetInverse(det, 26);

adj[0][0] = plainMatrix[1][1];
adj[0][1] = (-1)*plainMatrix[0][1];
adj[1][0] = (-1)*plainMatrix[1][0];
adj[1][1] = plainMatrix[0][0];

for(int i = 0; i < 2; i++){


for(int j = 0; j < 2; j++){
plainteksInv[i][j] = detInv * adj[i][j];
if(plainteksInv[i][j] < 0){
plainteksInv[i][j] = 26 - (abs(plainteksInv[i][j])%26);
}else{
plainteksInv[i][j] = plainteksInv[i][j];
plainteksInv[i][j] = plainteksInv[i][j] % 26;
}
}
}

for(int i = 0; i < 2; i++){


for(int j = 0; j < 2; j++){
key [i][j] = 0;
for(int k = 0; k < 2; k++){
key [i][j] += (plainteksInv[i][k] * cipMatrix[k][j]);
}
key [i][j] %= 26;
}
}

for (int i = 0; i < 2; i++){


for (int j = 0; j < 2; j++){
transpose[j][i] = key[i][j];
}
}

for(int i = 0; i < 2; i++){


for (int j = 0; j < 2; j++){
cout << (transpose[i][j]) << "\t";
}
cout <<endl;
}
}else{
cout << "Determinan tidak relatif " <<endl;
cout << "Key not found" <<endl<<endl;
}
}

int main(){
findKey();
}

Program Find Key Hill Cipher diatas ditulis menggunakan C++, terdapat 5 fungsi utama yaitu :
● mod26(), fungsi untuk modulo 26
● findDet(), fungsi untuk mencari determinan matriks
● findDetInverse(), fungsi untuk mencari invers matriks
● gcd(), fungsi gcd
● findKey(), fungsi mencari key

Untuk alur aplikasinya, diawal akan diminta memasukan plaintext dan ciphertext. Lalu jika
determinan sudah relatif akan keluar key, jika determinan tidak relatif, akan keluar peringatan.

You might also like