解密是按照制定的算法和关键数据,将一个密文数据进行逆向运算得到的正确的明文数据的过程
如:用户账号密码存储,此时任何人都不能查看该用户的明文密码
1.原始数据
2.编码(转换为字节数据)
3.使用算法加密
4.利用盐值更新密文
5.得到加密后的数据
编码:字符串.encode(编码名称)——>字节数据
解码:字节数据.decode(编码名称)——>字符串数据
==========================================
import rsa rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) #创建公钥 message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) #拼接明文js加密文件中得到 passwd = rsa.encrypt(message, key) #加密 passwd = binascii.b2a_hex(passwd) #将加密信息转换为16进制。 return passwdwindows下使用AES时安装pycryptodome 模块,pip install pycryptodome
linux 下使用AES时安装pycrypto模块,pip install pycrypto
```
from Crypto.Cipherimport AES
from binasciiimport b2a_hex, a2b_hex
from Cryptoimport Random
import base64
import json
class AesEncry(object):
# aes秘钥 ,可根据自身需要手动生成
key ="aes_keysaes_keysaes_keys"
def encrypt(self, data):
data = json.dumps(data)
mode = AES.MODE_ECB
padding =lambda s: s + (16 -len(s) %16) *chr(16 -len(s) %16)
cryptos = AES.new(self.key.encode("utf-8"), mode)
cipher_text = cryptos.encrypt(padding(data).encode("utf-8"))
return base64.b64encode(cipher_text).decode("utf-8")
def decrypt(self, data):
cryptos = AES.new(self.key.encode("utf-8"), AES.MODE_ECB)
decrpytBytes = base64.b64decode(data)
meg = cryptos.decrypt(decrpytBytes).decode('utf-8')
return meg[:-ord(meg[-1])]
aes_encry_util = AesEncry()
#明文
data ="mypwd_test"
#加密
encry_data = aes_encry_util.encrypt(data)
print(encry_data)
# 对密文进行解密
decry_data = aes_encry_util.decrypt(encry_data)
print(decry_data)
```
如上便完成了利用python进行AES的ECB加密