django,python和链接加密 [英] django, python and link encryption

查看:190
本文介绍了django,python和链接加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要安排一些用于生成用户特定链接的encrpytion。用户将点击此链接,在其他视图下,与加密字符串相关的链接将被解密,并返回结果。

I need to arrange some kind of encrpytion for generating user specific links. Users will be clicking this link and at some other view, related link with the crypted string will be decrypted and result will be returned.

为此,我需要某种加密函数消耗一个数字(或一个字符串),该数字(或一个字符串)是我选定项目的主键,绑定到用户帐户,也消耗某种种子,并生成将在其他页面解密的加密代码。

For this, I need some kind of encryption function that consumes a number(or a string) that is the primary key of my selected item that is bound to the user account, also consuming some kind of seed and generating encryption code that will be decrypted at some other page.

所以这样的东西

my_items_pk = 36 #primary key of an item
seed = "rsdjk324j23423j4j2" #some string for crypting
encrypted_string = encrypt(my_items_pk,seed)
#generates some crypted string such as "dsaj2j213jasas452k41k"
and at another page:
decrypt_input = encrypt(decypt,seed)
print decrypt_input
#gives 36

我想让我的种子成为某种主要变量(不是一些类),为此目的(即一些数字或字符串)。

I want my "seed" to be some kind of primary variable (not some class) for this purpose (ie some number or string).

如何我在python和django下实现了这一点?

How can I achieve this under python and django ?

推荐答案

本身没有内置Python的加密算法。但是,您可能需要查看 Python加密工具包(PyCrypt)。我只是修改了它,但是它在Python的文档中引用了加密服务。以下是使用PyCrypt如何使用AES加密字符串的示例:

There are no encryption algorithms, per se, built in to Python. However, you might want to look at the Python Cryptography Toolkit (PyCrypt). I've only tinkered with it, but it's referenced in Python's documentation on cryptographic services. Here's an example of how you could encrypt a string with AES using 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)

然后,您可以将这部分网址作为GET请求的一部分。

You would then make this part of your URL, perhaps as part of a GET request.

要解密,只需逆转进程;假设 encryption_obj 是如上创建的,并且您已经检索到URL的相关部分,则可以这样做:

To decrypt, just reverse the process; assuming that encryption_obj is created as above, and that you've retrieved the relevant part of the URL, this would do it:

from urllib import unquote

# We've already created encryption_object as shown above

ciph = unquote(quoted_ciph)
plain = encryption_obj.decrypt(ciph)

你也可以考虑一种不同的方法:一个简单方法是将主键(使用盐,如果你愿意)哈希,并将hash和pk存储在数据库中。为用户提供哈希作为其链接的一部分,当他们返回并显示哈希时,查找相应的pk并返回相应的对象。 (如果你想去这条路线,看看内置的图书馆 hashlib 。)

You also might consider a different approach: one simple method would be to hash the primary key (with a salt, if you wish) and store the hash and pk in your database. Give the user the hash as part of their link, and when they return and present the hash, look up the corresponding pk and return the appropriate object. (If you want to go this route, check out the built-in library hashlib.)

举个例子,你可以在models.py中定义类似的东西:

As an example, you'd have something like this defined in 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()

在视图中使用以下内容生成散列:

And you'd generate the hash in a view using something like the following:

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。

Note that you'd have to quote this version as well; if you prefer, you can use hexdigest() instead of digest (you wouldn't have to quote the resulting string), but you'll have to adjust the length of the field to 64.

这篇关于django,python和链接加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆