leafsummer
9/14/2015 - 11:26 AM

import pyd/so files

import pyd/so files

#!/usr/bin/env python

import os
import sys
import shutil
import md5

def calc_file_md5(file) :
    m = md5.new()
    f = open(file,'rb')
    while True :
        d = f.read(8096)
        if not d :
            break
        m.update(d)
    return m.hexdigest()

class nCrypt() :
    def __init__(self) :
        self.__winlibs = []
        self.__winlibs.append('win_nspycrypt_25_x86.pyd')
        self.__winlibs.append('win_nspycrypt_27_x64.pyd')
        
        self.__nixlibs = []
        self.__nixlibs.append('nix_nspycrypt_25_x86.pyd')
        self.__nixlibs.append('nix_nspycrypt_27_x86.pyd')
        self.__nixlibs.append('nix_nspycrypt_27_x64.pyd')
        
        self.home = os.path.dirname(os.path.abspath(__file__))
        self.__libdir = self.home + os.sep + 'pyds'
        
        self.__winlibs = [self.__libdir + os.sep + x for x in self.__winlibs]
        self.__nixlibs = [self.__libdir + os.sep + x for x in self.__nixlibs]
        
        self.__md5list = {}
        for d in self.__winlibs :
            self.__md5list[d] = str(calc_file_md5(d)).lower()
        for d in self.__nixlibs :
            self.__md5list[d] = str(calc_file_md5(d)).lower()
        
        t_sys_path = sys.path
        sys.path = [self.home]
        
        if os.name == 'nt' :
            self.__touse = self.home + os.sep + 'nspycrypt.pyd'
            self.__libs = self.__winlibs
        else :
            self.__touse = self.home + os.sep + 'nspycrypt.so'
            self.__libs = self.__nixlibs
        
        if os.path.exists(self.__touse) :
            __md5 = str(calc_file_md5(self.__touse)).lower()
            if not __md5 in self.__md5list.values() :
                try :
                    os.remove(self.__touse)
                except BaseException , e :
                    try :
                        open(self.__touse , 'w').close()
                    except BaseException , e :
                        pass
        
        self.__handle = None
        
        try :
            self.__handle = __import__('nspycrypt')
            sys.path.extend(t_sys_path)
        except ImportError , e :
            _succ = False
            for _p in self.__libs :
                try :
                    shutil.copyfile(_p , self.__touse)
                    self.__handle = __import__('nspycrypt')
                    _succ = True
                    break
                except ImportError , e :
                    continue
                except BaseException , e :
                    continue
            sys.path.extend(t_sys_path)
            if not _succ :
                raise
        self.__key = ''
        self.__A_Z = range(ord('a'),ord('z')+1) + range(ord('A'),ord('Z')+1)
    
    def set_key(self,key) :
        self.__key = self.__handle.GetMD5Key(key)
    
    def encrypt(self,msg) :
        return self.__handle.Encrypt(self.__key , msg)
    
    def __is_crypted(self,msg) :
        if (len(msg)%16) : return False
        if str(msg).startswith('<') :
            for i in range(1,5) :
                if ord(msg[i]) not in self.__A_Z :
                    return True
            return False
        return True
    
    def decrypt(self,msg) :
        if not self.__is_crypted(msg) :
            return msg
        return self.__handle.Decrypt(self.__key , msg)