def encrypt(self, msg): Basic.validate_type(msg, types.StringType) #make hmac mac = self.make_hmac(msg) inbuf = cStringIO.StringIO(mac + msg) outbuf = cStringIO.StringIO() outbuf.write(self.encryptCipher.update(inbuf.read())) outbuf.write(self.encryptCipher.final()) #no idea what this does because it is undocumented return outbuf.getvalue()
def encrypt(self, msg): Basic.validate_type(msg, types.StringType) #make hmac mac = self.make_hmac(msg) inbuf = cStringIO.StringIO(mac + msg) outbuf = cStringIO.StringIO() outbuf.write(self.encryptCipher.update(inbuf.read())) outbuf.write(self.encryptCipher.final() ) #no idea what this does because it is undocumented return outbuf.getvalue()
def decrypt(self, msg, usePadding=True): """Decrypt msg (str) with key, return the result (as a string) @param msg: message to be decrypted @type msg: string @param usePadding: use padding if True, otherwise the message isn't padded (as for blinding) @type usePadding: bool @return: encrypted message as string""" Basic.validate_type(msg, types.StringType) if usePadding: return M2Crypto.m2.rsa_private_decrypt(self.key.rsa, msg, M2Crypto.RSA.pkcs1_padding) else: return M2Crypto.m2.rsa_private_decrypt(self.key.rsa, msg, M2Crypto.RSA.no_padding)
def encrypt(self, msg): Basic.validate_type(msg, types.StringType) #make hmac mac = self.make_hmac(msg) inbuf = cStringIO.StringIO(mac + msg) outbuf = cStringIO.StringIO() outbuf.write(self.encryptCipher.update(inbuf.read())) outbuf.write(self.encryptCipher.final()) #no idea what this does because it is undocumented encryptedKeyConstructor = self.key.encrypt(self.randomData) encryptedKeyConstructor = struct.pack('!%ss'% BANK_KEY_LENGTH, encryptedKeyConstructor) msg = encryptedKeyConstructor+outbuf.getvalue() return msg
def decrypt(self, encryptedMsg): Basic.validate_type(encryptedMsg, types.StringType) #decrypt the message inbuf = cStringIO.StringIO(encryptedMsg) outbuf = cStringIO.StringIO() outbuf.write(self.decryptCipher.update(inbuf.read())) outbuf.write(self.decryptCipher.final()) msg = outbuf.getvalue() mac = msg[:32] msg = msg[32:] #validate the HMAC if self.make_hmac(msg) != mac: raise Exception('HMAC does not authenticate, is something bad going on?') return msg
def encrypt(self, msg): Basic.validate_type(msg, types.StringType) #make hmac mac = self.make_hmac(msg) inbuf = cStringIO.StringIO(mac + msg) outbuf = cStringIO.StringIO() outbuf.write(self.encryptCipher.update(inbuf.read())) outbuf.write(self.encryptCipher.final() ) #no idea what this does because it is undocumented encryptedKeyConstructor = self.key.encrypt(self.randomData) encryptedKeyConstructor = struct.pack('!%ss' % BANK_KEY_LENGTH, encryptedKeyConstructor) msg = encryptedKeyConstructor + outbuf.getvalue() return msg
def decrypt(self, encryptedMsg): Basic.validate_type(encryptedMsg, types.StringType) #decrypt the message inbuf = cStringIO.StringIO(encryptedMsg) outbuf = cStringIO.StringIO() outbuf.write(self.decryptCipher.update(inbuf.read())) outbuf.write(self.decryptCipher.final()) msg = outbuf.getvalue() mac = msg[:32] msg = msg[32:] #validate the HMAC if self.make_hmac(msg) != mac: raise Exception( 'HMAC does not authenticate, is something bad going on?') return msg
def blind(self, message, r, length=None): """Blind a message using random number r (assuming length of the key by default) @param message: string to be blinded @type message: string @param r: blinding factor @type r: long @param length: length of the message after blinding (needed to convert from a long to a string). @type long: None or int @return: string of message blinded with r assuming length of n """ Basic.validate_type(message, types.StringType) Basic.validate_type(r, types.LongType) message = Basic.bytes_to_long(message) tmp = pow(r, self.e, self.n) tmp = (message * tmp) % self.n return Basic.long_to_bytes(tmp, length or self.keyLen)
def decrypt(self, msg): Basic.validate_type(msg, types.StringType) #decrypt the message encRandomData, msg = Basic.read_message('!%ss'%( BANK_KEY_LENGTH), msg) randomData = self.key.decrypt(encRandomData[0]) self.make_sym_key(randomData) inbuf = cStringIO.StringIO(msg) outbuf = cStringIO.StringIO() outbuf.write(self.decryptCipher.update(inbuf.read())) outbuf.write(self.decryptCipher.final()) msg = outbuf.getvalue() mac = msg[:32] msg = msg[32:] #validate the HMAC if self.make_hmac(msg) != mac: raise Exception('HMAC does not authenticate, is something bad going on?') return msg
def decrypt(self, msg): Basic.validate_type(msg, types.StringType) #decrypt the message encRandomData, msg = Basic.read_message('!%ss' % (BANK_KEY_LENGTH), msg) randomData = self.key.decrypt(encRandomData[0]) self.make_sym_key(randomData) inbuf = cStringIO.StringIO(msg) outbuf = cStringIO.StringIO() outbuf.write(self.decryptCipher.update(inbuf.read())) outbuf.write(self.decryptCipher.final()) msg = outbuf.getvalue() mac = msg[:32] msg = msg[32:] #validate the HMAC if self.make_hmac(msg) != mac: raise Exception( 'HMAC does not authenticate, is something bad going on?') return msg
def verify(self, msg, sig): """Return boolean of whether sig is a valid signature of msg with this key.""" Basic.validate_type(msg, types.StringType) Basic.validate_type(sig, types.StringType) return self.key.verify(Crypto.make_hash(msg), sig, 'sha256')