大江东去,浪淘尽,千古风流人物。故垒西边,人道是,三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千堆雪。江山如画,一时多少豪杰。遥想公瑾当年,小乔初嫁了,雄姿英发。羽扇纶巾,谈笑间,樯橹灰飞烟灭。故国神游,多情应笑我,早生华发。人生如梦,一尊还酹江月。

南鱼

果然,我没有抓住重点

导航

国产sm4加密 及smb mont挂载

看此代码前  请自行搭建smb服务器:then    look!!!!

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# date:2013-05-07 by ny
from gmssl.sm4 import CryptSM4, SM4_ENCRYPT, SM4_DECRYPT
from smb.SMBConnection import *
import logging
import os
import re


def encry_xml(file_path):
    try:
        key = b'xxxxxxxxxxxxxxxx'
        crypt_sm4 = CryptSM4()
        #file_path = r"xxx/xxxx/xx.xml"
        ecb_path_en = file_path
        ecb_path_en = ecb_path_en.rstrip(".xml") + "_test.xml"
        f = open(file_path, 'rb')
        value = f.read()
        f.close()
        crypt_sm4.set_key(key, SM4_ENCRYPT)
        encrypt_value = crypt_sm4.crypt_ecb(value)
        # os.remove(file_path)  # 删除原有的加密文件
        f = open(ecb_path_en, 'wb')  # 重新写入,路径和文件名与原来一致
        f.write(encrypt_value)
        f.close()
        return ecb_path_en
    except Exception, e:
        print e
        logging.info("加密失败")
        return False


def smb_send_report(path):
    '''
    smb 上传报表
    '''
    smb_address = 'xxxxxxx' # smb ip
    smb_user = 'xxxx'
    smb_password = 'xxxxxx'
    smb_my_name = "anyname"
    smb_remote_name = ""
    service_name = 'share'  # 可配置服务名
    # 初始化一个samba访问对象
    samba = SMBConnection(smb_user, smb_password, smb_my_name, smb_remote_name)
    # 创建一个samba连接
    smb_port = 139
    x = samba.connect(smb_address, smb_port)  # 返回True/False
    if not x:
        logging.info('连接失败')
        samba.close()
        return
    filename = os.path.join(path,'xxx.xml')
    if not filename:
        logging.info('没有找到待传文件')
        samba.close()
        return
    print filename
    # 过滤掉特殊字符
    filename = re.sub(r'[\/:*?"<>|]', '_', filename)
    # 调用加密函数,加密成功返回加密文件路径加名,失败返回False
    if encry_xml(filename):
        up_file = encry_xml(filename)
    else:
        logging.info('加密失败')
        return
    # 上传文件到服务器

    try:
        f = open(up_file, 'rb')
    except Exception, e:
        print e
        logging.info('打开文件失败')
        f.close()
        samba.close()
        return
    # smb_path = '/'  # 远程文件地址
    try:
        # 上传文件
        t = samba.storeFile(service_name, filename, f)
        print t  # 上传字节数
        f.close()
        samba.close()
        # print "up success"
    except Exception, e:
        print e
        # print 'up failure'
        logging.info('up failure')
        f.close()
        samba.close()
        return
    samba.close()




if __name__ == '__main__':
    # 通过下面的方式进行简单配置输出方式与日志级别
    logging.basicConfig(filename='logger.log', level=logging.INFO)
    # logging.debug('debug message')
    # logging.info('info message')
    # logging.warn('warn message')
    # logging.error('error message')
    # logging.critical('critical message')
    # 发送加密报表
    path= 'xxx/xxx/xxx'
    smb_send_report(path)
    

 附加sm2加解密 单纯回忆下

from gmssl.gmssl import sm2, func
private_key = '00B9AB0B828FF68872F21A837FC303668428DEA11DCD1B24429D0C99E24EED83D5'
public_key = 'B9C9A6E04E9C91F7BA880429273747D7EF5DDEB0BB2FF6317EB00BEF331A83081A6994B8993F3F5D6EADDDB81872266C87C018FB4162F5AF347B483E24620207'
sm2_crypt = sm2.CryptSM2(
public_key=public_key, private_key=private_key)




data ='111'
enc_data = sm2_crypt.encrypt(data)
dec_data =sm2_crypt.decrypt(enc_data)
assert dec_data == data

 

posted on 2019-05-22 16:28  南鱼羁荒渡  阅读(874)  评论(0编辑  收藏  举报