SHARE
TWEET

Untitled

a guest May 17th, 2017 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from sage.all import *
  2. class Encrypter(object):
  3.     publicKey = []
  4.     """docstring for ClassName"""
  5.     def __init__(self, baseNumber):
  6.         super(Encrypter, self).__init__()
  7.         self.baseNumber = baseNumber
  8.         self.privateKey = 0
  9.         self.sum = 0
  10.         self.generatePrivateKey()
  11.         self.generatePublicKey()
  12.  
  13.     def generatePrivateKey(self):
  14.         flag = False
  15.         while flag == False:
  16.             privateKey = randrange(10000, 2147483648)
  17.             if gcd(privateKey, self.baseNumber) == 1:
  18.                 flag = True
  19.                 self.privateKey = privateKey
  20.  
  21.     def generatePublicKey(self):
  22.         count = 100
  23.         sum = 0
  24.         for i in xrange(0, count):
  25.             e = randrange(2147483648) % self.baseNumber
  26.             mult = randrange(2,5)
  27.             aKey = self.privateKey * mult + e * self.baseNumber
  28.             sum += aKey
  29.             self.publicKey.append(aKey)
  30.         self.sum = sum
  31.  
  32.     def encrypt(self, openText):
  33.         b = 0
  34.         randomValue = 0
  35.         for i in xrange(0,100):
  36.             randomValue = randrange(0,2)
  37.             if randomValue == 1:
  38.                 b = b + self.publicKey[i]
  39.         cipherText = b + openText
  40.         return cipherText
  41.  
  42.     def decrypt(self, cipherText):
  43.         while cipherText < 0:
  44.             cipherText += self.privateKey
  45.         openText = cipherText % self.privateKey
  46.         return openText % self.baseNumber
  47.  
  48. #main
  49. encrypter = Encrypter(3000)
  50.  
  51. number1 = 30
  52. number2 = 50
  53.  
  54. code1 = encrypter.encrypt(number1)
  55. print(code1)
  56. code2 = encrypter.encrypt(number2)
  57. print(code2)
  58.  
  59. resultCode = code1 + code2
  60. print(resultCode)
  61.  
  62. resultNumber = encrypter.decrypt(resultCode)
  63. print(resultNumber)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top