django中自定义field的to_python函数不工作?
在用pydes写一个自定义的加密字段,
from pyDes import triple_des, PAD_PKCS5
from binascii import unhexlify as unhex
from binascii import hexlify as dohex
class BaseEncryptedField(models.CharField):
def __init__(self, *args, **kwargs):
self.td = triple_des(unhex('c35414909168354f77fe89816c6b625bde4fc9ee51529f2f'))
super(BaseEncryptedField, self).__init__(*args, **kwargs)
def to_python(self, value):
return self.td.decrypt(unhex(value), padmode=PAD_PKCS5)
def get_db_prep_value(self, value):
return dohex(self.td.encrypt(value, padmode=PAD_PKCS5))
该字段在数据库中存储成功,但是在获取时,并不能得到加密的数据。请问写的有问题吗?
xxrlo
11 years, 2 months ago