本文实例讲述了Python实现简单的可逆加密程序。分享给大家供大家参考。具体如下:
Python代码如下:
复制代码 代码如下:#coding=utf-8
'''''
Description: 可逆的加密与解密
Environment: python2.5.x
Author:idehong@gmail.com
'''
import os
import sys
class Code(object):
'''''可逆的加密与解密'''
def __init__(self, key = "idehong@gmail.com"):
self.__src_key = key
self.__key = self.__get_strascii(self.__src_key, True)
def encode(self, value):
'''''加密函数, 加密后为一串数字'''
return "%d" % (self.__get_strascii(value, True) ^ self.__key)
def decode(self, pwd):
'''''解密函数'''
if self.is_number(pwd):
return self.__get_strascii( (int(pwd)) ^ self.__key, False )
else:
print 'require number.'
def reset_key(self, key):
'''''重新设置key'''
self.__src_key = key
self.__key = self.__get_strascii(self.__src_key, True)
#===============================================================================
# 内部调用接口
#===============================================================================
def __get_strascii(self, value, bFlag):
if bFlag:
return self.__get_str2ascii(value)
else:
return self.__get_ascii2str(value)
def __get_str2ascii(self, value):
ls = []
for i in value:
ls.append( self.__get_char2ascii( i ) )
return long("".join(ls))
def __get_char2ascii(self, char):
'''''获取单个字符的acsii码值'''
try:
return "%03.d" % ord(char)
except (TypeError, ValueError):
print "key error."
exit(1)
def __get_ascii2char(self, ascii):
if self.is_ascii_range(ascii):
return chr(ascii)
else:
print "ascii error(%d)" % ascii
exit(1)
def __get_ascii2str(self, n_chars):
ls = []
s = "%s" % n_chars
n, p = divmod(len(s), 3)
if p > 0:
nRet = int(s[0 : p])
ls.append( self.__get_ascii2char(nRet))
pTmp = p
while pTmp < len(s):
ls.append( self.__get_ascii2char( int(s[pTmp: pTmp + 3])) )
pTmp += 3
return "".join(ls)
#================================================================================
# 工具接口
#================================================================================
def is_number(self, value):
try:
int(value)
return True
except (TypeError, ValueError):
pass
return False
def is_ascii_range(self, n):
return 0 <= n < 256
def is_custom_ascii_range(self, n):
return 33 <= n <48 or 58 <= n < 126
class Usage(object):
'''''
命令行参数读取与解析
'''
def __init__(self):
self._clsWork = Code()
self._args_dic = {'arg_help' : ['-"Usage: pwd [-options] [args...] where option include:"
strHelp += """
-"""
print strHelp
def args(self, argv_ls):
'''''dispatch command'''
# print argv_ls
if len(argv_ls) <= 1 or len(argv_ls) > 5:
print 'Unrecognized option'
return
cmd_dic = {}
curr_cmd = ''
# control command
for i, v in enumerate(argv_ls[1:]):
for j in self._args_dic.items():
# add command
if v in j[1] and j[0] not in cmd_dic:
curr_cmd = j[0]
cmd_dic[curr_cmd] = []
break
else:
# add argv
if cmd_dic:
cmd_dic[curr_cmd].append(v)
# exec command
if cmd_dic:
self.exec_cmd(cmd_dic)
else:
print 'Unrecognized option'
def exec_cmd(self, cmd_dic):
'''''exec cmd'''
if len(cmd_dic) == 2:
if 'arg_p' in cmd_dic and 'arg_k' in cmd_dic\
and len(cmd_dic['arg_p']) == 1 and len(cmd_dic['arg_k']) == 1:
self._clsWork.reset_key(cmd_dic['arg_k'][0])
print self._clsWork.encode(cmd_dic['arg_p'][0])
return
elif 'arg_t' in cmd_dic and 'arg_k' in cmd_dic\
and len(cmd_dic['arg_t']) == 1 and len(cmd_dic['arg_k']) == 1:
self._clsWork.reset_key(cmd_dic['arg_k'][0])
print self._clsWork.decode(cmd_dic['arg_t'][0])
return
self.help()
if __name__ == '__main__':
usage = Usage()
usage.args(sys.argv)
希望本文所述对大家的Python程序设计有所帮助。
Python,简单的,可逆,加密
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]