Facebook
From Hoang, 1 Month ago, written in Python.
Embed
Download Paste or View Raw
Hits: 156
  1. from Crypto.Util.number import long_to_bytes, bytes_to_long, isPrime, inverse
  2. import random, time
  3.  
  4. class RSA:
  5.     def __init__(self):
  6.         random.seed(int(time.time()))
  7.         p = self.getPrime(random.getrandbits(512))
  8.         q = self.getPrime(random.getrandbits(512))
  9.         assert isPrime(p) == True and isPrime(q) == True
  10.         self.n = p * q
  11.         self.phi = (p-1)*(q-1)
  12.         self.e = 65537
  13.         self.d = inverse(self.e, self.phi)
  14.         self.flag = "ISPCTF{}"
  15.         self.enc_flag = hex(pow(bytes_to_long(self.flag.encode('utf-8')), self.e, self.n))
  16.    
  17.     def getPrime(self, num):
  18.         if num % 2 == 0:
  19.             num += 1
  20.         while not isPrime(num):
  21.             num += 2
  22.         return num
  23.     def title(self):
  24.         return '''
  25.        ----------------------------------------------------------------
  26.        |     WELCOME TO MY RSA SERVICE ENCRYPT AND DECRYPT FOR FREE   |
  27.        ----------------------------------------------------------------                                                
  28.        |        1. Give me a message and then receive a encryption    |
  29.        |        2. Give me a decryption and then receive a message    |
  30.        |        3. Receive encrypted the flag as a gift               |
  31.        |        4. Give me the key (d) to get the flag as rewards     |
  32.        |--------------------------------------------------------------|
  33.        '''
  34.        
  35.     def encrypt(self, mess):
  36.         return hex(pow(bytes_to_long(mess.encode('utf-8')), self.e,self.p*self.q))
  37.  
  38.     def decrypt(self, isGetFlag, enc, key):
  39.         try:
  40.             if isGetFlag:
  41.                 key = int(key, 16)
  42.             else:
  43.                 key = self.d
  44.             mess = long_to_bytes(pow(int(enc, 16), key, self.n)).decode('utf-8')
  45.             return mess
  46.         except Exception as e:
  47.             return "Invalid Key"
  48.        
  49.     def process(self):
  50.         print(self.title())
  51.         while True:
  52.             try:
  53.                 choice = int(input("Your choice: "))
  54.                 if choice == 1:
  55.                     mess = input("What would you like to encrypt? Type here: ")
  56.                     enc = self.encrypt(mess)
  57.                     print(f"Here your cipher: {enc}")
  58.                 elif choice == 2:
  59.                     enc = input("What would you like to decrypt? Type here: ")
  60.                     data = self.decrypt(isGetFlag=False, enc=enc, key=None)
  61.                     print(f"Here your data: {data}")
  62.                 elif choice == 3:
  63.                     print(f"Here your encryption of the flag: {self.enc_flag}")
  64.                 elif choice == 4:
  65.                     key = input("What is your key? Type here: ")
  66.                     data = self.decrypt(isGetFlag=True, enc=self.enc_flag, key=key)
  67.                     if data == self.flag:
  68.                         print(f"Congratulation !! Please submit your flag and receive your reward !! {self.flag}")
  69.                     else:
  70.                         print(data)
  71.                 else:
  72.                     print(self.title())
  73.             except Exception as e:
  74.                 print("Something went wrong")
  75.                
  76.                
  77. app = RSA()
  78. app.process()