一个python自定义的加密解密函数,python加密解密,一个python自定义的
一个python自定义的加密解密函数,python加密解密,一个python自定义的
一个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 = 15s1 = encrypt(key, 'hello world')s2 = decrypt(key, s1)print s1,'\n',s2 # HGKGDGDGAGPCIHAGNHDGLG# hello world
相关内容
- python 3个文件合并成3列.py,python.py,"""python 3个
- Windows下Python获取磁盘空闲空间并写入日志,python空闲
- monkeyrunner 代码示例,monkeyrunner示例,'''from com.
- python获取本机网卡的mac地址,python获取网卡mac,python获取
- programming python 之tkinter初学,pythontkinter,#!/usr/bin/e
- 基本字符串压缩,字符串压缩,class Zipper
- python编写小程序(计算器),python小程序,#coding=gbkd
- 有两个文件,一个存储学号,姓名,另一个存储学号,
- python实现堆排序,python堆排序,#沿左,右子节点较大者依
- Python 计算已经过去多少个周末,python已经过去,计算已
评论关闭