0% found this document useful (0 votes)
17 views5 pages

Crytography

The document contains various encryption and decryption techniques including substitution ciphers, Rail Fence transposition, columnar transposition, RSA algorithm, Diffie-Hellman key exchange, AES, DES, and SHA-256 hashing. Each technique is implemented in Python with example code demonstrating how to encrypt and decrypt text. The document serves as a comprehensive guide for understanding and implementing these cryptographic methods.

Uploaded by

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

Crytography

The document contains various encryption and decryption techniques including substitution ciphers, Rail Fence transposition, columnar transposition, RSA algorithm, Diffie-Hellman key exchange, AES, DES, and SHA-256 hashing. Each technique is implemented in Python with example code demonstrating how to encrypt and decrypt text. The document serves as a comprehensive guide for understanding and implementing these cryptographic methods.

Uploaded by

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

1.

#encryption for substitution technique

def encrypt(plaintext, key):


alphabet = "abcdefghijklmnopqrstuvwxyz"
result = ""

for char in plaintext:


if char.isalpha():
lower_char = char.lower()
index = alphabet.index(lower_char)
encrypted_char = key[index]
if char.isupper():
result += encrypted_char.upper()
else:
result += encrypted_char
else:
result += char

return result

key = "qwertyuiopasdfghjklzxcvbnm"
plaintext = input("Enter the plaintext: ")
encrypted_text = encrypt(plaintext, key)
print("Encrypted text:", encrypted_text)

2.#decryption of substitution technique

def decrypt(ciphertext, key):


alphabet = "abcdefghijklmnopqrstuvwxyz"
result = ""

for char in ciphertext:


if char.isalpha():
lower_char = char.lower()
index = key.index(lower_char)
decrypted_char = alphabet[index]
if char.isupper():
result += decrypted_char.upper()
else:
result += decrypted_char
else:
result += char

return result

key = "qwertyuiopasdfghjklzxcvbnm"
ciphertext = input("Enter the ciphertext: ")
decrypted_text = decrypt(ciphertext, key)
print("Decrypted text:", decrypted_text)

3.#Rail Fence Transposition

def encrypt_rail_fence(text, key):


rail = ['' for _ in range(key)]
row, direction = 0, 1
for char in text:
rail[row] += char
row += direction

if row == 0 or row == key - 1:


direction *= -1

return ''.join(rail)

text = "helloworldhowareyou"
key = 7
print("Rail Fence Encrypted:",encrypt_rail_fence(text, key) )

4.# Single Columnar Transposition

def encrypt_columnar(text, key):


columns = ['' for _ in range(key)]
for i, char in enumerate(text):
columns[i % key] += char

return ''.join(columns)

text = "hello"
key = 3

print("Single Columnar Encrypted:", encrypt_columnar(text, key))

5.#Double Columnar Transposition

def encrypt_double_columnar(text, key1, key2):


columns = ['' for _ in range(key1)]
for i, char in enumerate(text):
columns[i % key1] += char

intermediate_text = ''.join(columns)
columns = ['' for _ in range(key2)]

for i, char in enumerate(intermediate_text):


columns[i % key2] += char

return ''.join(columns)

text = "hello"
key1 = 3
key2 = 1
encrypted_double_columnar = encrypt_double_columnar(text, key1, key2)
print("Double Columnar Encrypted:", encrypted_double_columnar)

6.#RSA Algorithm Implementation

import math

def gcd(a, b):


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

def mod_inverse(e, phi):


for d in range(1, phi):
if (e * d) % phi == 1:
return d
return None

def encrypt_rsa(plaintext, e, n):


return [(ord(char) ** e) % n for char in plaintext]

def decrypt_rsa(ciphertext, d, n):


return ''.join([chr((char ** d) % n) for char in ciphertext])

p = 61
q = 53
n = p * q
phi = (p - 1) * (q - 1)

e = 17
d = mod_inverse(e, phi)

plaintext = "hello"
ciphertext = encrypt_rsa(plaintext, e, n)
decrypted_text = decrypt_rsa(ciphertext, d, n)

print("RSA Encrypted:", ciphertext)


print("RSA Decrypted:", decrypted_text)

7.#Diffie-Hellman Key Exchange Algorithm

def power_mod(base, exponent, mod):


result = 1
base = base % mod
while exponent > 0:
if exponent % 2 == 1:
result = (result * base) % mod
exponent = exponent >> 1
base = (base * base) % mod
return result

P = 23
G = 5

a = 6
A = power_mod(G, a, P)

b = 15
B = power_mod(G, b, P)

shared_key_a = power_mod(B, a, P)
shared_key_b = power_mod(A, b, P)

print("Shared Key A:", shared_key_a)


print("Shared Key B:", shared_key_b)
pip install pycryptodome
8.#AES Algorithms implementation

from Crypto.Cipher import AES


from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes

def encrypt_aes(plaintext, key):


cipher = AES.new(key, AES.MODE_CBC)
ciphertext = cipher.encrypt(pad(plaintext.encode(), AES.block_size))
return cipher.iv + ciphertext

def decrypt_aes(ciphertext, key):


iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = unpad(cipher.decrypt(ciphertext[AES.block_size:]), AES.block_size)
return plaintext.decode()

key = get_random_bytes(16)
plaintext = "hello world"
ciphertext = encrypt_aes(plaintext, key)
decrypted_text = decrypt_aes(ciphertext, key)

print("AES Encrypted:", ciphertext)


print("AES Decrypted:", decrypted_text)

9.#DES Algorithms implementation

from Crypto.Cipher import DES


from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes

def encrypt_des(plaintext, key):


cipher = DES.new(key, DES.MODE_CBC)
ciphertext = cipher.encrypt(pad(plaintext.encode(), DES.block_size))
return cipher.iv + ciphertext

def decrypt_des(ciphertext, key):


iv = ciphertext[:DES.block_size]
cipher = DES.new(key, DES.MODE_CBC, iv)
plaintext = unpad(cipher.decrypt(ciphertext[DES.block_size:]), DES.block_size)
return plaintext.decode()

key = get_random_bytes(8)
plaintext = "hello123"
ciphertext = encrypt_des(plaintext, key)
decrypted_text = decrypt_des(ciphertext, key)

print("DES Encrypted:", ciphertext)


print("DES Decrypted:", decrypted_text)

10.#SHA algorithm implmentation


import hashlib

def sha256_hash(text):
return hashlib.sha256(text.encode()).hexdigest()

text = "hello world"


hashed_text = sha256_hash(text)
print("SHA-256 Hashed:", hashed_text)

pip install pycryptodome

You might also like