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)

EncryptedData.Content = content

return EncryptedData.Encrypt()

def decrypt(key,content): # 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,自己写加密解密算法,比如: 代码示例:

#!/bin/python

#

def encrypt(key, s):

b = bytearray(str(s).encode("gbk"))

n = len(b) # 求出 b 的字节数

c = bytearray(n*2)

j = 0

for i in range(0, n):

b1 = b[i]

b2 = b1 ^ key # b1 = b2^ key

c1 = b2 % 16

c2 = b2 // 16 # b2 = c2*16 + c1

c1 = c1 + 65

c2 = c2 + 65 # c1,c2都是0~15之间的数,加上65就变成了A-P 的字符的编码

c[j] = c1

c[j+1] = c2

j = j+2

return c.decode("gbk")

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, s1)

print s1,'\n',s2

# HGKGDGDGAGPCIHAGNHDGLG

# hello world

4,python可以把python源码文件编译成pyc二进制格式的文件,不显示源码,也算是一种加密方法吧。

可以按如下的方法操作:

执行命令python -m py_compile create_slave.py

直接生成一个create_slave.pyc文件,然后可以用create_slave.pyc来替换create_slave.py作为脚本来执行。

python加密与解密_Python字符串加密与解密的方法总结相关推荐

  1. python关键字中文意思_python 字符串只保留汉字的方法

    如下所示: def is_chinese(uchar): """判断一个unicode是否是汉字""" if uchar >= u'\ ...

  2. python整数格式化表达式_Python字符串格式化表达式和格式化方法

    Python格式化字符串由两种方式可以选择:一种是格式化表达式(Formatting Expression),一种是格式化方法(Formatting Method).其中格式化表达式在全Python版 ...

  3. 【C# 练习】编写一个应用程序用来输入的字符串进行加密,对于字母字符串加密规则如下:‘a’→’d’ ‘b’→’e’ ‘w’→’z’ …… ‘x’→’a’ ‘y’→’b’ ‘z’→’c’

    题目: 编写一个应用程序用来输入的字符串进行加密,对于字母字符串加密规则如下: 'a'→'d' 'b'→'e' 'w'→'z' -- 'x'→'a' 'y'→'b' 'z'→'c''A'→'D' 'B ...

  4. python中字符串查找子串_Python字符串中查找子串的方法

    Python字符串中查找子串的方法 发布于 2015-04-12 08:58:32 | 230 次阅读 | 评论: 0 | 来源: 网友投递 Python编程语言Python 是一种面向对象.解释型计 ...

  5. 使用python hashlib模块给明文字符串加密,以及如何撞库破解密码

    文章目录: 1 hashlib介绍 2 hashlib模块使用 2.1 查看hashlib中有哪些hash算法 2.2 对字符串进行加密 2.3 对于数据比较大,加密可以分块,结果一样 2.4 has ...

  6. java 字符串加密 解密_java字符串加密解密

    java字符串加密解密 try { String test = "123456789@fdj.com"; EncryptionDecryption des = new Encryp ...

  7. python字符串成熟编码_python字符串转公式两种方法获取网页编码python版

    在web开发的时候我们经常会遇到网页抓取和分析,各种语言都可以完成这个功能.我喜欢用python实现,因为python提供了很多成熟的模块,可以很方便的实现网页抓取. 但是在抓取过程中会遇到编码的问题 ...

  8. python 字符串拼接_Python字符串拼接的6种方法(转)

    add by zhj: 对于多行字符串连接,第6种连接方法很方便,连接时不会添加额外的空格. 1. 加号 第一种,有编程经验的人,估计都知道很多语言里面是用加号连接两个字符串,Python里面也是如此 ...

  9. python编程字符输入连接_python字符串连接的N种方式总结

    python中有很多字符串连接方式,今天在写代码,顺便总结一下: 最原始的字符串连接方式:str1 + str2 python 新字符串连接语法:str1, str2 奇怪的字符串方式:str1 st ...

最新文章

  1. Kotlin 二分法算法游戏--猜价格
  2. IDEA统一设置编码为utf-8编码及tomcat 乱码问题的解决
  3. start ssh-agent
  4. SpringBoot+MyBatis+Shiro 搭建杂谈
  5. MapReduce Java API实例-统计出现过的单词
  6. java 内存模型 ——学习笔记
  7. 软件设计师 - 数据流图
  8. python3获取用户输入_python3.4控制用户输入与输出
  9. 06-04 Jenkins 权限控制
  10. Ubuntu+CUDA+OpenCV+Caffee安装
  11. ps自定义(新建)图框工具
  12. Spoken English-口语-发音规则
  13. DateCalander
  14. 小孩用的台灯什么样的品牌好?2023儿童台灯灯具品牌排行榜
  15. Win10系统无法打开桌面的个性化设置、显示设置及任务栏设置等,处理方法及参考链接如下。
  16. 树莓派获取LAN ip地址并发送到微信
  17. hualinux 1.25:Web开发技术发展史
  18. 五一清北学堂培训之Day 3之DP
  19. 糖尿病足溃疡疗法行业调研报告 - 市场现状分析与发展前景预测
  20. java jdbc gbase_Gbase JDBC 应用示例

热门文章

  1. 语谱图(二) Spectrogram 的产生
  2. 软件工程实验一:详细设计及编码
  3. 2021-06-03 JavaGUI 贪吃蛇+图片素材免费
  4. phalcon第二个例子invo
  5. 变量、变量命名及赋值
  6. 网络变压器产品可以做到多大的绝缘电压?4000V可以吗?
  7. 防止产品发布灾难的7个软件测试技巧
  8. ei capitan mysql_OSX 10.11 EI Capitan初步上手体验以及开发环境配置
  9. 严肃不搞笑的小黄鸭调试法
  10. 基础综合练习题 (Java)