0% found this document useful (0 votes)
26 views13 pages

Experiment On Cryptography From A Student

The document details the implementation of Turing coding versions 1 and 2, along with the RSA algorithm using Python. It includes code examples for each algorithm, explanations of their functionality, and running examples demonstrating their encryption and decryption processes. The conclusion highlights the evolution of cryptographic techniques from simple ciphers to complex systems like RSA for enhanced data security.

Uploaded by

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

Experiment On Cryptography From A Student

The document details the implementation of Turing coding versions 1 and 2, along with the RSA algorithm using Python. It includes code examples for each algorithm, explanations of their functionality, and running examples demonstrating their encryption and decryption processes. The conclusion highlights the evolution of cryptographic techniques from simple ciphers to complex systems like RSA for enhanced data security.

Uploaded by

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

Experiment on

Cryptograph
Full name : Douae Salhi

Student ID : 202338060032
Questionnaire :

1. Implementing the turing coding of V.1 .


2. Implementing the turing coding of V.2 .
3. Implement the RSA algorithm .
4 . Report :
A. Introduction (what’s the turing coding versions and RSA algorithms?)
B.implementation (how the algorithms work?)
C. running example .
5. conclusion .
6. Sources .

(I used python to code)


1.Implementing the Turing Coding (v.1) Algorithms :

Here’s a basic example of a substitution cipher for Turing's Code version 1:

def turing_code_v1_encrypt(text, shift):


encrypted_text = ""
for char in text:
if char.isalpha():
shift_char = chr(((ord(char.lower()) - 97 + shift) % 26) + 97)
encrypted_text += shift_char
else:
encrypted_text += char
return encrypted_text

def turing_code_v1_decrypt(encrypted_text, shift):


decrypted_text = ""
for char in encrypted_text:
if char.isalpha():
shift_char = chr(((ord(char.lower()) - 97 - shift + 26) % 26) + 97)
decrypted_text += shift_char
else:
decrypted_text += char
return decrypted_text

# Example usage
text = "hello world"
shift = 3
encrypted = turing_code_v1_encrypt(text, shift)
decrypted = turing_code_v1_decrypt(encrypted, shift)
print(f"Encrypted: {encrypted}, Decrypted: {decrypted}")
```

2. Implementing the Turing Coding (v.2) Algorithms :

For version 2, we might consider a slightly more complex algorithm, such as


a polyalphabetic cipher:

def turing_code_v2_encrypt(text, key):


key_length = len(key)
encrypted_text = ""
for i, char in enumerate(text):
if char.isalpha():
shift = ord(key[i % key_length].lower()) - 97
shift_char = chr(((ord(char.lower()) - 97 + shift) % 26) + 97)
encrypted_text += shift_char
else:
encrypted_text += char
return encrypted_text

def turing_code_v2_decrypt(encrypted_text, key):


key_length = len(key)
decrypted_text = ""
for i, char in enumerate(encrypted_text):
if char.isalpha():
shift = ord(key[i % key_length].lower()) - 97
shift_char = chr(((ord(char.lower()) - 97 - shift + 26) % 26) + 97)
decrypted_text += shift_char
else:
decrypted_text += char
return decrypted_text

# Example usage
text = "hello world"
key = "key"
encrypted = turing_code_v2_encrypt(text, key)
decrypted = turing_code_v2_decrypt(encrypted, key)
print(f"Encrypted: {encrypted}, Decrypted: {decrypted}")
```
3. Implementing the RSA Algorithm:
Here's a simple implementation of the RSA algorithm:

import random

def gcd(a, b):


while b != 0:
a, b = b, a % b
return a

def modinv(a, m):


m0, x0, x1 = m, 0, 1
if m == 1:
return 0
while a > 1:
q = a // m
m, a = a % m, m
x0, x1 = x1 - q * x0, x0
if x1 < 0:
x1 += m0
return x1

def generate_keypair(p, q):


n=p*q
phi = (p - 1) * (q - 1)
e = random.randrange(1, phi)
g = gcd(e, phi)
while g != 1:
e = random.randrange(1, phi)
g = gcd(e, phi)
d = modinv(e, phi)
return ((e, n), (d, n))

def encrypt(pk, plaintext):


key, n = pk
cipher = [(ord(char) ** key) % n for char in plaintext]
return cipher

def decrypt(pk, ciphertext):


key, n = pk
plain = [chr((char ** key) % n) for char in ciphertext]
return ''.join(plain)

# Example usage
p = 61
q = 53
public, private = generate_keypair(p, q)
message = "hello world"
encrypted_msg = encrypt(public, message)
decrypted_msg = decrypt(private, encrypted_msg)
print(f"Encrypted: {encrypted_msg}, Decrypted: {decrypted_msg}")
```
4. Report :

A. Introduction :

A Turing machine is an abstract computational model introduced by Alan


Turing in 1936. It consists of:

-An infinite tape divided into cells, each capable of holding a symbol.
-A tape head that reads and writes symbols on the tape.
-A finite set of states that dictate the machine's operations based on the current
symbol and state.

Functionality:

-The machine reads the symbol under the tape head.


-Based on the current state and the symbol, it writes a new symbol, changes its
state, and moves the tape head left or right.
-It can enter an infinite loop, similar to real computer programs.

Versions of Turing Machines :

Deterministic Turing Machine (DTM):

-Has a single action for each combination of state and symbol.


-Operates in a predictable manner.

Non-Deterministic Turing Machine (NDTM):


-Can have multiple possible actions for a given state and symbol.
-Used in theoretical computer science to explore computational complexity.

Multi-Tape Turing Machine:

-Contains multiple tapes and heads, allowing for more complex computations.
-More powerful than a single-tape machine but still Turing complete.

Random Access Machine (RAM):

-A model that represents a more practical computation model, akin to modern


computers.
-Allows for direct access to memory locations, unlike the sequential access of a
Turing machine.

Counter Machine:

-A simplified model that uses counters instead of a tape.


-It can perform computations similar to Turing machine but easier to analyze.

B. Implimentation :

Algorithm 1: turing coding (V.1)

This algorithm encrypts and decrypts text using a basic Caesar cipher with a
specified shift value.

Encryption:

- The function `turing_code_v1_encrypt` takes in a `text` string and a `shift`


value.
- It iterates over each character in the `text`.
- If the character is an alphabet letter, it shifts the character by the specified
number of places (`shift`) in the alphabet.
- It converts the character to lowercase, adjusts its ASCII value, shifts it within
the range of lowercase letters (a-z), and then converts it back to a character.
- Non-alphabet characters are added to the `encrypted_text` without change.
- The resulting `encrypted_text` contains the shifted characters.

Decryption:
- The function `turing_code_v1_decrypt` reverses the process by subtracting the
shift value.
- It performs the same checks and operations but subtracts the shift value from
each character's ASCII value.

Algorithm 2: turing coding (V.2)

This algorithm encrypts and decrypts text using the Vigenère cipher, which
employs a keyword to provide varying shifts for each character.

Encryption:

- The function `turing_code_v2_encrypt` takes a `text` and a `key`.


- It iterates over each character in the `text`, using the `key` to determine the
shift for each character.
- The key is repeated as necessary to match the length of the text.
- The shift for each character is based on the corresponding character in the
key.
- It shifts each character in the `text` accordingly.
- Non-alphabet characters are added to the `encrypted_text` without change.
- The resulting `encrypted_text` contains characters shifted based on the key.

Decryption:

- The function `turing_code_v2_decrypt` reverses the process using the same


key.
- It subtracts the corresponding key character's shift value from each text
character.

Algorithm 3: RSA Encryption (with key generation, encryption, and


decryption)

This algorithm is a simplified implementation of the RSA encryption algorithm.

Key Generation:
- The function `generate_keypair` generates a public and private key pair using
two prime numbers `p` and `q`.
- It calculates the modulus `n` (product of `p` and `q`) and Euler's totient
function `phi`.
- It chooses a random integer `e` that is coprime to `phi` and calculates the
multiplicative inverse `d` of `e` modulo `phi` using the `modinv` function.
- The public key is `(e, n)`, and the private key is `(d, n)`.

Encryption:
- The function `encrypt` takes a public key `(e, n)` and a `plaintext`.
- It converts each character in the plaintext to its ASCII value, raises it to the
power of `e`, and takes the modulus `n`.
- The resulting list of numbers is the encrypted message.

Decryption:
- The function `decrypt` takes a private key `(d, n)` and the `ciphertext`.
- It raises each number in the ciphertext to the power of `d` and takes the
modulus `n`.
- It converts the resulting numbers back to characters to reconstruct the original
plaintext.

These algorithms illustrate different methods of encryption, from simple Caesar


shifts to more complex RSA encryption based on mathematical properties.

C . running examples :

Turing code (V.1) algorithm:

Input :

def turing_code_v1_encrypt(text, shift):


encrypted_text = ""
for char in text:
if char.isalpha():
shift_char = chr(((ord(char.lower()) - 97 + shift) % 26) + 97)
encrypted_text += shift_char
else:
encrypted_text += char
return encrypted_text

def turing_code_v1_decrypt(encrypted_text, shift):


decrypted_text = ""
for char in encrypted_text:
if char.isalpha():
shift_char = chr(((ord(char.lower()) - 97 - shift + 26) % 26) + 97)
decrypted_text += shift_char
else:
decrypted_text += char
return decrypted_text

text = "hello world"


shift = 3

encrypted = turing_code_v1_encrypt(text, shift)


decrypted = turing_code_v1_decrypt(encrypted, shift)

print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")

Output :

Encrypted: khoor zruog


Decrypted: hello world

Turing coding (V.2)

Input :

def turing_code_v2_encrypt(text, key):


key_length = len(key)
encrypted_text = ""
for i, char in enumerate(text):
if char.isalpha():
shift = ord(key[i % key_length].lower()) - 97
shift_char = chr(((ord(char.lower()) - 97 + shift) % 26) + 97)
encrypted_text += shift_char
else:
encrypted_text += char
return encrypted_text

def turing_code_v2_decrypt(encrypted_text, key):


key_length = len(key)
decrypted_text = ""
for i, char in enumerate(encrypted_text):
if char.isalpha():
shift = ord(key[i % key_length].lower()) - 97
shift_char = chr(((ord(char.lower()) - 97 - shift + 26) % 26) + 97)
decrypted_text += shift_char
else:
decrypted_text += char
return decrypted_text

text = "hello world"


key = "key"

encrypted = turing_code_v2_encrypt(text, key)


decrypted = turing_code_v2_decrypt(encrypted, key)

print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")

Output :

Encrypted: ripqv brmdm


Decrypted: hello world

RSA algorithm:

Input :

import random

def gcd(a, b):


while b != 0:
a, b = b, a % b
return a

def modinv(a, m):


m0, x0, x1 = m, 0, 1
if m == 1:
return 0
while a > 1:
q = a // m
m, a = a % m, m
x0, x1 = x1 - q * x0, x0
if x1 < 0:
x1 += m0
return x1
def generate_keypair(p, q):
n=p*q
phi = (p - 1) * (q - 1)
e = random.randrange(1, phi)
g = gcd(e, phi)
while g != 1:
e = random.randrange(1, phi)
g = gcd(e, phi)
d = modinv(e, phi)
return ((e, n), (d, n))

def encrypt(pk, plaintext):


key, n = pk
cipher = [(ord(char) ** key) % n for char in plaintext]
return cipher

def decrypt(pk, ciphertext):


key, n = pk
plain = [chr((char ** key) % n) for char in ciphertext]
return ''.join(plain)

p = 61
q = 53
public, private = generate_keypair(p, q)
message = "hello world"

encrypted_msg = encrypt(public, message)


decrypted_msg = decrypt(private, encrypted_msg)

print(f"Encrypted: {encrypted_msg}")
print(f"Decrypted: {decrypted_msg}")

Output :

Encrypted: [2790, 2146, 3110, 3110, 2502, 1265, 4203, 3110, 3479, 4203,
2813]
Decrypted: hello world

5. Conclusion :
To sum it up,the provided algorithms demonstrate different cryptographic
methods. The Basic Caesar Cipher shifts each letter by a specified number,
providing simple encryption with limited security. The Vigenère Cipher
improves on this by using a keyword for variable shifts, making the encryption
more complex and secure. The RSA algorithm, a public-key cryptographic
system, relies on prime numbers to create a secure encryption-decryption pair
with a public key for encryption and a private key for decryption. This
algorithm is widely used for secure communications due to its robust security
features. Together, these algorithms illustrate the evolution of cryptographic
techniques from simple substitution to advanced mathematical systems,
enhancing data security through increased complexity and variability.

6.Sources :

- wikipedia.com
- cryptii.com
- boxentriq.com
- encryptionconsulting.com
- simplilearn.com

You might also like