This article describes the Python implementation of a simple reversible encryption program. Share to everyone for your reference. Specifically as follows:
The Python code is as follows:
Copy Code code 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 digits '
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 Acsii code value of a single character '
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)
#================================================================================
# tool Interface
#================================================================================
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 <= n <48 or <= n < 126
Class Usage (object):
'''''
Command line parameter reading and parsing
'''
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)
I hope this article will help you with your Python programming.