This article describes how to implement a simple reversible encryption program in Python. The example analyzes the principle and related skills of implementing reversible encryption in Python, which is of great practical value, for more information about how to implement a simple reversible encryption program in Python, see the example in this article. Share it with you for your reference. The details are as follows:
The Python code is as follows:
The 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 = pmod (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 -P
-K -T """
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.