2012年5月22日 星期二

AzDG可逆加密演算法 for Python 3.2

遷移到Python 3.2的環境時 AzDG 就動不了,所以得再產出一個3.2版的。

AzDG = AzDG()
 m = AzDG.encode('中文測試')
AzDG.decode(m)
#! /usr/bin/python
# -*- coding: utf-8 -*-
# python 3.2

import hashlib
import base64
import time
import random



class AzDG:
    """docstring for AzDG"""

    __all__ = ["encode", "decode"]

    cipher = '0123456789'
    charset = 'utf-8'
    
    def __init__(self, cipher = None):
        if cipher != None:
            self.cipher = cipher

    def cipherEncode(self, sourceText):    
        cipherHash = hashlib.md5(self.cipher.encode(self.charset)).hexdigest().encode(self.charset)        
        encodeText = bytearray()
        for i in range(len(sourceText)):            
            encodeText.append(sourceText[i] ^ cipherHash[i%32])    
        return encodeText.decode('ISO-8859-1')
                
    def encode(self, sourceText, charset = 'utf-8'):
        if charset != self.charset:
            self.charset = charset
        sourceText = sourceText.encode(self.charset)        
        noise = hashlib.md5(str(time.time()).encode(self.charset)).hexdigest().encode(self.charset)        
        encodeText = bytearray()
        for i in range(len(sourceText)):            
            encodeText.append(noise[i%32])    
            encodeText.append(sourceText[i] ^ noise[i%32])        
        return base64.b64encode(self.cipherEncode(encodeText).encode('ISO-8859-1')).decode(self.charset)
    
    def decode(self, sourceText, charset = 'utf-8'):
        if charset != self.charset:
            self.charset = charset
        decodeSourceText = self.cipherEncode(base64.b64decode(sourceText.encode(self.charset)))        
        textLength = len(decodeSourceText)
        decodeText = bytearray()
        i = 0            
        while i < textLength:    
            decodeText.append(ord(decodeSourceText[i]) ^ ord(decodeSourceText[i+1]))    
            i += 2
        return decodeText.decode(self.charset) 

沒有留言: