本文实例讲述了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,简单的,可逆,加密
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]