def encrypt(self, plaintext, stable=False): result = plaintext log('info', 'Going to encrypt text') if plaintext: if isinstance(plaintext, unicode): # swig wrapper for _xssl.so won't currently marshal unicode strings plaintext = plaintext.encode('utf-8') try: version, key, iv = self.getKey() if not key or not iv: # the key must have been tempered with self.generateNewKey() version, key, iv = self._getKey() if key and iv: pyssl = pySSL.getInstance() # encrypt using default cipher, passcode (key+iv), with salt, base64-encoded results # (note the passcode is generated from key+iv for backward compatibility) passcode = key+iv if stable: encryptedText = pyssl.encrypt_passcode(plaintext, passcode, True, use_salt=False) else: encryptedText = pyssl.encrypt_passcode(plaintext, passcode, True) # format: VVV(key version) + encryptedText result = self._toHex(version>>8)[1] + self._toHex(version & 0xff) + encryptedText else: log('error', 'Failed to encrypt data! Plain-text is returned!') except: log('error', 'Exception, failed to encrypt data! Plain-text is returned!') raise OperationError('Exception, Failed to encrypt data! Plain-text is returned!') return result
def _decrypt(self, encryptedText): version = int(encryptedText[0:3]) plaintext = encryptedText version, key, iv = self.getKey(version) if key and iv: pyssl = pySSL.getInstance() # decrypt using default cipher, passcode (key+iv), base64-encoded input # (note the passcode is generated from key+iv for backward compatibility) passcode = key+iv plaintext = pyssl.decrypt_passcode(encryptedText[3:], passcode, True) else: log('error', 'Failed to decrypt data! Encrypted text is returned!') return plaintext