验证中...
Languages: Python
Categories: 加密和安全
Latest update 2019-08-14 18:12
security_swxlbjs
Raw Copy
import base64
import rsa
import os
from rsa import common
from rsa import transform
from Crypto.Cipher import _AES
class Security:
def Encryption(self, text):
"""text str"""
"""Data str"""
try:
text = text.encode('utf-8')
Data = base64.b32encode(base64.b64encode(text)).decode('utf-8')
except:
Data = None
finally:
return Data
def Decryption(self, text):
"""text str"""
"""Data str"""
try:
text = text.encode('utf-8')
Data = base64.b64decode(base64.b32decode(text)).decode('utf-8')
except:
Data = None
finally:
return Data
def SHost(self, Key):
"""Key str"""
"""Data str"""
try:
Key = [x for x in Key]
Length = len(Key)
while 32 > Length:
Length = Length + 1
Key.append("_")
Data = "".join(Key[0: 32])
except:
Data = None
finally:
return Data
class SRsa:
def __init__(self, pubOutside=None, priOutside=None):
if pubOutside is None:
self.pub = rsa.PublicKey.load_pkcs1(
open(os.path.join(os.path.join(os.path.dirname(__file__), "pem"), "public.pem"), "r").read().encode(
'utf-8'))
else:
self.pub = rsa.PublicKey.load_pkcs1(pubOutside)
if priOutside is None:
self.pri = rsa.PrivateKey.load_pkcs1(
open(os.path.join(os.path.join(os.path.dirname(__file__), "pem"), "private.pem"), "r").read().encode(
'utf-8'))
else:
self.pri = rsa.PrivateKey.load_pkcs1(priOutside)
def new(self, Length):
"""Length int"""
"""pub str"""
"""pri str"""
try:
(pubkey, prikey) = rsa.newkeys(Length)
pub = pubkey.save_pkcs1().decode('utf-8')
pri = prikey.save_pkcs1().decode('utf-8')
except:
pub = None
pri = None
finally:
return pub, pri
def Encryption(self, text):
"""text str"""
"""Data int"""
try:
result = b''
max_msg_length = common.byte_size(self.pub.n) - 11
count = len(text) // max_msg_length
if len(text) % max_msg_length > 0:
count = count + 1
for x in range(count):
start = max_msg_length * x
size = max_msg_length
content = text[start: start + size]
content = str(content).encode('utf-8')
result = result + rsa.encrypt(content, self.pub)
Data = transform.bytes2int(result)
except:
Data = None
finally:
return Data
def Decryption(self, text):
"""text int"""
"""Data str"""
try:
text = transform.int2bytes(int(text))
result = b''
key_length = common.byte_size(self.pri.n)
if len(text) % key_length != 0:
raise KeyboardInterrupt
count = len(text) // key_length
for x in range(count):
start = key_length * x
size = key_length
content = text[start: start + size]
result = result + rsa.decrypt(content, self.pri)
Data = result.decode('utf-8')
except:
Data = None
finally:
return Data
def PriEncryption(self, text):
"""text str"""
"""Data int"""
try:
result = b''
max_msg_length = common.byte_size(self.pri.n) - 11
count = len(text) // max_msg_length
if len(text) % max_msg_length > 0:
count = count + 1
for x in range(count):
start = max_msg_length * x
size = max_msg_length
content = text[start: start + size]
content = str(content).encode('utf-8')
result = result + rsa.encrypt(content, rsa.PublicKey(self.pri.n, self.pri.e))
Data = transform.bytes2int(result)
except:
Data = None
finally:
return Data
class SAes:
def __init__(self, key):
self.key = key
self.JmKey = _AES.new(self.key)
self.Complement = "_"
def Encryption(self, text):
"""text str"""
"""Data str"""
try:
text = base64.b32encode(text.encode('utf-8')).decode('utf-8')
Times = len(text)
while Times % 16 != 0:
Times = Times + 1
text = str("%s%s" % (text, self.Complement))
Data = self.JmKey.encrypt(text)
Data = base64.b64encode(Data)
Data = Data.decode('utf-8')
except:
Data = None
finally:
return Data
def Decryption(self, text):
"""text str"""
"""Data str"""
try:
text = base64.b64decode(text.encode('utf-8'))
Data = self.JmKey.decrypt(text)
Data = str(Data, encoding='utf-8').replace("_", "")
Data = base64.b32decode(Data.encode('utf-8')).decode('utf-8')
except:
Data = None
finally:
return Data

Comment list( 0 )

You need to Sign in for post a comment

Help Search