Muhd Faizun Nazim Bin MD Fauzan - NWS21070675
Muhd Faizun Nazim Bin MD Fauzan - NWS21070675
DIPLOMA PROGRAMME
GENERAL INSTRUCTIONS:
1. Write down clearly (with capital letters) your name, trade, group and the date
of examination at the bottom of this page.
2. Do not open this question booklet until you are told to do so by the invigilator.
3. Read the specific instructions and questions carefully before answering.
Level
CL Full
No Assessment Criteria PO of Score Marks Comment
O Marks
Diff
1. Lab work #1 0 0
Install Python
2 Lab work #2 2 4 P3 15
Create a Python program to apply
substitution technique.
3. Lab work #3 2 4 P3 15
Create a Python program to apply
transposition technique.
4. Lab work #4 2 4 P3 15
Create a Python program to apply
Hash algorithm technique.
Lab work #5
Create a Python program to apply 2 4
5 P3 15
Public Key encryption algorithm
technique.
Lab work #6
6 Create a Python program to apply 2 4 P3 15
cryptographic protocol.
Total 75
Total Marks /75
Total PO 4 /75
Signature
Page 2 of 20
Copyright of German-Malaysian Institute. All rights reserved.
SCHEME – each lab work given
APPROACHES MEETS
NO. CRITERIA EXCELLENT [3] MARKS MARKS MARKS MARKS MARKS MARKS
EXPECTATIONS [1] EXPECTATIONS [2]
LAB 1 LAB 2 LAB 3 LAB 4 LAB 5 LAB 6
LAB WORKS
Functional
Able to setup the functional
program: Able to setup the
1 Unable to setup the programming without major
Able to construct the functional programming
functional programming. assistance.
python language with little assistance.
toward cryptography
Discover encryption
technique: Able to discover the
Unable to discover the
2 Able to discover the Able to discover the cryptography techniques,
cryptography techniques,
classical and modern cryptography techniques analyse the strength of
and analyze the strength
encryption technique with little assistance. encryption without major
of encryption technique.
assistance.
Demonstrate:
Able to demonstrate Able to demonstrate the
Unable to demonstrate the Able to demonstrate the various
the various encryption various cipher algorithm
3 various cipher algorithm in algorithm without major
techniques. in difference scenarios
difference scenarios. assistance.
with little assistance.
Interpretation:
Unable to interpret most of Able to interpret all the findings
4 Able to Interpret Able to interpret most of
the findings and result. and result clearly and
findings. the findings and result.
comprehensively.
TOTAL
ANSWER, OUTPUT AND ELABORATE THE FINDINGS
Step 1: Take the photo your laptop at every time to start creating a Python program
Step 2: Screen capture the OUTPUT PROGRAM and result at every lab works given.
Step 3: Submit the lab sheet in PDF format and rename the file with your FULL NAME and
STUDENT ID.
Lab work #1
Install Python
Software installation:
https://www.python.org/downloads/
Python Extension
inside VS Code
Select Python
language
CBS 2373 CRYPTOGRAPHY
Lab work #2
Create a Python program to apply substitution technique.
Page 5 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
Page 6 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
import tkinter as tk
Vigenere Cipher
def vigenere_cipher(text, key, mode):
result = ""
key_len = len(key)
def encrypt():
plaintext = entry_text.get()
key = entry_key.get()
ciphertext =
vigenere_cipher(plaintext, key,
"encrypt")
result_label.config(text="Encrypted
Text: " + ciphertext)
def decrypt():
ciphertext = entry_text.get()
key = entry_key.get()
plaintext =
vigenere_cipher(ciphertext, key,
"decrypt")
result_label.config(text="Decrypted
Text: " + plaintext)
key_label = tk.Label(window,
text="Enter Key:")
entry_key = tk.Entry(window)
encrypt_button = tk.Button(window,
text="Encrypt", command=encrypt)
decrypt_button = tk.Button(window,
text="Decrypt", command=decrypt)
result_label = tk.Label(window,
text="Result:")
Page 7 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
key_label.pack()
entry_key.pack()
encrypt_button.pack()
decrypt_button.pack()
result_label.pack()
Page 8 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
Lab work #3
Create a Python program to apply transposition technique.
Page 9 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
def encrypt():
plain_text = entry_text.get()
rails = int(entry_rails.get())
cipher_text =
encrypt_rail_fence(plain_text, rails)
Page 10 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
result_label.config(text="Encrypted
Text: " + cipher_text)
def decrypt():
cipher_text = entry_text.get()
rails = int(entry_rails.get())
plain_text =
decrypt_rail_fence(cipher_text, rails)
result_label.config(text="Decrypted
Text: " + plain_text)
rails_label = tk.Label(window,
text="Number of Rails:")
entry_rails = tk.Entry(window)
encrypt_button = tk.Button(window,
text="Encrypt", command=encrypt)
decrypt_button = tk.Button(window,
text="Decrypt", command=decrypt)
result_label = tk.Label(window,
text="Result:")
Page 11 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
Lab work #4
Create a Python program to apply Hash algorithm technique.
Page 12 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
def calculate_md5():
input_text = entry_text.get()
md5_hash = 1. Imports:
hashlib.md5(input_text.encode()).hexd
igest() tkinter library is imported to create the
result_label.config(text="MD5 Hash: graphical user interface (GUI).
" + md5_hash)
2. Function calculate_md5:
# Create the main window
window = tk.Tk() This function is invoked when the
window.title("MD5 Hash Generator") "Calculate MD5" button is pressed.
It retrieves the text entered in the
# Create and configure widgets input field.
text_label = tk.Label(window, It generates the MD5 hash of the input
text="Enter Text:") text using hashlib.md5 and encodes
entry_text = tk.Entry(window) the text before hashing using
.encode().
calculate_button = tk.Button(window,
text="Calculate MD5", 3. Main Window Creation:
command=calculate_md5)
A window is created using tkinter.Tk().
result_label = tk.Label(window, The window's title is set to "MD5 Hash
text="MD5 Hash:") Generator".
5. Widget Placement:
Page 13 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
Lab work #5
Create a Python program to apply Public Key encryption algorithm technique.
Page 14 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
n = prime1 * prime2
fi = (prime1 - 1) * (prime2 - 1)
e=2
while True:
if math.gcd(e, fi) == 1:
break
e += 1
Page 15 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
d=2
while True:
if (d * e) % fi == 1:
break
d += 1
private_key = d
encoded.append(encrypt(ord(letter)))
return encoded
def decoder(encoded):
s = ''
# Calling the decrypting function in
the decoding function
for num in encoded:
s += chr(decrypt(num))
return s
if __name__ == '__main__':
primefiller()
setkeys()
message = "Test Message"
# Uncomment below for manual
input
# message = input("Enter the
message\n")
# Calling the encoding function
coded = encoder(message)
print("Initial message:")
print(message)
print("\n\nThe encoded
message(encrypted by public key)\n")
print(''.join(str(p) for p in coded))
print("\n\nThe decoded
message(decrypted by public key)\n")
Page 16 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
print(''.join(str(p) for p in
decoder(coded)))
import tkinter as tk
DSA (Digital Signature Algorithm) import hashlib
import random
def generate_dsa_keys():
global p, q, g, x, y
p = 809
q = 13
g=6
x = random.randint(1, q - 1)
y = (g ** x) % p
public_key_text.delete(1.0, tk.END)
public_key_text.insert(tk.END, f"p:
{p}\nq: {q}\ng: {g}\ny: {y}")
def sign_message():
global r, s
message = message_text.get(1.0,
tk.END).strip()
k = random.randint(1, q - 1)
r = (g ** k % p) % q Here's an example based on the
h= provided code:
int(hashlib.sha1(message.encode()).he
xdigest(), 16) Click "Generate DSA Keys" to generate
s = (k ** -1 * (h + x * r)) % q DSA keys. The "Public Key" text field
signature_text.delete(1.0, tk.END) shows the values of p, q, g, and y.
signature_text.insert(tk.END, f"r:
{r}\ns: {s}") In the "Message" text field, enter a
message (e.g., "Hello, world!").
def verify_signature():
message = message_text.get(1.0, Click "Sign Message." The "Signature"
tk.END).strip() text field shows the calculated r and s
h= values.
int(hashlib.sha1(message.encode()).he
xdigest(), 16) Click "Verify Signature." If the
w = s ** -1 % q signature is valid, the "Verification
u1 = (h * w) % q Result" label will display "Signature is
u2 = (r * w) % q valid."
v = ((g ** u1 * y ** u2) % p) % q
The validity of the signature depends
if v == r: on the correctness of the key pair
generation, message signing, and
verification_result.config(text="Signatu verification processes. In practice,
re is valid.") these processes are much more
else: complex and involve secure key
management. This code is a simplified
verification_result.config(text="Signatu demonstration and should not be used
re is not valid.") for real cryptographic security.
window = tk.Tk()
window.title("DSA Signature
Generation and Verification")
generate_keys_button =
tk.Button(window, text="Generate DSA
Keys", command=generate_dsa_keys)
generate_keys_button.pack()
public_key_text = tk.Text(window,
height=6, width=40)
public_key_text.pack()
message_text = tk.Text(window,
height=6, width=40)
message_text.pack()
Page 17 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
sign_button = tk.Button(window,
text="Sign Message",
command=sign_message)
sign_button.pack()
signature_text = tk.Text(window,
height=6, width=40)
signature_text.pack()
verify_button = tk.Button(window,
text="Verify Signature",
command=verify_signature)
verify_button.pack()
verification_result = tk.Label(window,
text="")
verification_result.pack()
window.mainloop()
Page 18 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
Lab work #6
Create a Python program to apply cryptographic protocol.
Page 19 of 20
Copyright of German-Malaysian Institute. All rights reserved.
CBS 2373 CRYPTOGRAPHY
2. Generate a Key:
3. Encrypt a Message:
Page 20 of 20
Copyright of German-Malaysian Institute. All rights reserved.