'base 64 decoded not the same as original encoded value
Ive created a login system that uses AES to encrypt user input. Below is the code that takes the input, pads, encrypts and then converts to base64. this is then stored in a csv which is also in the code below.
#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
mode = AES.MODE_CBC
f_name_in = F_name.get().encode('utf8')
S_name_in = S_name.get().encode('utf8')
email_in = Email_22.get().encode('utf8')
p = padded_text(f_name_in)
p2 = padded_text(S_name_in)
p3 = padded_text(email_in)
print(p)
print(len(p))
cipher = AES.new(key, mode, IV=IV)
fname_enc = cipher.encrypt(p)
Sname_enc = cipher.encrypt(p2)
email_enc = cipher.encrypt(p3)
print(fname_enc)
fname_enc64 = base64.b64encode(fname_enc)
Sname_enc64 = base64.b64encode(Sname_enc)
email_enc64 = base64.b64encode(email_enc)
print(fname_enc64)
print(len(fname_enc64))
def save_to_csv():
import pandas as pd
import numpy as np
import csv
data = [[ID,fname_enc64,Sname_enc64,email_enc64]]
header = ['ID','Fname','Sname','Email']
df = pd.DataFrame(data)
df.to_csv('enc_logins.csv', mode='a', index=False, header=None, encoding='utf8')
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(df)
dropbox_storage.dropbox_upload()
Send_to_DB()
I then retrieve the file, import into a dataframe and base 64 decode. But this doesnt provide me with the same output as the original data before I base64 encoded.
original encrypted data via AES:
b'\x04\xe0l\xfe\x8b\xa5R, -Sl\xf2&\xf4\x9a'
above padded and encrypted to base64:
b'BOBs/oulUiwgLVNs8ib0mg=='
data after retrieving and decoding(correct and matches above):
b'BOBs/oulUiwgLVNs8ib0mg=='
result of decoding this from base64:
b'l\x13\x81\xb3\xfa.\x95H\xb0\x80\xb5M\xb3\xc8\x9b\xd2h'
What could be causing this incorrect base64 decoding? im being really stupid and missing something obvious im sure.
code for decoding below
bs64 = lambda x: base64.b64decode(x)
with open('enc_logins.csv', 'r', encoding='utf8') as csvfile:
df = pd.read_csv(csvfile, names=['ID', 'Fname', 'Sname', 'Email'],delimiter=",", na_filter=False, encoding='utf8')
newdf = df[df['ID'] == L_ID_entry.get()]
print(newdf)
df1 = newdf['Fname'].apply(bs64)
df2 = newdf['Sname'].apply(bs64)
df3 = newdf['Email'].apply(bs64)
print(df1)
print("df1 above")
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 |
|---|
