# Decrpyt Credentials on Python code

**URL:** <https://forum.uipath.com/t/decrpyt-credentials-on-python-code/505525>\
**Category:** Studio\
**Tags:** workflow-diff, question, studio, python, orchestrator, credentials, activities\
**Created:** [January 8, 2023, 6:29am UTC](https://forum.uipath.com/t/decrpyt-credentials-on-python-code/505525 "2023-01-08T06:29:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![2022learncode](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/2022learncode/32/263455_2.png) [@2022learncode](https://forum.uipath.com/u/2022learncode)\
**Post date:** [January 8, 2023, 6:29am UTC](https://forum.uipath.com/t/decrpyt-credentials-on-python-code/505525/1 "2023-01-08T06:29:13Z")

</div>

Hi,

Please share an simple idea on directly passing the orchestrator DB asset from UiPath to my python code as parameter and Decrypt the DB password on python code to access DB connection.

---

<div class="post-metadata">

**Author:** ![mahmoud.zaky](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/mahmoud.zaky/32/301869_2.png) [@mahmoud.zaky](https://forum.uipath.com/u/mahmoud.zaky)\
**Post date:** [January 8, 2023, 7:12am UTC](https://forum.uipath.com/t/decrpyt-credentials-on-python-code/505525/2 "2023-01-08T07:12:47Z")

</div>

It will be better for performance to access and get all data you need direct from Orcastrator API using below URL

[https://docs.uipath.com/orchestrator/reference/api-references](https://docs.uipath.com/orchestrator/reference/api-references)

---

<div class="post-metadata">

**Author:** ![2022learncode](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/2022learncode/32/263455_2.png) [@2022learncode](https://forum.uipath.com/u/2022learncode)\
**Post date:** [January 8, 2023, 11:23am UTC](https://forum.uipath.com/t/decrpyt-credentials-on-python-code/505525/3 "2023-01-08T11:23:16Z")

</div>

An samples?

---

<div class="post-metadata">

**Author:** ![mahmoud.zaky](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/mahmoud.zaky/32/301869_2.png) [@mahmoud.zaky](https://forum.uipath.com/u/mahmoud.zaky)\
**Post date:** [January 8, 2023, 4:05pm UTC](https://forum.uipath.com/t/decrpyt-credentials-on-python-code/505525/4 "2023-01-08T16:05:29Z")

</div>

@2022learncode you can find below examples on the Reset Project

> **[UiPath Connector Guide](https://postman.uipath.rocks/)**
>
> \#### What is this about:
> 
> The UiPath Orchestrator API is used by third-party platforms to build connectors/adapters. Common use cases of connectors are to start jobs in UiPath Orchestrator or create queue items which can be processes by robots. This...

also find below video playlist include all details you need 🙂

https://www.youtube.com/embed/G3u7OLJia5A?feature=oembed&wmode=opaque&list=PLGClccnZ8FnRkbXvs6tp-AAmQAzLAhtVz

---

<div class="post-metadata">

**Author:** ![Legoman](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@Legoman](https://forum.uipath.com/u/Legoman)\
**Post date:** [January 8, 2023, 8:30pm UTC](https://forum.uipath.com/t/decrpyt-credentials-on-python-code/505525/5 "2023-01-08T20:30:42Z")

</div>

Here’s a github implementation of using Windows SecureStrings in Python.  
Has to be run by the same user that owns the password though.

> <https://github.com/aznashwan/py-securestring/blob/master/securestring.py>

---

<div class="post-metadata">

**Author:** ![Kaviyarasu\_N](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/kaviyarasu_n/32/244106_2.png) [@Kaviyarasu\_N](https://forum.uipath.com/u/Kaviyarasu_N)\
**Post date:** [January 9, 2023, 3:25am UTC](https://forum.uipath.com/t/decrpyt-credentials-on-python-code/505525/6 "2023-01-09T03:25:31Z")

</div>

> [@2022learncode](#):
>
> Please share an simple idea on directly passing the [orchestrator](https://docs.uipath.com/orchestrator/docs/introduction) DB asset from UiPath to my python code as parameter and Decrypt the DB password on python code to access DB connection.

Hi @2022learncode

Plese follow the below steps in UiPath & python code as well

1. In UiPath, create a Python script activity and pass the orchestrator DB asset as an input parameter to the Python script.
2. In the Python script, retrieve the input parameter and store it in a variable.
3. Use the `decrypt` function from the `cryptography` library to decrypt the DB password.
4. Use a Python library such as `pyodbc` to connect to the DB using the decrypted password and other necessary connection details.

**Sample Code:**

import pyodbc  
from cryptography.fernet import Fernet

def decrypt(key, ciphertext):  
f = Fernet(key)  
return f.decrypt(ciphertext)

def main(db\_asset):  
# Retrieve the password from the DB asset and decrypt it  
password = decrypt(db\_asset[‘Key’], db\_asset[‘Password’])

```
# Connect to the DB using pyodbc
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};'
                            'SERVER=' + db_asset['Server'] + ';'
                            'DATABASE=' + db_asset['Database'] + ';'
                            'UID=' + db_asset['Username'] + ';'
                            'PWD=' + password)

# Do something with the connection
cursor = connection.cursor()
cursor.execute("SELECT * FROM my_table")
rows = cursor.fetchall()
for row in rows:
    print(row)

```

Regards,  
Kaviyarasu N
