'Decryption not working - how to get raw data from csv/pandas - python

Below is my code for decrypting from a csv file stored on DropBox. I get the user to type in their ID, I match this with a database containing hashed values, and then I use the ID typed in to search my stored csv file for the matching row. I then place all the row values into my decryption function.

Also im aware my variable names/formatting is awful im just using this code as a prototype as of right now.

My results are being printed as such:

b'b\xebS\x1b\xc8v\xe2\xf8\xa2\\x84\x0e7M~\x1b'

b'\x01B#6i\x1b\xfc]\xc3\xca{\xd5{B\xbe!'

b'in*V7\xf3P\xa0\xb2\xc5\xd2\xb7\x1dz~\x95'

I store my key and IV so they are always the same, yet the decryption doesnt seem to work. My only thinking is perhaps my data is changed somehow when stored in a csv or pandas table etc. does anyone know what the issue would be or if the bytes can be altered when stored/imported to dataframe?

also maybe i am extracting the data from my csv to pandas incorrectly?

 def login():
        import sqlite3
        import os.path

def decoder():
    from Crypto.Cipher import AES
    import hashlib
    from secrets import token_bytes

    cursor.execute(
        '''
        Select enc_key FROM Login where ID = (?);
        ''',
        (L_ID_entry.get(), ))

    row = cursor.fetchone()
    if row is not None:
        keys = row[0]

        #design padding function for encryption
        def padded_text(data_in):
            while len(data_in)% 16 != 0:
                data_in = data_in + b"0"
            return data_in

        #calling stored key from main file and reverting back to bytes
        key_original = bytes.fromhex(keys)
        mode = AES.MODE_CBC

        #model
        cipher = AES.new(key_original, mode, IV3)

        #padding data
        p4 = padded_text(df1.tobytes())
        p5 = padded_text(df2.tobytes())
        p6 = padded_text(df3.tobytes())

        #decrypting data
        d_fname = cipher.decrypt(p4)
        d_sname = cipher.decrypt(p5)
        d_email = cipher.decrypt(p6)

        print(d_fname)
        print(d_sname)
        print(d_email)

#connecting to db
try:
    conn = sqlite3.connect('login_details.db')
    cursor = conn.cursor()
    print("Connected to SQLite")

except sqlite3.Error as error:
    print("Failure, error: ", error)
finally:
    #downloading txt from dropbox and converting to dataframe to operate on
    import New_user
    import ast
    _, res = client.files_download("/user_details/enc_logins.csv")
    with io.BytesIO(res.content) as csvfile:
        with open("enc_logins.csv", 'rb'):
            df = pd.read_csv(csvfile,  names=['ID', 'Fname', 'Sname', 'Email'], encoding= 'unicode_escape')
            newdf = df[(df == L_ID_entry.get()).any(axis=1)]
            print(newdf)
            df1 = newdf['Fname'].to_numpy()
            df2 = newdf['Sname'].to_numpy()
            df3 = newdf['Email'].to_numpy()
            print(df1)
            print(df2)
            print(df3)
            csvfile.close()

    decoder()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source