0% found this document useful (0 votes)
65 views

Kriptografi Hill & Permutation Cipher

The document contains programs implementing permutation encryption and Hill cipher encryption on a given plaintext. It includes: 1. A program for permutation encryption on {1,2,3,4} with the permutation (2 4 3 1) and decryption. It encrypts the plaintext "wemeetatmidnight". 2. Programs for Hill cipher encryption and decryption using a 2x2 key matrix. It finds the inverse key for decryption. It encrypts the plaintext "wewillmeetatmidnight". 3. A subprogram to find the inverse of a Hill cipher key matrix. It finds the inverse of the key matrix [[2 5], [9 5]].
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)
65 views

Kriptografi Hill & Permutation Cipher

The document contains programs implementing permutation encryption and Hill cipher encryption on a given plaintext. It includes: 1. A program for permutation encryption on {1,2,3,4} with the permutation (2 4 3 1) and decryption. It encrypts the plaintext "wemeetatmidnight". 2. Programs for Hill cipher encryption and decryption using a 2x2 key matrix. It finds the inverse key for decryption. It encrypts the plaintext "wewillmeetatmidnight". 3. A subprogram to find the inverse of a Hill cipher key matrix. It finds the inverse of the key matrix [[2 5], [9 5]].
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/ 11

𝑇𝑢𝑔𝑎𝑠 2 𝐾𝑟𝑖𝑝𝑡𝑜𝑔𝑟𝑎𝑓𝑖

𝐼𝑓𝑓𝑎ℎ 𝑁𝑢𝑟𝑙𝑎𝑡ℎ𝑖𝑓𝑎ℎ 𝐹𝑖𝑘𝑟𝑖 (17/412720/𝑃𝐴/18039)

Nomor 1 dan 2
Buat program untuk Kriptosistem Permutasi pada {1, 2, 3, 4,} dengan  = (2 4 3 1) dan Hill
 2 5
dengan kunci matriks   . Termasuk subprogram mencari invers kunci untuk dekripsi.
 9 5
Implementasikan pada plainteks wemeetatmidnight
Jawab :
Kriptosistem Permutasi
import string

def lteks(word):
return list(word)

def teks(s):
txt = ""
return (txt.join(s))

def s_encrypt(text):
# if message length is an odd number, place a zero at the end.
len_chk = 0
if len(text) % 4 != 0:
text += "0"
len_chk = 1

b=lteks(text)
m=list(b)

for i in range(0,len(b)):
if i % 4 ==0:
m[i+1]=chr((ord(b[i])-97) % 26 + 65)
elif i % 4 ==1:
m[i+2]=chr((ord(b[i])-97) % 26 + 65)
elif i % 4 ==2:
m[i]=chr((ord(b[i])-97) % 26 + 65)
else :
m[i-3]=chr((ord(b[i])-97) % 26 + 65)
return(teks(m))

def s_decrypt(text):
b=lteks(text)
perm=list(b)
return(teks(m))

def s_decrypt(text):
b=lteks(text)
perm=list(b)
for i in range(0,len(b)):
if i % 4 ==0:
perm[i+3]=chr((ord(b[i])-65) % 26 + 97)
elif i % 4 ==1:
perm[i-1]=chr((ord(b[i])-65) % 26 + 97)
elif i % 4 ==2:
perm[i]=chr((ord(b[i])-65) % 26 + 97)
else :
perm[i-2]=chr((ord(b[i])-65) % 26 + 97)

return(teks(perm))

t=s_encrypt('wewillmeetatmidnight')
print ('hasil encripsi : ', t)
t1=s_decrypt(t)
print('hasil dekripsi : ',t1)

hasil program :
hasil encripsi : IWWEELMLTEATNMDITIHG
hasil dekripsi : wewillmeetatmidnight

Kriptosistem Hill
import sys
import numpy as np
def cipher_encryption(plain, key2d):
# if message length is an odd number, place a zero at the end.
len_chk = 0
if len(plain) % 2 != 0:
plain += "0"
len_chk = 1
# msg to matrices
row = 2
col = int(len(plain)/2)
msg2d = np.zeros((row, col), dtype=int)
itr1 = 0
itr2 = 0
for i in range(len(plain)):
if i%2 == 0:
msg2d[0][itr1]= int(ord(plain[i]) - 65)
itr1 += 1
else:
msg2d[1][itr2] = int(ord(plain[i]) - 65)
itr2 += 1
# key to 2x2
key2d = np.zeros((2,2), dtype=int)
itr3 = 0
for i in range(2):
for j in range(2):
key2d[i][j] = ord(key[itr3]) - 65
itr3 += 1
print (key2d)
# checking validity of the key
# finding determinant
deter = key2d[0][0] * key2d[1][1] - key2d[0][1] * key2d[1][0]
deter = deter % 26
# finding multiplicative inverse
for i in range(26):
temp_inv = deter * i
if temp_inv % 26 == 1:
mul_inv = i
break
else:
continue
if mul_inv == -1:
print("Invalid key")
sys.exit()
encryp_text = ""
itr_count = int(len(plain)/2)
if len_chk == 0:
for i in range(itr_count):
temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1]
encryp_text += chr((temp1 % 26) + 65)
temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1]
encryp_text += chr((temp2 % 26) + 65)
else:
for i in range(itr_count-1):
temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1]
encryp_text += chr((temp1 % 26) + 65)
temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1]
encryp_text += chr((temp2 % 26) + 65)
print("Encrypted text: {}".format(encryp_text))
return encryp_text
encryp_text += chr((temp2 % 26) + 65)
print("Encrypted text: {}".format(encryp_text))
return encryp_text

def cipher_decryption(cipher, key):


# if message length is an odd number, place a zero at the end.
len_chk = 0
if len(cipher) % 2 != 0:
cipher += "0"
len_chk = 1
# msg to matrices
row = 2
col = int(len(cipher)/2)
msg2d = np.zeros((row, col), dtype=int)
itr1 = 0
itr2 = 0
for i in range(len(cipher)):
if i%2 == 0:
msg2d[0][itr1]= int(ord(cipher[i]) - 65)
itr1 += 1
else:
msg2d[1][itr2] = int(ord(cipher[i]) - 65)
itr2 += 1
# key to 2x2
key2d = np.zeros((2,2), dtype=int)
itr3 = 0
for i in range(2):
for j in range(2):
key2d[i][j] = ord(key[itr3]) - 65
itr3 += 1
# finding determinant
deter = key2d[0][0] * key2d[1][1] - key2d[0][1] * key2d[1][0]
deter = deter % 26
# finding multiplicative inverse
for i in range(26):
temp_inv = deter * i
if temp_inv % 26 == 1:
mul_inv = i
break
else:
continue
# adjugate matrix
# swapping
key2d[0][0], key2d[1][1] = key2d[1][1], key2d[0][0]
#changing signs
key2d[0][1] *= -1
key2d[1][0] *= -1
key2d[0][1] = key2d[0][1] % 26
key2d[1][0] = key2d[1][0] % 26
# multiplying multiplicative inverse with adjugate matrix
for i in range(2):
for j in range(2):
key2d[i][j] *= mul_inv
# modulo
for i in range(2):
for j in range(2):
key2d[i][j] = key2d[i][j] % 26
print(key2d)
# cipher to plaintext
decryp_text = ""
itr_count = int(len(cipher)/2)
if len_chk == 0:
for i in range(itr_count):
temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1]
decryp_text += chr((temp1) % 26 + 97) #chr((temp1 % 26) + 65)
temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1]
decryp_text += chr((temp2) % 26 + 97)
# for
else:
for i in range(itr_count-1):
temp1 = msg2d[0][i] * key2d[0][0] + msg2d[1][i] * key2d[0][1]
decryp_text += chr((temp1) % 26 + 97)
temp2 = msg2d[0][i] * key2d[1][0] + msg2d[1][i] * key2d[1][1]
decryp_text += chr((temp1) % 26 + 97)
# for
# if else
print("Decrypted text: {}".format(decryp_text))

plaintext = "wewillmeetatmidnight"
plaintext = plaintext.upper().replace(" ","")
key = 'cfjf'
key = key.upper().replace(" ","")
ciphertext = cipher_encryption(plaintext, key)
cipher_decryption(ciphertext, key)
Output Program

[[2 5]
[9 5]]
Encrypted text: MKGEZYSYZBRRMSTOUYFC
[[11 15]
[ 1 20]]
Decrypted text: wewillmeetatmidnight

Subprogram mencari invers kunci hill cipher:


import numpy as np
def inv_hill(key):
key2d = np.zeros((2,2), dtype=int)
itr3 = 0
for i in range(2):
for j in range(2):
key2d[i][j] = ord(key[itr3]) - 65
itr3 += 1
print('matriks kunci : ')
print(key2d)
# finding determinant
deter = key2d[0][0] * key2d[1][1] - key2d[0][1] * key2d[1][0]
deter = deter % 26
# finding multiplicative inverse
for i in range(26):
temp_inv = deter * i
if temp_inv % 26 == 1:
mul_inv = i
break
else:
continue
# adjugate matrix
# swapping
key2d[0][0], key2d[1][1] = key2d[1][1], key2d[0][0]
#changing signs
key2d[0][1] *= -1
key2d[1][0] *= -1
key2d[0][1] = key2d[0][1] % 26
key2d[1][0] = key2d[1][0] % 26
key2d[i][j] *= mul_inv
# multiplying multiplicative inverse with adjugate matrix
for i in range(2):
for j in range(2):
key2d[i][j] *= mul_inv
# modulo
for i in range(2):
for j in range(2):
key2d[i][j] = key2d[i][j] % 26
print('invers :')
print(key2d)
return
key='cfjf'
key = key.upper().replace(" ","")
inv_hill(key)

Output sub program :


matriks kunci :
[[2 5]
[9 5]]
invers :
[[11 15]
[ 1 20]]

Nomor 3
Pelajari Theory Shanon, khususnya example 2.3 dan Huffmans Encoding untuk example 2.4
fungsi f.
Jawaban :
Nomor 4
Jelaskan SPN example 1.3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy