1

I am one error while saving data into DB using Django and Python. Error is given below.

Error :

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/lampp/htdocs/d30/carClinic_vulnerable/bookingservice/views.py", line 153, in signsave
    passw.save()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 806, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 836, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 922, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 961, in _do_insert
    using=using, raw=raw)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1063, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 84, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/operations.py", line 135, in last_executed_query
    params = self._quote_params_for_last_executed_query(params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/operations.py", line 124, in _quote_params_for_last_executed_query
    return cursor.execute(sql, params).fetchone()
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.

I am explaining my model file below.

class User(models.Model):
    """docstring for User"""

    uname = models.CharField(max_length=200)
    password = models.CharField(max_length=200)
    date = models.DateTimeField(default=datetime.now, blank=True)
    raw_password = models.CharField(max_length=200,blank=True, null=True)

Here are the datas to save into DB.

if request.method == 'POST':
        name = request.POST.get('uname')
        password = request.POST.get('pass')
        sec_pass = password
        con_password = request.POST.get('conpass')
        length = 16 - (len(password) % 16)
        password += chr(length)*length
        obj = AES.new('this is a carkey', AES.MODE_CBC, 'This is an IV456')
        enpass = obj.encrypt(password)
        if sec_pass == con_password:
            passw = User(
                uname=name,
                password=enpass,
                raw_password=password,
            )
            passw.save()
    return render(request, 'bookingservice/login.html', {})

Here when I am encrypting the data and trying to save into database the above error is coming.PLease help me to resolve this error.

  • 2
    **Why are you doing this**? Django has a perfectly good and secure authentication system already. Why are you mucking about with crypto? – Daniel Roseman Jun 26 '17 at 14:37
  • I have one requirement like this. Can you solve this error ? –  Jun 26 '17 at 14:38
  • I'm guessing ``enpass`` is bytes and not actually a string. You will probably need to decode it? – Grimmy Jun 26 '17 at 14:42
  • @Grimmy : Can you write your solution ? –  Jun 26 '17 at 14:44
  • base64 is definitely the answer here as @EduardStepanov mentioned in the answer. ``encrypt`` returns binary data and cannot be decoded into a string. – Grimmy Jun 26 '17 at 14:52

1 Answers1

1

May be this answer will be helpful for you.

Or you can use base64 to encode your encoded password in string:

import base64
# your code before if statement is here
b64enpass = base64.b64encode(enpass)
if sec_pass == con_password:
    passw = User(
        uname=name,
        password=b64enpass,
        raw_password=password,
    )
    passw.save()

But of course in this case you have to decode password after receiving User objects from db.

Eduard Stepanov
  • 1,183
  • 8
  • 9