def attack(public_key_file, cipher): key = open(public_key_file, 'r') rsakey = RSA.importKey(key) s = 2 modified = square_multiply(s, rsakey.e, rsakey.n) cipher = int(binascii.hexlify(cipher), 16) modified = (cipher * modified) % rsakey.n return binascii.unhexlify(hex(modified)[2:-1])
def attack(public_key_file,cipher): key=open(public_key_file,'r') rsakey=RSA.importKey(key) s=2 modified = square_multiply(s,rsakey.e,rsakey.n) cipher = int(binascii.hexlify(cipher),16) modified = (cipher*modified)%rsakey.n return binascii.unhexlify(hex(modified)[2:-1])
def decrypt_RSA(private_key_file, cipher, state='sm'): key = open(private_key_file,'r').read() rsakey = RSA.importKey(key) if state == 'sm': cipher_int = pyrsa_sq_mul.unpack_bigint(cipher) decrypted_byte_message_int = pyrsa_sq_mul.square_multiply(cipher_int, rsakey.d, rsakey.n) decrypted_byte_message_str = pyrsa_sq_mul.pack_bigint(decrypted_byte_message_int) elif state == 'part3': cipher_rsa = PKCS1_OAEP.new(rsakey) decrypted_byte_message_str = cipher_rsa.decrypt(cipher) return decrypted_byte_message_str
def encrypt_RSA(public_key_file, message, state='sm'): key = open(public_key_file,'r').read() rsakey = RSA.importKey(key) if state == 'sm': if isinstance(message, int): byte_message_int = message else: byte_message_int = pyrsa_sq_mul.unpack_bigint(message) encrypt_byte_message_int = pyrsa_sq_mul.square_multiply(byte_message_int, rsakey.e, rsakey.n) encrypt_byte_message_str = pyrsa_sq_mul.pack_bigint(encrypt_byte_message_int) elif state == 'part3': cipher_rsa = PKCS1_OAEP.new(rsakey) encrypt_byte_message_str = cipher_rsa.encrypt(message) return encrypt_byte_message_str
parser = argparse.ArgumentParser(description='RSA') parser.add_argument('-pub', dest='pub', help='public key') parser.add_argument('-priv', dest='priv', help='private key') parser.add_argument('-message', dest='message', help='message') args = parser.parse_args() pub = args.pub priv = args.priv message = open(args.message).read() ciphertext = encrypt_RSA(pub, message) plaintext = decrypt_RSA(priv, ciphertext) print plaintext signature = sign_RSA(priv, message) verify = verify_sign(pub, signature, message) signature = bin(100) key = open(pub, 'r') rsakey = RSA.importKey(key) message = bin(square_multiply(100, rsakey.e, rsakey.n)) verify_sign(pub, signature, message) message = bin(100) cipher = encrypt_RSA(pub, message) modified = attack(pub, cipher) decrypt = decrypt_RSA(priv, modified) print "ENCRYPTING:", message print "CIPHER:", cipher print "MODIFIED:", modified print "DECRYPTED:", ord(decrypt) print ""
parser.add_argument('-pub', dest='pub',help='public key') parser.add_argument('-priv', dest='priv',help='private key') parser.add_argument('-message', dest='message', help='message') args=parser.parse_args() pub=args.pub priv=args.priv message=open(args.message).read() ciphertext = encrypt_RSA(pub,message) plaintext = decrypt_RSA(priv,ciphertext) print plaintext signature = sign_RSA(priv, message) verify = verify_sign(pub,signature,message) signature = bin(100) key=open(pub,'r') rsakey=RSA.importKey(key) message = bin(square_multiply(100,rsakey.e,rsakey.n)) verify_sign(pub,signature,message) message=bin(100) cipher = encrypt_RSA(pub,message) modified = attack(pub,cipher) decrypt = decrypt_RSA(priv,modified) print "ENCRYPTING:",message print "CIPHER:",cipher print "MODIFIED:",modified print "DECRYPTED:",ord(decrypt) print ""