Python 3 标准库 base64 加密与解密算法

#base64 base32 base16 加密与解密
import base64

#加密解密函数
def encrypt_decrypt(string):
	#将字符串转换成bytes 编码格式utf-8
	bytesString = string.encode(encoding='utf-8')
	print(('bytes编码后的字符串: %s') % bytesString)

	#base64编码
	encodeStr = base64.b64encode(bytesString)
	print('base64编码后的bytes类型encodeStr:', encodeStr)
	print('base64编码解码字符串:', encodeStr.decode())

	#base64解码
	decodeStr = base64.b64decode(encodeStr)
	print('base64解码后的字符串:',decodeStr.decode())

	#base32加密
	encodeStr32 = base64.b32encode(bytesString)
	print('base32加密后的bytes类型:', encodeStr32)
	print('base32加密后的字符串:', encodeStr32.decode())

	#base32解码
	decodeStr32 = base64.b32decode(encodeStr32)
	print('base32解码后的字符串:', decodeStr32.decode())

	#base16加密
	encodeStr16 = base64.b16encode(bytesString)
	print('base16加密后的字符串:', encodeStr16.decode())

	#base16解码
	decodeStr16 = base64.b16decode(encodeStr16)
	print('base16解码后的字符串:', decodeStr16.decode())

if __name__ == '__main__':
    #测试数据
	encrypt_decrypt('python3 linux java php7')

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
3DES (Triple Data Encryption Standard) 是一种对称加密算法,它使用三个不同的密钥进行加密。下面是一个使用 Python 实现 3DES 加密解密算法的示例代码: ```python from Crypto.Cipher import DES3 import base64 def encrypt_3des(key, data): # 将密钥转换为字节类型 key = key.encode('utf-8') # 创建 3DES 加密器 cipher = DES3.new(key, DES3.MODE_ECB) # 将数据转换为字节类型 data = data.encode('utf-8') # 使用 3DES 加密加密数据 encrypted_data = cipher.encrypt(data) # 将加密后的数据进行 base64 编码后返回 return base64.b64encode(encrypted_data).decode('utf-8') def decrypt_3des(key, encrypted_data): # 将密钥转换为字节类型 key = key.encode('utf-8') # 创建 3DES 解密器 cipher = DES3.new(key, DES3.MODE_ECB) # 将加密后的数据进行 base64 解码 encrypted_data = base64.b64decode(encrypted_data) # 使用 3DES 解密解密数据 decrypted_data = cipher.decrypt(encrypted_data) # 将解密后的数据转换为字符串类型并返回 return decrypted_data.decode('utf-8') ``` 使用示例: ```python key = '12345678901234567890123456789012' data = 'Hello, World!' encrypted_data = encrypt_3des(key, data) print('加密后的数据:', encrypted_data) decrypted_data = decrypt_3des(key, encrypted_data) print('解密后的数据:', decrypted_data) ``` 输出结果: ``` 加密后的数据: uF4LwX8JqfJbdSFV8iWj8Q== 解密后的数据: Hello, World! ``` 注意:以上代码使用了 PyCryptodome 库来实现 3DES 加密解密,需要先安装该库。可以使用以下命令来安装: ``` pip install pycryptodome ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值