def encrypt_data(data, encryption_key, iv=None): """ Function to encrypt data based on encryption key and iv Parameters: - data is each item in inData or outData part of workorder request as per TCF API 6.1.7 Work Order Data Formats - encryption_key is the key used to encrypt the data - iv is an initialization vector if required by the data encryption algorithm. The default is all zeros.iv must be a unique random number for every encryption operation. """ logger.debug("encrypted_session_key: %s", encryption_key) if iv is not None: encrypted_data = crypto.SKENC_EncryptMessage(encryption_key, iv, data) else: encrypted_data = crypto.SKENC_EncryptMessage(encryption_key, data) return encrypted_data
try: iv = crypto.SKENC_GenerateIV() except Exception as exc: logger.error("ERROR: Symmetric encryption iv generation test failed: ", exc) sys.exit(-1) try: key = crypto.SKENC_GenerateKey() except Exception as exc: logger.error("ERROR: Symmetric encryption key generation test failed: ", exc) sys.exit(-1) try: crypto.SKENC_EncryptMessage(iv, None, msg) logger.error("ERROR: Symmetric encryption invalid key detection test " + "failed: not detected.") sys.exit(-1) except Exception as exc: if (type(exc) == ValueError): logger.debug( "Symmetric encryption invalid key detection test successful!") else: logger.error( "ERROR: Symmetric encryption invalid key detection " + "test failed: ", exc) sys.exit(-1) try: crypto.SKENC_EncryptMessage(None, key, msg) logger.error("ERROR: Symmetric encryption invalid iv detection " +