On 5/13/07, elemental <kai....@gmail.com> wrote:
UnicodeDecodeError at /register/
'ascii' codec can't decode byte 0xb4 in position 0: ordinal not in
range(128)
For reference, here is the save portion of the model:
def save(self):
key = ezPyCrypto.key(512)
self.passport = key.encString(self.passport)
First, I'd recommend making a slight modification to this... you're
continually re-encrypting the passport information... so if you call
.save() twice you'll get an double-encrypted .passport value.
One option can be found in the Satchmo Project's handling of credit
card data. [0] They use a storeCC method and a decryptedCC property
to handle encryption/decryption.
Another option is to use getter and setter methods, and access them
via a property... this way encryption and decryption are transparent
operations (this may or may not be desirable, depending on your
environment).
So, for example:
class YourModel(models.Model):
...
encrypted_passport = models.CharField(...)
def get_passport(self):
#decrypt and return decrypted value
def set_passport(self, value):
#encrypt and store in self.encrypted_passport
passport = property(get_passport, set_passport)
Getting back to your original question, this sounds like you're
getting binary data back from encString (it looks like it's built on
top of pycrypto[1], so that's likely). You can try the following to
convert it to ASCII text before saving it:
self.passport = key.encString(...).encode('base64')
Then, just run .decode('base64') on the string before running decryption.
Hope that helps,
- Ben
[0]
http://www.satchmoproject.com/trac/browser/satchmo/trunk/satchmo/payment/models.py
[1] http://www.amk.ca/python/code/crypto.html