ValueError in encypted field using filter_in/filter_out

39 views
Skip to first unread message

Maurice Ling

unread,
Jun 9, 2017, 1:05:15 AM6/9/17
to web2py-users
Hi

I am trying to encrypt a field (using pyDes.py which is located in modules folder of my application) in my database using filter_in and filter_out. In pyDes, there is a class called des and I implemented a triple_des function in the database definition (models/cydb.py). The function, triple_des, seems to be working as I can insert a print statement to show the ciphertext and it does show up in console. However, I am getting a ValueError, which I have no idea where it pops out from.

Here is the traceback:

<type 'exceptions.ValueError'> the query contains a null character

Version

web2py™ Version 2.14.6-stable+timestamp.2016.05.10.00.21.47
Python Python 2.7.13: C:\Python27\python.exe (prefix: C:\Python27)

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Traceback (most recent call last):
File "C:\Users\Maurice Ling\Desktop\web2py\gluon\restricted.py", line 227, in restricted
exec ccode in environment
File "C:/Users/Maurice Ling/Desktop/web2py/applications/des_encryption_test/controllers/des_test.py", line 19, in <module>
File "C:\Users\Maurice Ling\Desktop\web2py\gluon\globals.py", line 417, in <lambda>
self._caller = lambda f: f()
File "C:/Users/Maurice Ling/Desktop/web2py/applications/des_encryption_test/controllers/des_test.py", line 15, in new_entry
contents=form.vars.contents)
File "C:\Users\Maurice Ling\Desktop\web2py\gluon\packages\dal\pydal\objects.py", line 726, in insert
ret = self._db._adapter.insert(self, self._listify(fields))
File "C:\Users\Maurice Ling\Desktop\web2py\gluon\packages\dal\pydal\adapters\base.py", line 746, in insert
raise e
ValueError: the query contains a null character

Here are the files that I used and I had attached my sample application.

Can anyone help?

Thank you in advance.

Maurice

File: controllers/des_test.py

session.encryptkey = '12345678901234567890123456789012'

session.cipherkey = hashlib.pbkdf2_hmac('sha512', session.encryptkey, '', 200000, 24)

def new_entry():
    form = FORM(TABLE(
            TR('Title: ', INPUT(_type='text', _name='title', _size=80)),
            TR('Contents: ', TEXTAREA(_type='text', _name='contents'),
            TR('',INPUT(_type='submit', _name='SUBMIT')))))
    if form.accepts(request.vars, session):
        cydb.encrypted_entries2.insert(title=form.vars.title,
                                      contents=form.vars.contents)
        redirect(URL(r=request, f='entries'))
    return dict(form=form)

File: models/cydb.py

import pyDes

def threeDES(data, key, operation, mode='ECB'):
    csysA = pyDes.des(key[:8], 'ECB')
    csysB = pyDes.des(key[8:16], 'ECB')
    csysC = pyDes.des(key[16:], 'ECB')
    if operation == 'encrypt':
        ctext1 = csysA.encrypt(data, ' ')
        ctext2 = csysB.encrypt(ctext1, ' ')
        ctext3 = csysC.encrypt(ctext2, ' ')
        return ctext3
    if operation == 'decrypt':
        ptext1 = csysC.decrypt(data, ' ')
        ptext2 = csysB.decrypt(ptext1, ' ')
        ptext3 = csysA.decrypt(ptext2, ' ')
        return ptext3
   
cydb = SQLDB('sqlite://cydb.db')

cydb.define_table('encrypted_entries2',
                SQLField('title'),
                SQLField('contents'))

cydb.encrypted_entries2.contents.filter_in = lambda value: threeDES(value, session.cipherkey, 'encrypt')
cydb.encrypted_entries2.contents.filter_out = lambda value: threeDES(value, session.cipherkey, 'decrypt')
web2py.app.des_encryption_test.w2p
Reply all
Reply to author
Forward
0 new messages