added lab2 questions
This commit is contained in:
		
							parent
							
								
									857ebf4a77
								
							
						
					
					
						commit
						a1b2495e47
					
				
					 5 changed files with 99 additions and 11 deletions
				
			
		
							
								
								
									
										31
									
								
								IS/Lab/Lab2/3des.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								IS/Lab/Lab2/3des.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,31 @@
 | 
			
		|||
from Crypto.Cipher import DES3
 | 
			
		||||
from Crypto.Util.Padding import pad, unpad
 | 
			
		||||
 | 
			
		||||
def des3_cipher(key):
 | 
			
		||||
    return DES3.new(key.encode('utf-8'), DES3.MODE_ECB)
 | 
			
		||||
 | 
			
		||||
def des3_en(ptext, key):
 | 
			
		||||
    cipher = des3_cipher(key)
 | 
			
		||||
    padded_text = pad(ptext.encode('utf-8'), DES3.block_size)
 | 
			
		||||
    return cipher.encrypt(padded_text)
 | 
			
		||||
 | 
			
		||||
def des3_de(ctext, key):
 | 
			
		||||
    cipher = des3_cipher(key)
 | 
			
		||||
    decrypted = unpad(cipher.decrypt(ctext), DES3.block_size)
 | 
			
		||||
    return decrypted.decode('utf-8')
 | 
			
		||||
 | 
			
		||||
def despad_key(key):
 | 
			
		||||
    key = key.ljust(24)[:24]
 | 
			
		||||
    return key
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    print("Welcome to 3DES (Triple DES)")
 | 
			
		||||
    ptext = input("Enter plaintext: ")
 | 
			
		||||
    key = input("Enter key (minimum 16 characters for 3DES): ")
 | 
			
		||||
    key = despad_key(key)
 | 
			
		||||
    ctext = des3_en(ptext, key)
 | 
			
		||||
    print("Your ciphertext: ", ctext)
 | 
			
		||||
    print("Your decrypted plaintext: ", des3_de(ctext, key))
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
							
								
								
									
										34
									
								
								IS/Lab/Lab2/aes_128.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								IS/Lab/Lab2/aes_128.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,34 @@
 | 
			
		|||
from Crypto.Cipher import AES
 | 
			
		||||
from Crypto.Util.Padding import pad, unpad
 | 
			
		||||
 | 
			
		||||
def aes_cipher(key):
 | 
			
		||||
    return AES.new(key.encode('utf-8'), AES.MODE_ECB)
 | 
			
		||||
 | 
			
		||||
def aes_en(ptext, key):
 | 
			
		||||
    cipher = aes_cipher(key)
 | 
			
		||||
    ptext = pad(ptext.encode('utf-8'), AES.block_size)
 | 
			
		||||
    return cipher.encrypt(ptext)
 | 
			
		||||
 | 
			
		||||
def aes_de(ctext, key):
 | 
			
		||||
    cipher = aes_cipher(key)
 | 
			
		||||
    decrypted = cipher.decrypt(ctext)
 | 
			
		||||
    return unpad(decrypted, AES.block_size).decode('utf-8')
 | 
			
		||||
 | 
			
		||||
def aespad_key(key):
 | 
			
		||||
    return key.ljust(16)[:16]
 | 
			
		||||
 | 
			
		||||
    # 16 for 128 bit, 24 for 192, 32 for 256
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    print("Welcome to AES-128")
 | 
			
		||||
    ptext = input("Enter plaintext: ")
 | 
			
		||||
    key = input("Enter key: ")
 | 
			
		||||
 | 
			
		||||
    key = aespad_key(key)
 | 
			
		||||
 | 
			
		||||
    ctext = aes_en(ptext, key)
 | 
			
		||||
    print("Your ciphertext: ", ctext)
 | 
			
		||||
    print("Your decrypted plaintext: ", aes_de(ctext, key))
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
							
								
								
									
										29
									
								
								IS/Lab/Lab2/aes_192.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								IS/Lab/Lab2/aes_192.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,29 @@
 | 
			
		|||
from Crypto.Cipher import AES
 | 
			
		||||
from Crypto.Util.Padding import pad, unpad
 | 
			
		||||
 | 
			
		||||
def aes_cipher(key):
 | 
			
		||||
    return AES.new(key.encode('utf-8'), AES.MODE_ECB)
 | 
			
		||||
 | 
			
		||||
def aes_en(ptext, key):
 | 
			
		||||
    cipher = aes_cipher(key)
 | 
			
		||||
    ptext = pad(ptext.encode('utf-8'), AES.block_size)
 | 
			
		||||
    return cipher.encrypt(ptext)
 | 
			
		||||
 | 
			
		||||
def aes_de(ctext, key):
 | 
			
		||||
    cipher = aes_cipher(key)
 | 
			
		||||
    decrypted = cipher.decrypt(ctext)
 | 
			
		||||
    return unpad(decrypted, AES.block_size).decode('utf-8')
 | 
			
		||||
 | 
			
		||||
def aespad_key(key):
 | 
			
		||||
        return key.ljust(24)[:24]
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    ptext = input("Enter plaintext: ")
 | 
			
		||||
    key = input("Enter key: ")
 | 
			
		||||
    key = aespad_key(key)
 | 
			
		||||
    ctext = aes_en(ptext, key)
 | 
			
		||||
    print("Ciphertext:", ctext.hex())
 | 
			
		||||
    print("Decrypted:", aes_de(ctext, key))
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
| 
						 | 
				
			
			@ -1,23 +1,18 @@
 | 
			
		|||
from Crypto.Cipher import DES
 | 
			
		||||
from Crypto.Util.Padding import pad, unpad
 | 
			
		||||
 | 
			
		||||
def des_cipher(key):
 | 
			
		||||
    return DES.new(key.encode('utf-8'), DES.MODE_ECB)
 | 
			
		||||
 | 
			
		||||
def despad_ptext(text):
 | 
			
		||||
    n = len(text) % 8
 | 
			
		||||
    if n != 0:
 | 
			
		||||
        return text + (' ' * (8 - n))
 | 
			
		||||
    else:
 | 
			
		||||
        return text
 | 
			
		||||
 | 
			
		||||
def des_en(ptext, key):
 | 
			
		||||
    cipher = des_cipher(key)
 | 
			
		||||
    ptext = despad_ptext(ptext).encode('utf-8')
 | 
			
		||||
    return cipher.encrypt(ptext)
 | 
			
		||||
    padded_text = pad(ptext.encode('utf-8'), DES.block_size)
 | 
			
		||||
    return cipher.encrypt(padded_text)
 | 
			
		||||
 | 
			
		||||
def des_de(ctext, key):
 | 
			
		||||
    cipher = des_cipher(key)
 | 
			
		||||
    return cipher.decrypt(ctext).decode('utf-8').rstrip()
 | 
			
		||||
    decrypted = unpad(cipher.decrypt(ctext), DES.block_size)
 | 
			
		||||
    return decrypted.decode('utf-8')
 | 
			
		||||
 | 
			
		||||
def despad_key(key):
 | 
			
		||||
    return key.ljust(8)[:8]
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +20,6 @@ def despad_key(key):
 | 
			
		|||
def main():
 | 
			
		||||
    print("Welcome to DES (Original)")
 | 
			
		||||
    ptext = input("Enter plaintext: ")
 | 
			
		||||
    despad_ptext(ptext)
 | 
			
		||||
    key = input("Enter key: ")
 | 
			
		||||
    key = despad_key(key)
 | 
			
		||||
    ctext = des_en(ptext, key)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										0
									
								
								IS/Lab/Lab2/des_vs_aes256.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								IS/Lab/Lab2/des_vs_aes256.py
									
										
									
									
									
										Normal file
									
								
							
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue