added lab2 q1
This commit is contained in:
		
							parent
							
								
									933a52b3a8
								
							
						
					
					
						commit
						857ebf4a77
					
				
					 3 changed files with 70 additions and 6 deletions
				
			
		| 
						 | 
				
			
			@ -2,21 +2,16 @@ def main():
 | 
			
		|||
    # Affine cipher: E(x) = (ax + b) mod 26
 | 
			
		||||
    # Given: "ab" -> "GL"
 | 
			
		||||
    # a=0, b=1 -> G=6, L=11
 | 
			
		||||
    # So: 6 = (a*0 + b) mod 26 -> b = 6
 | 
			
		||||
    # And: 11 = (a*1 + b) mod 26 -> 11 = (a + 6) mod 26 -> a = 5
 | 
			
		||||
 | 
			
		||||
    ciphertext = "XPALASXYFGFUKPXUSOGEUTKCDGEXANMGNVS"
 | 
			
		||||
 | 
			
		||||
    # Try all possible values of a and b for affine cipher
 | 
			
		||||
    for a in range(1, 26):
 | 
			
		||||
        # a must be coprime to 26
 | 
			
		||||
        if gcd(a, 26) != 1:
 | 
			
		||||
            continue
 | 
			
		||||
 | 
			
		||||
        for b in range(26):
 | 
			
		||||
            # Check if this key produces "ab" -> "GL"
 | 
			
		||||
            # if key produces "ab" -> "GL"
 | 
			
		||||
            if (a * 0 + b) % 26 == 6 and (a * 1 + b) % 26 == 11:
 | 
			
		||||
                # Found the key, now decrypt the message
 | 
			
		||||
                a_inv = mod_inverse(a, 26)
 | 
			
		||||
                decrypted = ""
 | 
			
		||||
                for char in ciphertext:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										33
									
								
								IS/Lab/Lab2/aes_des_bit.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								IS/Lab/Lab2/aes_des_bit.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
from crypto.Cipher import DES3
 | 
			
		||||
 | 
			
		||||
def des_pad(text):
 | 
			
		||||
    n = len(text) % 8
 | 
			
		||||
    return text + (b' ' * n)
 | 
			
		||||
 | 
			
		||||
def des_128(ptext, key):
 | 
			
		||||
    cipher=DES.new(key, DES.MODE_ECB)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def des_192(ptext, key):
 | 
			
		||||
 | 
			
		||||
def des_256(ptext, key):
 | 
			
		||||
 | 
			
		||||
def aes_128(ptext, key):
 | 
			
		||||
def aes_192(ptext, key):
 | 
			
		||||
def aes_256(ptext, key):
 | 
			
		||||
 | 
			
		||||
def bokeh_graph():
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    print("The AES/DES Encryptor")
 | 
			
		||||
    ptext = input("Enter plaintext: ")
 | 
			
		||||
    key = b'input("Enter key: ")'
 | 
			
		||||
    print("DES 128 Bit Cyphertext: ", des_128(ptext, key))
 | 
			
		||||
    print("DES 192 Bit Cyphertext: ", des_192(ptext, key))
 | 
			
		||||
    print("DES 256 Bit Cyphertext: ", des_256(ptext, key))
 | 
			
		||||
    print("AES 128 Bit Cyphertext: ", aes_128(ptext, key))
 | 
			
		||||
    print("AES 192 Bit Cyphertext: ", aes_192(ptext, key))
 | 
			
		||||
    print("AES 256 Bit Cyphertext: ", aes_256(ptext, key))
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
							
								
								
									
										36
									
								
								IS/Lab/Lab2/des.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								IS/Lab/Lab2/des.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,36 @@
 | 
			
		|||
from Crypto.Cipher import DES
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
 | 
			
		||||
def des_de(ctext, key):
 | 
			
		||||
    cipher = des_cipher(key)
 | 
			
		||||
    return cipher.decrypt(ctext).decode('utf-8').rstrip()
 | 
			
		||||
 | 
			
		||||
def despad_key(key):
 | 
			
		||||
    return key.ljust(8)[:8]
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
    print("Your ciphertext: ", ctext)
 | 
			
		||||
    print("Your decrypted plaintext: ", des_de(ctext, key))
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue