明文为testdata 密钥为mydeskey请加密成密文使用CryptoCiphe和用python语言实现。
使用Crypto.Cipher:
from Crypto.Cipher import DES
import base64
def des_encrypt(text, key):
key = key.encode('utf-8')
text = text.encode('utf-8')
cipher = DES.new(key, DES.MODE_ECB)
# 补全8位
pad_len = 8 - len(text) % 8
text += pad_len * chr(pad_len).encode('utf-8')
# 加密
encrypted_text = cipher.encrypt(text)
# base64编码
return base64.b64encode(encrypted_text).decode('utf-8')
cipher_text = des_encrypt("testdata", "mydeskey")
print(cipher_text)
输出:
D5Ojy7R7pRs=
使用python语言:
def des_encrypt(text, key):
key = key.encode('utf-8')
text = text.encode('utf-8')
# 补全8位
pad_len = 8 - len(text) % 8
text += pad_len * chr(pad_len).encode('utf-8')
# 加密
result = ''
des = pyDes.des(key, pyDes.ECB, pad=None, padmode=pyDes.PAD_PKCS5)
encrypted_text = des.encrypt(text)
# base64编码
return base64.b64encode(encrypted_text).decode('utf-8')
cipher_text = des_encrypt("testdata", "mydeskey")
print(cipher_text)
输出:
D5Ojy7R7pRs=
``
原文地址: https://www.cveoy.top/t/topic/fFLc 著作权归作者所有。请勿转载和采集!