Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- isprime = lambda number: all(number % i for i in range(2, number))
- class RSA:
- e = 3
- def __init__(self, *args, **kwargs):
- if args: self.p, self.q = args
- else:
- kwargs = kwargs or {'start': 100, 'end':1000}
- simples = [i for i in range(kwargs['start'], kwargs['end']) if isprime(i)]
- self.p = random.choice(simples)
- while True:
- self.q = random.choice(simples)
- if self.q != self.p: break;
- print (self.p, self.q)
- self.n = self.q * self.p
- self.euler_value = (self.p-1)*(self.q-1)
- def create_secret_key(self, start=1000000, end=10000000):
- for i in range(start, end):
- if (i * self.e) % self.euler_value == 1:
- self.secret_exp = i
- return self.secret_exp, self.n
- generate_open_key = lambda self: (self.e, self.n,)
- encrypt_value = lambda self, value: (value ** self.e) % self.n
- decrypt_value = lambda self, value: (value ** self.secret_exp) % self.n
- def main():
- rsa = RSA(3557, 2579)
- print(rsa)
- s_key = rsa.create_secret_key()
- print(s_key)
- o_key = rsa.generate_open_key()
- print(o_key)
- val = int(input("Enter value for decryption: ")) or 111111
- enc_value = (rsa.encrypt_value(val))
- print (enc_value)
- input("Enter any key for decryption ")
- print(rsa.decrypt_value(enc_value))
- # print( isprime(6111579))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement