def decrypt(self, root_path, res_path):
     """
     RSA的文件解密
     :param root_path: 加密后的文件
     :param res_path: 解密后的文件
     :return: 解密结果
     """
     new_line = b""
     cipher = PKCS1_OAEP.new(self.private_key, hashAlgo=SHA256)
     try:
         with open(root_path, "r") as rootf:
             lines = rootf.read().split("==")
         for line in lines:
             if len(line) > 0:
                 line = line + "=="
                 b64_decoded_message = base64.b64decode(line)
                 cipherContent = cipher.decrypt(b64_decoded_message)
                 new_line = new_line + cipherContent
         if not os.path.exists(os.path.dirname(res_path)):
             os.makedirs(os.path.dirname(res_path))
         with open(res_path, "w") as resf:
             resf.write(
                 str(new_line, encoding="UTF-8").replace("\r\n", "\n"))
         return True
     except Exception as e:
         traceback.print_exc()
         loggerUtil.error(traceback.print_exc())
     return False
    def encrypt(self, root_path, res_path):
        try:
            # 数据源
            data = open(root_path).read().encode("utf-8")
            # 输出源
            file_out = open(res_path, "wb")
            session_key = get_random_bytes(16)
            # Encrypt the session key with the public RSA key
            cipher_rsa = PKCS1_OAEP.new(self.rsa_public_key)
            enc_session_key = cipher_rsa.encrypt(session_key)
            # Encrypt the data with the AES session key
            cipher_aes = AES.new(session_key, AES.MODE_EAX)
            ciphertext, tag = cipher_aes.encrypt_and_digest(data)
            [
                file_out.write(x)
                for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext)
            ]
            return True
        except Exception as e:
            traceback.print_exc()
            loggerUtil.error(traceback.print_exc())
        return False


# if __name__ == '__main__':
#     FileDecryptEncrypt().decrypt("F:\\868663032830438_migu$user$login$_1554947856915.log","C:\\Users\\Carol\\Desktop\\others\\868663032830438_migu$user$login$_1554947856915.log")
 def __init__(self):
     try:
         self.private_key = RSA.import_key(
             open(
                 os.path.join(os.getcwd(), "app", "resource",
                              "rsa_private_key.pem")).read())
         self.rsa_public_key = RSA.import_key(
             open(
                 os.path.join(os.getcwd(), "app", "resource",
                              "rsa_public_key.pem")).read())
     except Exception as e:
         traceback.print_exc()
         loggerUtil.error("加载密钥出现异常")
 def decryptAES(self, root_path, res_path):
     try:
         file_in = open(root_path, "rb")
         enc_session_key, nonce, tag, ciphertext = \
             [file_in.read(x) for x in (self.private_key.size_in_bytes(), 16, 16, -1)]
         # Decrypt the session key with the private RSA key
         cipher_rsa = PKCS1_OAEP.new(self.private_key, hashAlgo=SHA256)
         session_key = cipher_rsa.decrypt(enc_session_key)
         # Decrypt the data with the AES session key
         cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
         data = cipher_aes.decrypt_and_verify(ciphertext, tag)
         # 返回解密后的数据
         with open(res_path, "w+") as resf:
             print(data.decode("utf-8"))
             resf.write(data.decode("utf-8"))
         return True
     except Exception as e:
         traceback.print_exc()
         loggerUtil.error(traceback.print_exc())
     return False
 def decrypt(self, root_path):
     """
     RSA的文件解密
     :param root_path: 加密后的文件
     :return: 解密后的文件内容
     """
     new_line = b""
     cipher = PKCS1_OAEP.new(self.private_key, hashAlgo=SHA256)
     try:
         with open(root_path, "r") as rootf:
             lines = rootf.read().split("==")
         for line in lines:
             if len(line) > 0:
                 line = line + "=="
                 b64_decoded_message = base64.b64decode(line)
                 cipherContent = cipher.decrypt(b64_decoded_message)
                 new_line = new_line + cipherContent
         return new_line
     except Exception as e:
         traceback.print_exc()
         loggerUtil.error(traceback.print_exc())
     return None
Beispiel #6
0
import HTMLParser  #处理html编码字符
import re
import json
from utils import LoggerUtil
from utils.SqlUtil import MongoDB
import pymongo
import urlparse
from bs4 import BeautifulSoup  #lxml解析器
from cfg import subjects, xds, PATH, URL, COLL
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

html_parser = HTMLParser.HTMLParser()
logger = LoggerUtil.getLogger(__name__)
logger_major = LoggerUtil.getLogger('major')


class PaperParse:
    '''分析组卷网的试卷页面'''
    def __init__(self, url=URL.rootUrl):
        self.session = requests.Session()
        self.session.get(url)

    def parseParperPropAll(self, url=URL.paper_url):
        '''分析试卷的所有公共属性'''
        mongo = MongoDB()
        #创建唯一索引
        # for key, value in COLL.type.items():
        #     coll = mongo.getCollection(value)
Beispiel #7
0
#!/usr/bin/python
#-*-coding:utf-8-*-

import requests
import os
import urlparse
import json
from utils.SqlUtil import PostgreSql
from utils import LoggerUtil, Utils
import re
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
logger = LoggerUtil.getLogger(__name__)

SELECT_SQL = 'SELECT qid,answer FROM T_QUES_ZUJUAN_EX WHERE cate=1 AND subject= %s AND qid > %s ORDER BY seq ASC LIMIT %s '
UPDATE_SQL = 'UPDATE T_QUES_ZUJUAN_EX SET  choice_answer = %s WHERE qid = %s'
UPDATE_STATUS_SQL = 'UPDATE T_QUES_ZUJUAN_EX SET  status = %s WHERE qid = %s'
ROWS = 1000
rootImagPath = '/data/meiqiming/data/zj_image_new'

OPTIONS = {'A': [], 'B': [], 'C': [], 'D': [], 'E': []}


def init():
    for parent, dir_names, file_names in os.walk('data'):
        for file_name in file_names:
            for key, values in OPTIONS.items():
                if file_name.startswith(key):
                    values.append(
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import os
import datetime
from logging import Logger
from file_processing import FileProcessor
from model_operations import Modeller
from utils import FileUtils, LoggerUtil
from sklearn.metrics import confusion_matrix, classification_report

data_path = "data"
save_model_path = "saved_models"
session_id = str(datetime.datetime.now().timestamp())
utils = FileUtils()
logger = LoggerUtil(session_id, 'files')


def prepare_dataset(dir_path, is_train=True):
    processor = FileProcessor(dir_path, is_train)
    processor.process_all_files()
    return processor.get_dataset()


def prepare_model(dataset):
    modeller = Modeller(dataset)
    modeller.define_model()
    modeller.train_model()
    return modeller

Beispiel #9
0
def test_thread():
    worker = Thread(target=lambda: LoggerUtil.error_log(threading.currentThread().getName()), name='worker' + str(1))
    worker.start()