您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python字符串加密解密的三种方法分享(base64 win32com)

5b51 2022/1/14 8:18:34 python 字数 3213 阅读 377 来源 www.jb51.cc/python

1.最简单的方法是用base64: 复制代码代码如下:importbase64 s1=base64.encodestring(\'helloworld\')s2=base64.decodestring(s1)prints1,s2

概述

1. 最简单的方法是用base64:

s1 = base64.encodestring('hello world')
s2 = base64.decodestring(s1)
print s1,s2

# aGVsbG8gd29ybGQ=\n
# hello world

Note: 这是最简单的方法了,但是不够保险,因为如果别人拿到你的密文,也可以自己解密来得到明文


2. 第二种方法是使用win32com.client

def decrypt(key,content:密文
    EncryptedData = win32com.client.Dispatch('CAPICOM.EncryptedData')
    EncryptedData.Algorithm.KeyLength = 5
    EncryptedData.Algorithm.Name = 2
    EncryptedData.SetSecret(key)
    EncryptedData.Decrypt(content)
    str = EncryptedData.Content
    return str

s1 = encrypt('lovebread','hello world')
s2 = decrypt('lovebread',s1)
print s1,s2

# MGEGCSsGAQQBgjdYA6BUMfigCisGAQQBgjdYAwGGrdBCAgMCAAECAmYBAgFABAgq
# GpllWj9cswQQh/fnBUZ6ijwKDTH9DLZmBgQYmfaZ3VFyS/lq391oDtjlcRFGnXpx
# lG7o
# hello world

3. 还有就是自己写加密解密算法,比如:

def decrypt(key,s):
    c = bytearray(str(s).encode("gbk"))
    n = len(c) # 计算 b 的字节数
    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 - 65
        c2 = c2 - 65
        b2 = c2*16 + c1
        b1 = b2^ key
        b[i]= b1
    try:
        return b.decode("gbk")
    except:
        return "Failed"

key = 15
s1 = encrypt(key,'hello world')
s2 = decrypt(key,'\n',s2

# HGKGDGDGAGPCIHAGNHDGLG
# hello world

总结

以上是编程之家为你收集整理的python字符串加密解密的三种方法分享(base64 win32com)全部内容,希望文章能够帮你解决python字符串加密解密的三种方法分享(base64 win32com)所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶