Django开发密码管理表实例【附源码】 (5)

额,上边的例子是不是有点复杂了,拿我们项目里边的实例为例,就是判断id是否存在,如果存在则更新,如果不存在则创建

object, created = Table.objects.update_or_create( id=postdata.get('id'), defaults=postdata )

update_or_create方法返回结果为一个元组(object, created),object为新建或者更新的对象,created为一个布尔值,表示是新建还是更新,True为新建

密码加密

对于密码加密,我们知道加密算法有很多,究竟哪种适合我们呢?

看一下我们的需求,存储的时候要加密存储,但也要能对加密后的数据进行解密以获取原始密码,那么就要求加密算法既支持加密,也支持解密,对于md5这种只支持加密的单向算法就不能选择了,在支持加解密的算法中应用最为广泛的当属RSA了,我们最终也选择了RSA,其实单单对于这个场景来说用3DES之类的对称加密就够了,速度也会快很多,但是考虑到系统里边有其他的加密需求选择RSA能满足更多情景。

RSA加密算法非常复杂,感兴趣的可以查询专业资料,RSA需要一对秘钥称为公钥和私钥,公钥可以对字符串进行加密生成加密字符串,拿私钥可以将加密字符串还原。

python3中RSA的使用

生成RSA秘钥对代码

from Cryptodome.PublicKey import RSA def create_rsa_key(): '''生成RSA秘钥对''' try: # 选择秘钥位数,位数越高越安全,同时加密速度也越慢 key = RSA.generate(2048) encrypted_key = key.exportKey(pkcs=8) public_key = key.publickey().exportKey().decode('utf-8') private_key = encrypted_key.decode('utf-8') return {'state': 1, 'message': {'public_key': public_key, 'private_key': private_key}} except Exception as err: return {'state': 0, 'message': str(err)} if __name__ == '__main__': print(create_rsa_key())

执行脚本生成RSA秘钥对,将RSA秘钥对配置在加解密的代码中

RSA加密解密代码

import binascii from Cryptodome.PublicKey import RSA from Cryptodome.Cipher import PKCS1_v1_5 class RsaCrypto(): '''RSA 加解密''' def __init__(self): self.private_key = '''-----BEGIN PRIVATE KEY----- MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDLetN9MfsVWZj2 K+i1vPQqeDKb8Fe52pHKvRtQggTF+x3YRCyk7UNQ27VupohM8t+Qzi2Zm3GnZY5X H7W8UGLnI1X1ykpWOW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qbodpcT+e45h+uuwPE8lFAPfxfbqc7/mCOXB70whlhFzaMtK27FIsJAoGAVtTJ qnF8bPpeWYO7Lx7TOu55Ofk9tcIHOc0csj/JKY3iMY80rBjU+p8JBJRMsfOX7Qxp jnshzdQsB75e5ZTptf9AJUWBzAs7cpiI2KMdtGTFCRlL7dMOpGS2gleK3JDD8+4G JNBR9EisSyQEg6EF3LgViMLH/G95OfNKQatCE+MCgYEAhRIuEMaL2idD9NKOVdgC fuSbiP5G69IBVD4uoDYFPQJjxqVOg3pORa8+cJTe+ZFaCkTGV3112eM5Vtm4Vd9Q pOh7VgJP1l3puZnUoSWGoWansx6aKok5FwuUrZWPjqr/Zrre8XXJyaiR520tuf0i StMfNAsijJgi2pq2PTMovhE= -----END PRIVATE KEY-----''' self.public_key = '''-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy3rTfTH7FVmY9ivotbz0 Kngym/BXudqRyr0bUIIExfsd2EQspO1DUNu1bqaITPLfkM4tmZtxp2WOVx+1vFBi 5yNV9cpKVjllbnj9yggOwwrbMA6eYZCYB8zz2Qvw5RYdLgqQJlJIBz+1lF0WaEfZ pnFTnVjfXPjVktCQOnMB0iPie/BHW9ZX3n+WDsLHi9hojpgYie0x2H468BcBkGmV Y4bKdz5Le2Lfx7nOTVNt+MAPY/kxRZ7m6TqxPZhU/KgpIRYViBHDbLk6gwwyPQV9 JS2hBJ/r4PXJxGfe1YHUEBtg+js8OBpXaX6KtVwegal4PmLAr8g/4f+EAy6Xo2cv 1QIDAQAB -----END PUBLIC KEY-----''' def encrypt(self, plaintext): '''加密方法''' try: recipient_key = RSA.import_key(self.public_key) cipher_rsa = PKCS1_v1_5.new(recipient_key) en_data = cipher_rsa.encrypt(plaintext.encode('utf-8')) hex_data = binascii.hexlify(en_data).decode('utf-8') return {'state': 1, 'message': hex_data} except Exception as err: return {'state': 0, 'message': str(err)} def decrypt(self, hex_data): '''解密方法''' try: private_key = RSA.import_key(self.private_key) cipher_rsa = PKCS1_v1_5.new(private_key) en_data = binascii.unhexlify(hex_data.encode('utf-8')) data = cipher_rsa.decrypt(en_data, None).decode('utf-8') return {'state': 1, 'message': data} except Exception as err: return {'state': 0, 'message': str(err)} if __name__ == '__main__': print(RsaCrypto().encrypt())

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyzdfy.html