With the rapid development of the Internet, data interaction between different applications has become more and more common. However, in the process of data interaction, there will always be some risks, such as: data leakage, data interception, etc., so encryption during data transmission is particularly important. AES encryption is a commonly used encryption method.
So how to implement AES encryption? This has become a problem that many IT personnel need to solve. Let's discuss it together today.
import requests
from Crypto.Cipher import AES
import base64
import json
# AES encryption function
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 decryption function
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()
# Register function
def register_account(url, account, password):
# encrypted password
key = 'xiufeng20230724'
iv = 'xiufenghuanyingni'
password_encrypted = encrypt(password, key, iv)
# Construct request data
data = {
'account': account,
'password': password_encrypted.decode()
}
# convert to json
json_data = json.dumps(data)
# Send a POST request
response = requests.post(url, json_data.encode())
# parse response data
if response.status_code == 200:
result = response.json()
ciphertext = result.get('data')
plaintext = decrypt(ciphertext, key, iv)
print("Successful registration! Return data:", plaintext)
else:
print("Registration failed! HTTP error code:", response.status_code)
# Test account registration
url = 'http://47.92.215.112:9797/api/account/register'
# custom account
account = 'your_account'
# custom password
password = 'your_password'
register_account(url, account, password)
The above is a piece of code based on AES encryption and decryption that we wrote based on python. Does it look amazing? The encrypted data cannot be read correctly without the corresponding secret key. In this way, our data has a high level of security during transmission.
Finally: The complete software testing video tutorial below has been organized and uploaded, and friends who need it can get it by themselves [Guaranteed 100% free]
Software Testing Interview Documentation
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.