Python implements simple reversible encryption program instances and python instances
This example describes how to implement a simple reversible encryption program in Python. Share it with you for your reference. The details are as follows:
The Python code is as follows:
Copy codeThe Code is as follows: # coding = UTF-8
'''''
Description: reversible encryption and decryption
Environment: python2.5.x
Author: idehong@gmail.com
'''
Import OS
Import sys
Class Code (object ):
''' Reversible encryption and decryption '''
Def _ init _ (self, key = "idehong@gmail.com "):
Self. _ src_key = key
Self. _ key = self. _ get_strascii (self. _ src_key, True)
Def encode (self, value ):
''''' Encryption function, encrypted as a string of numbers '''
Return "% d" % (self. _ get_strascii (value, True) ^ self. _ key)
Def decode (self, pwd ):
'''''''Decryption function '''
If self. is_number (pwd ):
Return self. _ get_strascii (int (pwd) ^ self. _ key, False)
Else:
Print 'require number .'
Def reset_key (self, key ):
''''' Reset key '''
Self. _ src_key = key
Self. _ key = self. _ get_strascii (self. _ src_key, True)
#===================================================== ==========================================================
# Internal call interface
#===================================================== ==========================================================
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 ):
'''' ''Gets the acⅱ code value of a single character '''
Try:
Return "% 03.d" % ord (char)
Handle T (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)
#===================================================== ========================================================== =
# Tool interface
#===================================================== ========================================================== =
Def is_number (self, value ):
Try:
Int (value)
Return True
Handle T (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 ):
'''''
Read and parse command line parameters
'''
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 'uncognized option'
Return
Performance_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 1__dic:
Curr_cmd = j [0]
Performance_dic [curr_cmd] = []
Break
Else:
# Add argv
If 1__dic:
Performance_dic [curr_cmd]. append (v)
# Exec command
If 1__dic:
Self.exe c_cmd (pai_dic)
Else:
Print 'uncognized option'
Def exec_cmd (self, performance_dic ):
'''''Exec cmd '''
If len (1__dic) = 2:
If 'arg _ P' in each _dic and 'arg _ k' in each _dic \
And len (into _dic ['arg _ P']) = 1 and len (into _dic ['arg _ k']) = 1:
Self. _ clsWork. reset_key (pai_dic ['arg _ k'] [0])
Print self. _ clsWork. encode (pai_dic ['arg _ P'] [0])
Return
Elif 'arg _ t' in each _dic and 'arg _ k' in each _dic \
And len (into _dic ['arg _ T']) = 1 and len (into _dic ['arg _ k']) = 1:
Self. _ clsWork. reset_key (pai_dic ['arg _ k'] [0])
Print self. _ clsWork. decode (pai_dic ['arg _ T'] [0])
Return
Self. help ()
If _ name _ = '_ main __':
Usage = Usage ()
Usage. args (sys. argv)
I hope this article will help you with Python programming.