繁体   English   中英

基本的crypto()和Delete()函数

[英]Basic encrypt() and decrypt() function

我的views.py中有一个函数,在某行我会请求一个ID的GET请求。 获得ID后,我想对该ID进行加密,然后再对其进行解密。 所以我需要两个功能

def encrypt(id):#let say the id is 100
   #do something
   return encrypted_id # 6-digit let say 985634

def decrypt(encrypted_id): # Here enter 985634
    #do something     
    return decrypted_id  # i should get 100

我已经阅读了许多帖子,但没有找到一种简单干净的方法来将其应用到我的views.py中。

sha1:您无法解密(为加密实现)Mee 2 M2。 AES处理8位数字的16位数字

我也尝试生成6位数的随机数,但是这个想法也没有希望。 有人可以告诉一个方法怎么做吗? 提前致谢

使用AES(来自pycrypto ),但是在加密之前填充纯文本。

本示例使用空字符(ASCII 0)填充明文

from Crypto.Cipher import AES
import base64

MASTER_KEY="Some-long-base-key-to-use-as-encryption-key"

def encrypt_val(clear_text):
    enc_secret = AES.new(MASTER_KEY[:32])
    tag_string = (str(clear_text) +
                  (AES.block_size -
                   len(str(clear_text)) % AES.block_size) * "\0")
    cipher_text = base64.b64encode(enc_secret.encrypt(tag_string))

    return cipher_text

解密后,删除空字符:

def decrypt_val(cipher_text):
    dec_secret = AES.new(MASTER_KEY[:32])
    raw_decrypted = dec_secret.decrypt(base64.b64decode(cipher_text))
    clear_val = raw_decrypted.decode().rstrip("\0")
    return clear_val

我遇到了完全相同的问题,并通过使用Hashids解决了它。

就这么简单

hashids = Hashids(salt="this is my salt")
hashed = hashids.encode(id) # to encode
id = hashids.decode(hashed) # to decode

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM