Home > Blockchain >  Cannot convert 'str' object to bytes error in RSA python
Cannot convert 'str' object to bytes error in RSA python

Time:04-26

I've been using rsa python library to encrypt and decrypt data, all is working fine. However when I save the encrypted data to a CSV file using pandas and if try to decrypt the saved data value by extracting it from the CVS file, then it no longer works

Below is my python code

import rsa
import pandas as pd


key = int(input("enter an interger key value : ")) #512
paasword = input("input pass : ")

publicKey, privateKey = rsa.newkeys(key) 
encpass = rsa.encrypt(paasword.encode(),publicKey)

print("\noriginal string: ", paasword)
print("\nencrypted string: ", encpass)

# Save to New CSV file


data={'Name':['Jhon'], 'Password':[encpass], } #new dict
df = pd.DataFrame(data) # create new pandas DataFrame

df.to_csv('my_data.csv', index=False) # write a new csv file

# Extract form CSV file

df = pd.read_csv("my_data.csv",index_col = 0) #using 0th column (Name) as index
find_password = df['Password']['Jhon'] #column id , index of that row;

print(find_password)

decpass = rsa.decrypt(find_password, privateKey).decode()
print("\ndecrypted string: ", decMessage)

> enter an integer key value: 512
> input pass: abhijeetbyte123

> original string:  abhijeetbyte123

> encrypted string:  b',.\x89\xb8&"\xdc|\x97.................................


> error : TypeError: cannot convert 'str' object to bytes

What should I do to fix this error

CodePudding user response:

The problem is the "serialization" to a CSV file of the encrypted password. encpass is of type bytes but Pandas writes a literal string representation to the CSV file. Thus, Pandas will also read this back as a string ("b'$\\xaa...'") which only looks similar to a bytes object.

If you want to write the password to a file, I suggest that you encode it with base64 and create a UTF-8 string representation of it. UTF-8 alone will most likely produce encoding errors for (pseudo-)random byte streams.

import rsa
from base64 import b64encode, b64decode
import pandas as pd


key = int(input("enter an interger key value : ")) #512
paasword = input("input pass : ")

publicKey, privateKey = rsa.newkeys(key) 
encpass = rsa.encrypt(paasword.encode(),publicKey)

print("\noriginal string: ", paasword)
print("\nencrypted string: ", encpass)

# Encode encrypted password to base64 and produce UTF-8 conform string
b64_enc_pass = b64encode(encpass).decode()

# Save to New CSV file
data={'Name':['Jhon'], 'Password':[b64_enc_pass], } #new dict
df = pd.DataFrame(data) # create new pandas DataFrame

df.to_csv('my_data.csv', index=False) # write a new csv file

# Extract form CSV file

df = pd.read_csv("my_data.csv",index_col = 0) #using 0th column (Name) as index
find_password = df['Password']['Jhon'] #column id , index of that row;

# Decode encrypted password from UTF-8 encoded string
find_password = b64decode(find_password.encode())

print(find_password)

decpass = rsa.decrypt(find_password, privateKey).decode()
print("\ndecrypted string: ", decpass)
  • Related