Home Backend Development Python Tutorial Python实现简单的可逆加密程序实例

Python实现简单的可逆加密程序实例

Jun 06, 2016 am 11:21 AM
python encryption

本文实例讲述了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             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     def is_custom_ascii_range(self, n):  
        return 33 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 -p  
        -k -t """
        print strHelp    
    def args(self, argv_ls):   
        '''''dispatch command'''   
#        print argv_ls  
        if 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程序设计有所帮助。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

What software is NET40? What software is NET40? May 10, 2024 am 01:12 AM

What software is NET40?

How to search deepseek How to search deepseek Feb 19, 2025 pm 05:18 PM

How to search deepseek

What language is the browser plug-in written in? What language is the browser plug-in written in? May 08, 2024 pm 09:36 PM

What language is the browser plug-in written in?

How to program deepseek How to program deepseek Feb 19, 2025 pm 05:36 PM

How to program deepseek

What are the common methods for program performance optimization? What are the common methods for program performance optimization? May 09, 2024 am 09:57 AM

What are the common methods for program performance optimization?

See all articles