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 + ( * add)
        self.ciphertext = cryptor.encrypt(text)
        return b2a_hex(self.ciphertext)

    # 解密后,去掉补足的空格用strip() 去掉
    def decrypt(self, text):
        cryptor = AES.new(self.key, self.mode, self.key)
        plain_text = cryptor.decrypt(a2b_hex(text))
        return plain_text.decode(utf-8).strip()

if __name__ == __main__:
    pc = EncryptStr(keyskeyskeyskeys)  # 初始化密钥
    e = pc.encrypt("passwd123")
    d = pc.decrypt("0e0dbd0509f9eaaafd420b8a2c72cbde")
    print(e, d)

 

以上是关于python字符串加密与反解密的主要内容,如果未能解决你的问题,请参考以下文章

Python 加密解密算法

可以解密加密数据的片段吗?

Python实现希尔加密/解密

使用Python pyDes和base64模块对字符串进行加密和解密

Python 加密解密

Python_字符串简单加密解密