Python全栈•基础(一)⁃Python3 base64算法加密解密

import base64

copyright = 'Copyright (c) 2012 Doucube Inc. All rights reserved.'

def main():
    #转成bytes string
    bytesString = copyright.encode(encoding="utf-8")
    print(bytesString)

    #base64 编码
    encodestr = base64.b64encode(bytesString)
    print(encodestr)
    print(encodestr.decode())

    #解码
    decodestr = base64.b64decode(encodestr)
    print(decodestr.decode())


if __name__ == '__main__':
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值