邮件群发 2024|如何实现信息加密及其在短信发送中的应用
更新时间:2022-09-10 5:07:24
分类:技巧 浏览:149
如何实现信息加密及其在短信发送中的应用
信息加密技术是一种利用物理或化学手段保护电子信息在传输和存储过程中避免泄露的技术。就信息结果而言,加密就是通过密码算法对数据进行变换邮件群发 2024,通过密钥(公钥)将明文转换为密文,使其成为没有正确密钥任何人都难以理解的消息。
众所周知的私钥加密算法有:RSA、算法等,其中影响最大的私钥加密算法是RSA,按类型分为公钥加密算法和私钥加密算法。与公钥加密算法加密算法相比,私钥加密实现了接收方和发送方的秘钥互不相同,密文不能通过其中的一个秘钥破译,越来越具有信息安全性。
在实际情况下,局域网对信息安全等级的要求并没有那么高,指出信息传输效率通常是通过公钥加密算法来实现的。下面我们将通过公钥加密算法的实现来讨论包的使用。
1.
继承和发展(2013年停止更新),可以实现包括SHA在内的多种加密方式1、MD5,详情请访问官网
*包安装
pip3
2.官网使用示例
*消息加密
通过随机函数生成秘钥邮件群发 2024,加密获取.nonce,标记,写入文件中三种信息,将秘钥写入文件。在实际应用中,可以写在不同的文件中,实现密钥分发。
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
file_out = open("encrypted.bin", "wb")
file_key = open("encrypted.key", "wb")
[ file_out.write(x) for x in (cipher.nonce, tag, ciphertext) ]
file_key.write(key)
file_out.close()
file_key.close()
```
from Crypto.Cipher import AES
file_in = open("encrypted.bin", "rb")
file_key = open("encrypted.key", "rb")
nonce, tag, ciphertext = [ file_in.read(x) for x in (16, 16, -1) ]
key = file_key.read()
#let's assume that the key is somehow available again
cipher = AES.new(key, AES.MODE_EAX, nonce)
data = cipher.decrypt_and_verify(ciphertext, tag)
首先我们定义二进制补码文件的写入和读取
###byte data
def read_file(one_file):
with open(one_file,'rb') as file_byte:
file_hex = file_byte.read()
return file_hex
def write_file(one_file, file_hex):
with open(one_file,'wb') as new_file:
new_file.write(file_hex)
class EncryptStr(object):
def __init__(self, key=None):
self.length = 16
self.key = key if key else get_random_bytes(self.length)
self.mode = AES.MODE_EAX
def encrypt(self, text):
#cryptor = AES.new(self.key, self.mode)
cipher = AES.new(self.key, self.mode)
ciphertext, tag = cipher.encrypt_and_digest(text.encode('utf-8'))
#[ print(x) for x in (cipher.nonce, tag, ciphertext)]
#self.ciphertext = cryptor.encrypt(text)
return cipher.nonce, tag, ciphertext
# 解密后,去掉补足的空格用strip() 去掉
def decrypt(self, nonce, text, tag):
cryptor = AES.new(self.key, self.mode, nonce)
plain_text = cryptor.decrypt_and_verify(text, tag)
return plain_text.decode('utf-8').strip('')
class AESEncrypter(object):
def __init__(self, key=None, iv=None):
self.length = 16
if isinstance(key, str) or not key:
self.key = key.encode('utf8') if key else get_random_bytes(self.length)
elif isinstance(key, bytes):

self.key = key
self.iv = iv if iv else self.key[0:self.length]
def _pad(self, text):
text_length = len(text)
padding_len = AES.block_size - int(text_length % AES.block_size)
if padding_len == 0:
padding_len = AES.block_size
t2 = chr(padding_len) * padding_len
t2 = t2.encode('utf8')
# print('text ', type(text), text)
# print('t2 ', type(t2), t2)
t3 = text + t2
return t3
def _unpad(self, text):
pad = ord(text[-1])
return text[:-pad]
def encrypt(self, raw):
raw = raw.encode('utf8')
raw = self._pad(raw)
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
encrypted = cipher.encrypt(raw)
return base64.b64encode(encrypted).decode('utf8')
def decrypt(self, enc):
print(self.key,type(self.key))
enc = enc.encode('utf8')
enc = base64.b64decode(enc)
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
decrypted = cipher.decrypt(enc)
return self._unpad(decrypted.decode('utf8'))
短信发送规范 编写短信,我们使用外部邮件和包裹发送短信,我们需要在短信文本中定义如下属性
最大发送次数
最大间隔时间
邮件发送者
发送者邮箱密码
邮箱服务器地址
接收者
抄送者
邮件主题
邮件正文
邮件附件
发送邮件的一般流程如下:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import utils
import mimetypes, sys,smtplib
#初始化服务器链接
smtp=smtplib.SMTP()
smtp.connect(server, port)
#服务器登录
smtp.login( addressor , password )
#构造邮寄正文
msginfo=MIMEMultipart()
msginfo['From'] = addressor
msginfo['To'] = receiver
msginfo['Message-ID'] = utils.make_msgid()
msginfo['Subject'] = subject
msginfo.attach(MIMEText( body , 'html' , 'utf-8' ))
# 邮件发送
smtp.sendmail( addressor , receiver.split(';') , msginfo.as_string())
加密技术在短信发送中的应用方法
这里我们以前面的形式2为例。首先,密文是可以直接读取的字符串,秘钥是字节,需要使用上面的函数读取。
with open('encrypted.bin','r') as f_bin:
Encrypted = f_bin.readline()
Key = read_file('encrypted.key')
data = AESEncrypter(Key).decrypt(Encrypted)
#这里得到的data就是邮箱密码了,填充至上文smtp.login( addressor , password )的password里,即:
password = data
版权声明:
本站内容部分来源网络,版权归作者所有,如有侵权,请联系我们删除!