OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

pyAesCrypt crypt dictionary, save cipher text to file then read file and decrypt

  • Thread starter Thread starter Cai Allin
  • Start date Start date
C

Cai Allin

Guest
So I am trying to encrypt a dictionary and save it as cypher text, then open and decrypt the file so my program can use the passwords.
I do not want to save the decrypted file and it should only be stored in a variable.

My current .py is as follows:

Code:
import io
import json
import pyAesCrypt


password = 'some_password'
bufferSize = 64 * 1024


def enc():
    loaded_settings = {'pass1': 'xxx',
                       'pass2': 'ccc',
                       'key': 'ddd',
                       'secret': 'sss'}

    # binary data to be encoded
    user_encode_data = json.dumps(loaded_settings).encode('utf-8')

    # input plaintext binary stream
    fIn = io.BytesIO(user_encode_data)

    # initialize ciphertext binary stream
    fCiph = io.BytesIO()

    # encrypt stream
    pyAesCrypt.encryptStream(fIn, fCiph, password, bufferSize)

    # print encrypted data
    print("This is the ciphertext:\n" + str(fCiph.getvalue()))
    with open("Output.txt", "w") as text_file:
        text_file.write(str(fCiph.getvalue()))


def dec():
    with open("Output.txt", "r") as text_file:
        cipher_text = text_file
        fCiph = io.BytesIO(cipher_text.read().encode())
    # get ciphertext length
    ctlen = len(fCiph.getvalue())

    # go back to the start of the ciphertext stream
    fCiph.seek(0)

    # initialize decrypted binary stream
    fDec = io.BytesIO()

    # decrypt stream
    pyAesCrypt.decryptStream(fCiph, fDec, password, bufferSize, ctlen)

    # print decrypted data
    print("Decrypted data:\n" + str(fDec.getvalue()))

    # decrypted data back as dict
    output_dict = json.loads(fDec.getvalue())

    print(output_dict['pass1'])


enc()
dec()

I am getting the error ValueError: File is corrupted or not an AES Crypt (or pyAesCrypt) file.
Is there something wrong with the way I am opening the cypher text?
<p>So I am trying to encrypt a dictionary and save it as cypher text, then open and decrypt the file so my program can use the passwords.<br />
I do not want to save the decrypted file and it should only be stored in a variable.</p>
<p>My current .py is as follows:</p>
<pre><code>import io
import json
import pyAesCrypt


password = 'some_password'
bufferSize = 64 * 1024


def enc():
loaded_settings = {'pass1': 'xxx',
'pass2': 'ccc',
'key': 'ddd',
'secret': 'sss'}

# binary data to be encoded
user_encode_data = json.dumps(loaded_settings).encode('utf-8')

# input plaintext binary stream
fIn = io.BytesIO(user_encode_data)

# initialize ciphertext binary stream
fCiph = io.BytesIO()

# encrypt stream
pyAesCrypt.encryptStream(fIn, fCiph, password, bufferSize)

# print encrypted data
print("This is the ciphertext:\n" + str(fCiph.getvalue()))
with open("Output.txt", "w") as text_file:
text_file.write(str(fCiph.getvalue()))


def dec():
with open("Output.txt", "r") as text_file:
cipher_text = text_file
fCiph = io.BytesIO(cipher_text.read().encode())
# get ciphertext length
ctlen = len(fCiph.getvalue())

# go back to the start of the ciphertext stream
fCiph.seek(0)

# initialize decrypted binary stream
fDec = io.BytesIO()

# decrypt stream
pyAesCrypt.decryptStream(fCiph, fDec, password, bufferSize, ctlen)

# print decrypted data
print("Decrypted data:\n" + str(fDec.getvalue()))

# decrypted data back as dict
output_dict = json.loads(fDec.getvalue())

print(output_dict['pass1'])


enc()
dec()
</code></pre>
<p>I am getting the error <code>ValueError: File is corrupted or not an AES Crypt (or pyAesCrypt) file.</code><br />
Is there something wrong with the way I am opening the cypher text?</p>
 

Latest posts

Online statistics

Members online
1
Guests online
2
Total visitors
3
Top