重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
#coding=utf-8
10年积累的做网站、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有益阳免费网站建设让你可以放心的选择与我们合作。
'''''
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' : ['-?', '-help'],
'arg_p' : ['-p', '-pwd'],
'arg_t' : ['-t', '-text'],
'arg_k' : ['-k', '-key'],
}
def help(self, *k):
strHelp = "Usage: pwd [-options] [args...] where option include:"
strHelp += """
-? -help print this help message
-k key_str -p pwd_str
-k key_str -t text_str"""
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)
##注意:最左边每个=表示一个空格
def encrypt(t):
====s=""
====for e in t:
========if e.islower():
============s+=chr(97+(ord(e)-97+7)%26)
========elif e.isupper():
============s+=chr(65+(ord(e)-65+7)%26)
========else:
============s+=e
====return s
t=input()
print(t,encrypt(t))
import string
def kaisa(s, k): #定义函数 接受一个字符串s 和 一个偏移量k
lower = string.ascii_lowercase #小写字母
upper = string.ascii_uppercase #大写字母
before = string.ascii_letters #无偏移的字母顺序 小写+大写
after = lower[k:] + lower[:k] + upper[k:] + upper[:k] #偏移后的字母顺序 还是小写+大写
#分别把小写字母和大写字母偏移后再加到一起
table = ''.maketrans(before, after) #创建映射表
return s.translate(table) #对s进行偏移 即加密
s = input('请输入一个字符串:')
k = int(input('请输入一个整数密钥:'))
print(kaisa(s, k))
调用此函数