--- title: "system.secrets.decrypt" description: "Decrypts the given JSON object containing an encrypted secret." --- # system.secrets.decrypt > Decrypts the given JSON object containing an encrypted secret. This function is used in **Python Scripting**. ## Description Decrypts the given JSON object containing an encrypted secret using the system encryption service. Manage the lifecycle of the returned PyPlaintext by using a `with...as` statement or by calling the `clear()` method. ## Syntax `system.secrets.decrypt(json)` ### Parameters |Type | Parameter | Description | |------- | ------- | ------| |Any| `json` | The JSON object containing the encrypted secret to decrypt.| ### Returns A PyPlaintext instance containing the decrypted secret. ### Scope Gateway, Vision Client, Perspective Session ## PyPlaintext Class It is important to remember to clear the PyPlaintext instance after you are done accessing the secret. The preferred way to do so is use the Python `with...as` statement, as it is an efficient way to manage resource teardown. If the `with...as` statement is not appropriate for your use-case, you must clear the PyPlaintext instance manually using the `clear()` method. * When using the `getSecretAsString()` methods, the returned string representing the secret is not cleaned up until garbage collection reclaims the memory. * When using the `getSecretAsBytes()` method, the byte array returned is a direct reference to the bytes stored in the PyPlaintext instance. Therefore the array returned is cleared when the PyPlaintext is cleared. Refer to the **Decode Returned Java Compatible Bytes** code example below for options on what to do when the pyPlaintext instance is returned with `getSecretAsBytes`. ## Code Examples ```python title="Code Snippet using a with...as Statement" # Decrypt the output from calling system.secrets.encrypt. with system.secrets.decrypt(encrypted) as pyPlaintext: # The original secret is returned by calling methods on the returned PyPlaintext object. # The byteArray returned is a Jython JArray of byte type. byteArray = pyPlaintext.getSecretAsBytes() asString = pyPlaintext.getSecretAsString() asStringCharSet = pyPlaintext.getSecretAsString("UTF-8") ``` ```python title="Code Snippet using the clear() Method" # Decrypt the output from calling system.secrets.encrypt. pyPlaintext = system.secrets.decrypt(encrypted) # The original secret is returned by calling methods on the returned PyPlaintext object. # The byteArray returned is a Jython JArray of byte type. byteArray = pyPlaintext.getSecretAsBytes() asString = pyPlaintext.getSecretAsString() asStringCharSet = pyPlaintext.getSecretAsString("UTF-8") # When done using PyPlaintext, call the clear() method. pyPlaintext.clear() ``` ```python title="Decode Returned Java Compatible Bytes" # The following two examples show how to decode Java compatible bytes # returned from getSecretAsBytes(). byteArray = pyPlaintext.getSecretAsBytes() # Option 1: Use Java String to decode to a string. from java.lang import String stringDecoded = str(String(byteArray, "UTF-8")) # Options 2: Convert to python bytearray and decode to a string. pyByte = bytearray(byteArraySecret) stringDecoded = pyByte.decode("UTF-8") ```