本身沒有內置到Python的加密算法。但是,您可能需要查看Python Cryptography Toolkit(PyCrypt)。我只修改了它,但它在Python的文檔cryptographic services中被引用。這裏是你怎麼可以加密使用AES PyCrypt一個字符串的例子:
from Crypto.Cipher import AES
from urllib import quote
# Note that for AES the key length must be either 16, 24, or 32 bytes
encryption_obj = AES.new('abcdefghijklmnop')
plain = "Testing"
# The plaintext must be a multiple of 16 bytes (for AES), so here we pad it
# with spaces if necessary.
mismatch = len(plain) % 16
if mismatch != 0:
padding = (16 - mismatch) * ' '
plain += padding
ciph = encryption_obj.encrypt(plain)
# Finally, to make the encrypted string safe to use in a URL we quote it
quoted_ciph = quote(ciph)
你會那麼做你的URL的這一部分,也許作爲一個GET請求的一部分。
解密,只是扭轉過程;假設encryption_obj
如上創建的,你已經檢索到的URL的相關部分,這將做到這一點:
from urllib import unquote
# We've already created encryption_object as shown above
ciph = unquote(quoted_ciph)
plain = encryption_obj.decrypt(ciph)
您也可以考慮不同的方法:一個簡單的方法是哈希主鍵(如果你願意,可以用鹽)並將散列和pk存儲在數據庫中。給用戶散列作爲鏈接的一部分,當他們返回並呈現散列時,查找相應的pk並返回適當的對象。 (如果你想要走這條路線,檢查出的內置庫hashlib)
舉個例子,你有這樣的事情在models.py定義:
class Pk_lookup(models.Model):
# since we're using sha256, set the max_length of this field to 32
hashed_pk = models.CharField(primary_key=True, max_length=32)
key = models.IntegerField()
你」 d使用類似如下生成視圖中的散列:
import hashlib
import Pk_lookup
hash = hashlib.sha256()
hash.update(str(pk)) # pk has been defined previously
pk_digest = hash.digest()
lookup = Pk_lookup(hashed_pk=pk_digest,key=pk)
lookup.save()
請注意,你必須引用此版本爲好;如果你願意,你可以使用hexdigest()
而不是digest
(你不必引用結果字符串),但你必須調整字段的長度爲64.
你爲什麼不只是使用'session'此? – voyager 2010-02-18 18:43:01
我認爲你的意思是檢查用戶身份驗證,是的,我確實會使用他們,但我也不想顯示項目PK給用戶,而是隱藏他們。 – Hellnar 2010-02-18 18:48:59