Experiment On Cryptography From A Student
Experiment On Cryptography From A Student
Cryptograph
Full name : Douae Salhi
Student ID : 202338060032
Questionnaire :
# 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}")
```
# 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
# 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 :
-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:
-Contains multiple tapes and heads, allowing for more complex computations.
-More powerful than a single-tape machine but still Turing complete.
Counter Machine:
B. Implimentation :
This algorithm encrypts and decrypts text using a basic Caesar cipher with a
specified shift value.
Encryption:
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.
This algorithm encrypts and decrypts text using the Vigenère cipher, which
employs a keyword to provide varying shifts for each character.
Encryption:
Decryption:
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.
C . running examples :
Input :
print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")
Output :
Input :
print(f"Encrypted: {encrypted}")
print(f"Decrypted: {decrypted}")
Output :
RSA algorithm:
Input :
import random
p = 61
q = 53
public, private = generate_keypair(p, q)
message = "hello world"
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