encode,md5,base64,aes,des,rsa实现
[Python]
encode & decode
from urllib import parse # url进行编码与解码 url = '你好啊' url_encode = parse.quote(url) print('url编码后:', url_encode) url_decode = parse.unquote(url_encode) print('url解码后:', url_decode) url_encode2 = parse.quote_plus(url) print('url2编码后:', url_encode2) url_decode2 = parse.unquote_plus(url_encode2) print('url2解码后:', url_decode2) request_params = { "id": 1, "name": "lanlang" } param_encode = parse.urlencode(request_params) print('param_encode:', param_encode) # id=1&name=lanlang param_decode = parse.unquote(param_encode) print('param_decode:', param_decode)
md5
import hashlib
msg = 'hello, MD5摘要算法'
m = hashlib.md5()
m.update(msg.encode('utf-8'))
md5_data = m.hexdigest() # 摘要
print('md5后的数据:', md5_data)
# acffb8e4d57563e9168fd0376fa9da4c
base64
import base64
# msg = 'hello, python'
msg = '你好哦'
# 编码
msg_byte = msg.encode()
print('文本字节:', msg_byte)
base64_encoded = base64.b64encode(msg_byte)
print('base64_encoded:', base64_encoded.decode())
# 解码
base64_decoded = base64.b64decode(base64_encoded)
print('base64_decoded:', base64_decoded, base64_decoded.decode())
aes
pip install pycryptodome
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
# key = get_random_bytes(16) # 生成随机的16字节密钥
key = b'1234567897891234'
plaintext = b'Hello, world!' # 加密数据
cipher = AES.new(key, AES.MODE_ECB) # 创建 AES 加密器对象
padded_plaintext = pad(plaintext, AES.block_size) # 填充明文数据
ciphertext = cipher.encrypt(padded_plaintext) # 加密
decrypted = cipher.decrypt(ciphertext) # 解密
decrypted_data = unpad(decrypted, AES.block_size) # 去除填充
print('原始数据:', plaintext)
print('加密后:', ciphertext.hex())
print('解密后:', decrypted_data)
exit(0)
data = b'Hello, world!'
# # 随机生成16字节(即128位)的加密密钥
# key = get_random_bytes(16)
# iv = get_random_bytes(16)
key = iv = b'1234567897891234'
# 使用CBC模式
cipher = AES.new(key, AES.MODE_CBC, iv)
# 对内容进行加密,pad函数用于分组和填充
encrypted_data = cipher.encrypt(pad(data, AES.block_size))
print('AES加密后数据:', encrypted_data, encrypted_data.hex())
# 需要重新new一个,否则会出现错误, 不能在encrypt之后,再进行decrypt
cipher_new = AES.new(key, AES.MODE_CBC, iv)
decrypted = cipher_new.decrypt(encrypted_data)
decrypted_data = unpad(decrypted, AES.block_size)
print('AES解密后数据:', decrypted_data.decode())
# 使用模块pycryptdome --> pip install pycryptodome
rsa:
生成秘钥对
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
key = RSA.generate(2048)
# 私钥
private_key = key.export_key()
with open('private_key.pem', 'wb') as f:
f.write(private_key)
# 公钥
public_key = key.public_key().export_key()
with open('public_key.pem', 'wb') as f:
f.write(public_key)
encrypt & decrypt
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
# 加密
data = b'Hello, world!'
with open('public_key.pem', 'rb') as f:
public_key = RSA.import_key(f.read())
cipher = PKCS1_OAEP.new(public_key)
encrypted_data = cipher.encrypt(data)
print('encrypted_data:', encrypted_data)
# 解密
with open('private_key.pem', 'rb') as f:
private_key = RSA.import_key(f.read())
cipher = PKCS1_OAEP.new(private_key)
decrypted_data = cipher.decrypt(encrypted_data)
print('decrypted_data:', decrypted_data.decode())
pycryptodome模块更多参考:
- https://www.jb51.net/python/3059815tp.htm
[Nodejs]
md5:
npm install crypto
const crypt = require('crypto')
let msg = 'hello, MD5摘要算法'
let md5 = crypt.createHash('md5')
md5_data = md5.update(msg).digest('hex')
console.log('Md5后的数据:', md5_data)
base64:
msg = '你好哦'
let buff = Buffer.from(msg, 'utf-8')
let base64_encoded = buff.toString('base64')
console.log('base64_encoded:', base64_encoded)
// 5L2g5aW95ZOm
let buff2 = Buffer.from(base64_encoded, 'base64')
let base64_decoded = buff2.toString('utf-8')
console.log('base64_decoded:', base64_decoded)
aes:
const crypt = require('crypto')
function aesEncrypt(data, key, iv) {
const cipher = crypt.createCipheriv('aes128', key, iv);
var crypted = cipher.update(data, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
function aesDecrypt(encrypted, key, iv) {
const decipher = crypt.createDecipheriv('aes128', key, iv);
var decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
key = '1234567897891234'
iv = '1234567897891234'
data = "Hello, world!"
encrypt_data = aesEncrypt(data, key, iv)
console.log('加密后数据:', encrypt_data)
encrypt_data = 'f4d236a9e916d66cf064686f4eb67c03'
decrypt_data = aesDecrypt(encrypt_data, key, iv)
console.log('解密后数据:', decrypt_data)
更多