python字符串加密与反解密
Posted jl_bai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python字符串加密与反解密相关的知识,希望对你有一定的参考价值。
在生产中会遇到很多情况是需要加密字符串的(如访问或存储密码)这些需求造就了需要字符串加密,以及反解密的问题,推荐两种方法来实现,下附代码:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def encrypt(key, s): b = bytearray(str(s).encode("utf-8")) n = len(b) c = bytearray(n*2) j = 0 for i in range(0, n): b1 = b[i] b2 = b1 ^ key c1 = b2 % 19 c2 = b2 // 19 c1 = c1 + 46 c2 = c2 + 46 c[j] = c1 c[j+1] = c2 j = j+2 return c.decode("utf-8") def decrypt(ksa, s): c = bytearray(str(s).encode("utf-8")) n = len(c) if n % 2 != 0: return "" n = n // 2 b = bytearray(n) j = 0 for i in range(0, n): c1 = c[j] c2 = c[j + 1] j = j + 2 c1 = c1 - 46 c2 = c2 - 46 b2 = c2 * 19 + c1 b1 = b2 ^ ksa b[i] = b1 return b.decode("utf-8") print(encrypt(11, ‘password123‘)) print(decrypt(11, ‘43<303>3/[email protected]‘))
第二种:
from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex class EncryptStr(object): def __init__(self, key): self.key = key self.mode = AES.MODE_CBC def encrypt(self, text): cryptor = AES.new(self.key, self.mode, self.key) length = 16 count = len(text) if (count % length != 0): add = length - (count % length) else: add = 0 text = text + (‘