Added Lab2/Lab3 IS

This commit is contained in:
sherlock 2025-08-12 02:15:01 +05:30
parent a8ced71b76
commit 4cab136b7a
6 changed files with 636 additions and 0 deletions

39
IS/Lab/Lab3/RSA.py Normal file
View file

@ -0,0 +1,39 @@
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import binascii
def rsa_keygen(bits=2048):
key = RSA.generate(bits)
return key
def rsa_en(ptext, pub_key):
cipher = PKCS1_OAEP.new(pub_key)
ctext = cipher.encrypt(ptext.encode('utf-8'))
return ctext
def rsa_de(ctext, priv_key):
cipher = PKCS1_OAEP.new(priv_key)
decrypted = cipher.decrypt(ctext)
return decrypted.decode('utf-8')
def main():
print("Welcome to RSA")
ptext = input("Enter plaintext: ")
# RSA key pair
key = rsa_keygen()
pub_key = key.publickey()
priv_key = key
print("\nPublic Key (n, e):")
print("n =", hex(pub_key.n))
print("e =", pub_key.e)
ctext = rsa_en(ptext, pub_key)
print("\nYour ciphertext (hex):", binascii.hexlify(ctext).decode())
decrypted = rsa_de(ctext, priv_key)
print("Your decrypted plaintext:", decrypted)
if __name__ == '__main__':
main()