def obtainKey(self, request, cert): key = self.decrypt(algs.int_repr(request['Key'])) signature = self.decrypt(algs.int_repr(request['Signature'])) length = request['Length'] verified = self.verifyKey(key, signature, cert) if verified: self.__obtained_keys.append( {'Key': algs.hex_repr(key, length), 'From': cert}) return { 'verified': verified, 'key': algs.hex_repr(key, length) }
def generateKeyToAbonent(self, length, cert): k = random.randint(2**(length - 1), 2**length) self.__generated_keys.append( {'Key': algs.hex_repr(k, length), 'To': cert}) e = cert['Public Key Info']['Exponent'] n = algs.int_repr(cert['Public Key Info']['Modulus']) l = cert['Public Key Info']['Modulus Length'] k1 = self.encrypt(k, cert) s1 = self.encrypt(self.signKey(k, length), cert) return { 'Key': algs.hex_repr(k1, l), 'Length': length, 'Signature': algs.hex_repr(s1, l) }
def getCertificate(self): return { 'Subject': self.__name, 'Issuer': 'Grubian Evhen', 'Public Key Info': { 'Algorithm': 'PKCS #1 RSA with md5', 'Modulus Length': self.__length, 'Exponent': self.__public_key[0], 'Modulus': algs.hex_repr(self.__public_key[1], self.__length) } }
def sign(self, msg): msg_raw = msg if type(msg) == str: msg = msg.encode('utf-8') md5 = hashlib.md5(msg) msg = md5.digest() msg = int.from_bytes(msg, 'big') return { 'Message': msg_raw, 'Signature': algs.hex_repr(pow(msg, self.__private_key, self.__public_key[1]), self.__length) }