# Encryption converter

User can change state of wallet encryption in any moment. We have built-in converter from .dat to .txt and opposite. The mechanism doesn't requier special skills. Program opens "C:\Low Caps Hub Terminal" and adjustes searching for .dat or .txt files, according to your choice.

<figure><img src="https://3670432737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwJuUkk9Y24vVhd9jDCn1%2Fuploads%2FT3gZ65Hfm3J72rz6yU2Q%2Fxxxxx.png?alt=media&#x26;token=9edb45cc-d952-4feb-9280-0e033983e09d" alt=""><figcaption></figcaption></figure>

**.txt to .dat**

If you decide to encrypt your wallet, you need to set the encryption key. Just after that, your .txt file will be replaced by .dat.

```python
def encrypt_txt_to_dat(self):
    directory = "C:\\Low Caps Hub Terminal"
    if os.path.exists(directory):
        filepath = filedialog.askopenfilename(
            title="Select .txt Wallet File",
            filetypes=[("Text Files", "*.txt")],
            initialdir=directory
        )
    else:
        self.show_message("Create/Import wallet first.", "Error")
        return

    if not filepath:
        return
    with open(filepath, 'r') as file:
        wallet_data = file.read()
    passphrase, dont_encrypt = self.prompt_for_passphrase("Encrypt Wallet", allow_no_encrypt=True)
    if dont_encrypt:
        self.show_message("Encryption canceled. Wallet remains unencrypted.", "Info")
        return
    if not passphrase:
        self.show_message("Passphrase is required to encrypt the wallet.", "Error")
        return
    encrypted_data = encrypt_data(wallet_data.encode('utf-8'), passphrase)
    encrypted_filepath = filepath.replace('.txt', '.dat')
    with open(encrypted_filepath, 'wb') as file:
        file.write(encrypted_data)
    self.show_message(f"Encrypted wallet saved to {encrypted_filepath}", "Encryption Successful")
    os.remove(filepath)
```

\
\&#xNAN;**.dat to .txt**

If you would like do decrypt your wallet, you can do it. Of course during it, you need to enter your key/password as well.

```python
def decrypt_dat_to_txt(self):
    directory = "C:\\Low Caps Hub Terminal"
    if os.path.exists(directory):
        filepath = filedialog.askopenfilename(
            title="Select .dat Wallet File",
            filetypes=[("DAT Files", "*.dat")],
            initialdir=directory
        )
    else:
        self.show_message("Create/Import wallet first.", "Error")
        return

    if not filepath:
        return
    passphrase, _ = self.prompt_for_passphrase("Decrypt Wallet", allow_no_encrypt=False)
    if not passphrase:
        self.show_message("Passphrase is required to decrypt the wallet.", "Error")
        return
    with open(filepath, 'rb') as file:
        encrypted_data = file.read()
    decrypted_data = decrypt_data(encrypted_data, passphrase)
    if decrypted_data is None:
        self.show_message("Failed to decrypt the wallet. Incorrect passphrase or corrupted file.", "Error")
        return
    decrypted_filepath = filepath.replace('.dat', '.txt')
    with open(decrypted_filepath, 'w') as file:
        file.write(decrypted_data.decode('utf-8'))
    self.show_message(f"Decrypted wallet saved to {decrypted_filepath}", "Decryption Successful")
    os.remove(filepath)
```
