SHARE
TWEET

Untitled

a guest May 17th, 2017 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from sage.all import *
  2.  
  3. class Encrypter(object):
  4.  
  5.     def __init__(self, matrixLength):
  6.         super(Encrypter, self).__init__()
  7.         self.numberSpace = ZZ
  8.         self.matrixLength = matrixLength
  9.         self.matrixSpace = MatrixSpace(self.numberSpace, matrixLength, matrixLength)
  10.         self.generateSecretKey()
  11.  
  12.     def generateSecretKey(self):
  13.         self.skVector = random_vector(self.numberSpace, self.matrixLength)
  14.         print(self.skVector)
  15.  
  16.     def encrypt(self, opentext):
  17.         skIndex = randrange(self.matrixLength)
  18.         helpMatrix = self.matrixSpace()
  19.         M = self.matrixSpace()
  20.         DMatrix = self.matrixSpace()
  21.         helpMatrix[skIndex] = self.skVector
  22.         for i in range(self.matrixLength):
  23.             if (i != skIndex):
  24.                 randVector = random_vector(self.numberSpace, self.matrixLength)
  25.                 helpMatrix[i] = randVector
  26.         #if helpMatrix cannot be .transpose() cause determinant = 0
  27.         while (helpMatrix.determinant() == 0):
  28.             i = randrange(self.matrixLength)
  29.             if (i != skIndex):
  30.                 helpMatrix[i] = random_vector(self.numberSpace, self.matrixLength)
  31.         helpMatrix = helpMatrix.transpose()
  32.         DMatrix[skIndex,skIndex] = opentext
  33.         M = helpMatrix * DMatrix * helpMatrix.inverse()
  34.         return M
  35.  
  36.     def decrypt(self, ciphertext):
  37.         result = ciphertext * self.skVector
  38.         for i in range(self.matrixLength):
  39.             if (self.skVector[i] != 0):
  40.                 return result[i] / self.skVector[i]
  41.         print("Error in decrypter the skVector have all zeros")
  42.         return 0
  43.  
  44.  
  45. matrixLength = 3
  46. a = Encrypter(matrixLength)
  47. c1 = a.encrypt(100)
  48. print(c1)
  49. c2 = a.encrypt(224)
  50. print(c2)
  51. cResult = c1 * c2
  52. print(cResult)
  53. a.decrypt(cResult)
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