views:

40

answers:

1

Okay, I'm totally new to Python, so I decided to make a simple app. Here is my encryption function:

from Crypto.Cipher import AES
def encPass(login, password):
    keyPhr=os.environ['HOME']+login
    hashObj = hashlib.md5()
    hashObj.update(keyPhr)
    keyPhr=hashObj.hexdigest()
    keyObj=AES.new(keyPhr)
    encPwd=keyObj.encrypt(password+'pssd')
    return encPwd

as you see, it gets login and pass, and encrypt pass with binding to pc.

The problem is when i try to feed encPws to sqlite3 and insert it into a table, it says:

sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.

If i try to put encPwd in unicode() or hex():

TypeError: hex() argument can't be converted to hex
UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 0: unexpected code byte

I guess it's because i'm newbie, but what should I do? The same with Blowfish instead of AES

+1  A: 

You can use buffer and insert the encPwd as a blob.

Or you can use something like base64 to convert your encPwd to ASCII (which is a subset of UTF8) so you can insert it as a string.

Doug Currie
base64 says "incorrect padding". this is kind of dark sorcery?
creitve
Okay, thank you anyway! I'll return to this later.
creitve
Only b64decode would give "incorrect padding". You use b64encode to turn encPwd into a string suitable for the database, and use b64decode to convert back into the original encPwd value.
Doug Currie
Oh, yes, that's what autocomplete is all about, thanks a lot!
creitve