AES加密功能的代码实现

 随着互联网的快速发展,不同应用程序之间的数据交互变得越来越普遍。但是数据在交互的过程中,总会有一些风险的存在,例如:数据的泄露,数据的拦截等,那么在数据传输过程中的加密就显得尤为重要。AES加密是常用的一种加密方式。

那么如何实现AES加密呢?这就成为了很多IT人员的需要解决的问题,今天我们就来一起探讨一下。   

import requests

from Crypto.Cipher import AES

import base64

import json

# AES加密函数

def encrypt(text, key, iv):

    cipher = AES.new(key, AES.MODE_CBC, iv)

    length = 16

    count = len(text)

    if count % length != 0:

        add = length - (count % length)

    else:

        add = 0

    text = text + ('\0' * add)

    ciphertext = cipher.encrypt(text)

    return base64.b64encode(ciphertext)

# Base64解密函数

def decrypt(ciphertext, key, iv):

    cipher = AES.new(key, AES.MODE_CBC, iv)

    plaintext = cipher.decrypt(base64.b64decode(ciphertext))

    return plaintext.rstrip(b'\0').decode()

# 注册功能

def register_account(url, account, password):

    # 加密密码

    key = 'xiufeng20230724'

    iv = 'xiufenghuanyingni'

    password_encrypted = encrypt(password, key, iv)

    # 构造请求数据

    data = {

        'account': account,

        'password': password_encrypted.decode()

    }

    # 转化成json

    json_data = json.dumps(data)

    # 发送POST请求

    response = requests.post(url, json_data.encode())

    # 解析响应数据

    if response.status_code == 200:

        result = response.json()

        ciphertext = result.get('data')

        plaintext = decrypt(ciphertext, key, iv)

        print("注册成功!返回数据:", plaintext)

    else:

        print("注册失败!HTTP错误码:", response.status_code)

# 测试账号注册

url = 'http://47.92.215.112:9797/api/account/register'

# 自定义的账号

account = 'your_account'

# 自定义的密码

password = 'your_password'

register_account(url, account, password)

以上是咱们根据python编写的一段依据AES加密和解密的代码,是不是看起来很神奇呢?经过加密后的数据,如果没有对应的秘钥是无法正确读取数据的。这样咱们的数据在传输过程中就有了很高的安全性。

最后:下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

猜你喜欢

转载自blog.csdn.net/wx17343624830/article/details/131896367