python加密与解密_Python字符串加密与解密的方法总结

python对字符串做加密解密处理,大致有三种方法:base64,win32com.client和自定义加密解密算法,最安全可靠的方式,建议是自写加密解密算法。

1,使用base64: 代码示例:

#!/bin/python

#edit: www.#

#

import base64

s1 = base64.encodestring('hello world')

s2 = base64.decodestring(s1)

print s1,s2

# aGVsbG8gd29ybGQ=\n

# hello world

注: 此方法简单便不安全,当别人拿到你的密文时,即可解密得到明文;

不过可以把密文字符串进行处理,如字母转换成数字或是特殊字符等,自己解密的时候在替换回去在进行base64.decodestring,要安全很多。

2,使用win32com.client 代码示例:

#!/bin/python

#

import win32com.client

def encrypt(key,content): # key:密钥,content:明文

EncryptedData = win32com.client.Dispatch('CAPICOM.EncryptedData')

EncryptedData.Algorithm.KeyLength = 5

EncryptedData.Algorithm.Name = 2

EncryptedData.SetSecret(key)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值