Commit 410d7db9 authored by shenshuo's avatar shenshuo

新增加密模块

parent 05e0664f
......@@ -11,6 +11,7 @@ import sys
import time
import re
import subprocess
import base64
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
......@@ -97,6 +98,56 @@ class MyCrypt:
plain_text = cryptor.decrypt(a2b_hex(text)).decode('utf-8')
return plain_text.rstrip('\0')
class MyCryptV2:
def __init__(self, key='HOrUmuJ4bCVG6EYu2docoRNNYSdDpJJw'):
"""
Usage:
#实例化
mc = MyCrypt()
#加密方法
mc.my_encrypt('password')
#解密方法
mc.my_decrypt('ZpZjEcsqnySTz6UsXD/+TA==')
:param key:
"""
self.key = key
# str不是16的倍数那就补足为16的倍数
def add_to_16(self, value):
while len(value) % 16 != 0:
value += '\0'
return str.encode(value) # 返回bytes
def my_encrypt(self, text):
"""
加密方法
:param text: 密码
:return:
"""
aes = AES.new(self.add_to_16(self.key), AES.MODE_ECB)
# 先进行aes加密
encrypt_aes = aes.encrypt(self.add_to_16(text))
# 用base64转成字符串形式
encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8').replace('\n', '')# 执行加密并转码返回bytes
# print('[INFO]: 你的加密为:{}'.format(encrypted_text))
return encrypted_text
def my_decrypt(self, text):
"""
解密方法
:param text: 加密后的密文
:return:
"""
# 初始化加密器
aes = AES.new(self.add_to_16(self.key), AES.MODE_ECB)
# 优先逆向解密base64成bytes
base64_decrypted = base64.decodebytes(text.encode(encoding='utf-8'))
# 执行解密密并转码返回str
decrypted_text = str(aes.decrypt(base64_decrypted), encoding='utf-8').replace('\0', '')
# print('[INFO]: 你的解密为:{}'.format(decrypted_text))
return decrypted_text
### 当前时间
def now_time():
return time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
......
......@@ -9,14 +9,14 @@ from distutils.core import setup
setup(
name='opssdk',
version='0.0.12',
version='0.0.13',
packages=['opssdk', 'opssdk.logs', 'opssdk.operate', 'opssdk.install', 'opssdk.get_info', 'opssdk.utils', 'websdk'],
url='https://github.com/ss1917/ops_sdk/',
license='',
install_requires=['fire', 'shortuuid', 'pymysql===0.9.3', 'sqlalchemy===1.3.0', 'python3-pika===0.9.14', 'PyJWT',
'Crypto===1.4.1', 'requests', 'redis===2.10.6', 'tornado===5.0',
'aliyun-python-sdk-core-v3===2.8.6', 'aliyun-python-sdk-dysmsapi','python-dateutil===2.7.5',
'ldap3===2.6' ],
'aliyun-python-sdk-core-v3===2.8.6', 'aliyun-python-sdk-dysmsapi', 'python-dateutil===2.7.5',
'ldap3===2.6', 'pycryptodome'],
author='shenshuo',
author_email='191715030@qq.com',
description='SDK of the operation and maintenance script'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment