【python字符串加密、解密】base64编码简介及相关用法

一、base64编码简介

1. 定义
base64:就是使用64个可打印字符来表示二进制数据的方法。
64个字符:
‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/’
64个字符按顺序分别对应了十进制数字的0到63,可以理解为上面的字符串的索引。

2. 转换流程
将字符串转换为base64编码流程:首先将文本字符串--------转换为二进制--------然后用64个字符表示二进制数。
字符串转换为二进制的方式:string.encode(编码方式)
其中string表示要转换的字符串,编码方式有:UTF-8、GBK(繁体中文、简体中文)、GB2312(简体中文编码)、UNICODE等。

二、将字符串转换为base64编码

转换代码如下:

import base64


string = "helloworld"
temp_b = string.encode("utf-8")  # 将字符串转换为二进制
content_b = base64.b64encode(temp_b)
print(content_b)
print('*'*50)
str_result = content_b.decode('utf-8')
print(str_result)

#运行结果
b'aGVsbG93b3JsZA=='
**************************************************
aGVsbG93b3JsZA==

注意:整数是不能编码的,整数只有进制转换,比如十进制转换为二进制。如下代码:

string = 12
print(string.encode("GB2312"))

# 运行结果
Traceback (most recent call last):
  File "C:\Users\Admin\Desktop\test.py", line 22, in <module>
    print(string.encode("GB2312"))
AttributeError: 'int' object has no attribute 'encode'

三、将编码还原为字符串

>>> import base64
>>> str_result = "aGVsbG93b3JsZA=="
>>> my_str = base64.b64decode(str_result).decode("utf-8")
>>> my_str
'helloworld'

四、编码方式不同对于Base64编码的区别

第一种情况:
待编码的文本字符串是中文,分别使用不同的编码将中文转换为二进制。

  • 使用"GB2312"编码中文转换为二进制后用64字符表示

    import base64
    
    
    string = "您好"
    temp_b = string.encode("GB2312")     # 将字符串编码为二进制
    content_b = base64.b64encode(temp_b)  # 调用方法并传入二进制数
    print(content_b)
    print('*'*50)
    str_result = content_b.decode("GB2312")
    print(str_result)
    

    运行结果

    b'xPq6ww=='
    **************************************************
    xPq6ww==
    
  • 使用UTF-8编码将中文转换为二进制用64字符表示

    import base64
    
    
    string = "您好"
    temp_b = string.encode("UTF-8")    # 将字符串编码为二进制
    content_b = base64.b64encode(temp_b)  # 调用方法并传入二进制数
    print(content_b)
    print('*'*50)
    str_result = content_b.decode("UTF-8")
    print(str_result)
    

    运行结果

    b'5oKo5aW9'
    **************************************************
    5oKo5aW9
    

第二种情况
待编码的文本字符串是是英文数字,使用不用编码将中文转换为二进制,后在进行base64编码结果是一样的。
代码如下:

import base64


string = "qwert"
temp_b = string.encode("GB2312")    # 将字符串编码为二进制
content_b = base64.b64encode(temp_b)  # 调用方法并传入二进制数
print(content_b)
print('*'*50)
str_result = content_b.decode("GB2312")
print(str_result)

## 运行结果
b'cXdlcnQ='
**************************************************
cXdlcnQ=
import base64


string = "qwert"
temp_b = string.encode("UTF-8")    # 将字符串编码为二进制
content_b = base64.b64encode(temp_b)  # 调用方法并传入二进制数
print(content_b)
print('*'*50)
str_result = content_b.decode("UTF-8")
print(str_result)

## 运行结果
b'cXdlcnQ='
**************************************************
cXdlcnQ=

以上参考:https://www.cnblogs.com/luguo3000/p/3940197.html
有不足之处,敬请指正。

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python提供了多种加密解密方式,以下是其中几种常用的方法: 1. ROT13加密解密 ROT13是一种简单的加密算法,将字母表中的每个字母替换为它后面的第13个字母。例如,字母A将被替换为字母N,字母B将被替换为字母O,以此类推。ROT13加密解密使用相同的算法。 加密代码: ```python def rot13(message): result = '' for letter in message: if letter.isalpha(): if letter.isupper(): result += chr((ord(letter) - 65 + 13) % 26 + 65) else: result += chr((ord(letter) - 97 + 13) % 26 + 97) else: result += letter return result ``` 解密代码: ```python def rot13(message): result = '' for letter in message: if letter.isalpha(): if letter.isupper(): result += chr((ord(letter) - 65 - 13) % 26 + 65) else: result += chr((ord(letter) - 97 - 13) % 26 + 97) else: result += letter return result ``` 2. Base64加密解密 Base64是一种编码方式,用于将二进制数据换为ASCII字符,使其能够在传输过程中安全地传输。Base64编码使用64个字符来表示二进制数据,每个字符对应6个二进制位,因此它可以将3个字节的数据编码为4个字符的字符串加密代码: ```python import base64 def base64_encode(message): message_bytes = message.encode('ascii') base64_bytes = base64.b64encode(message_bytes) base64_message = base64_bytes.decode('ascii') return base64_message ``` 解密代码: ```python import base64 def base64_decode(message): base64_bytes = message.encode('ascii') message_bytes = base64.b64decode(base64_bytes) message = message_bytes.decode('ascii') return message ``` 3. AES加密解密 AES是一种对称加密算法,它使用相同的密钥对数据进行加密解密。AES支持不同的密钥长度,包括128位、192位和256位。以下是使用AES加密解密字符串的示例代码: 加密代码: ```python from Crypto.Cipher import AES import base64 def aes_encrypt(message, key): key = key.encode('utf-8') message = message.encode('utf-8') cipher = AES.new(key, AES.MODE_EAX) ciphertext, tag = cipher.encrypt_and_digest(message) nonce = cipher.nonce ciphertext = base64.b64encode(ciphertext).decode('utf-8') nonce = base64.b64encode(nonce).decode('utf-8') tag = base64.b64encode(tag).decode('utf-8') return ciphertext, nonce, tag ``` 解密代码: ```python from Crypto.Cipher import AES import base64 def aes_decrypt(ciphertext, nonce, tag, key): key = key.encode('utf-8') ciphertext = base64.b64decode(ciphertext.encode('utf-8')) nonce = base64.b64decode(nonce.encode('utf-8')) tag = base64.b64decode(tag.encode('utf-8')) cipher = AES.new(key, AES.MODE_EAX, nonce=nonce) message = cipher.decrypt_and_verify(ciphertext, tag) message = message.decode('utf-8') return message ``` 以上是几种常用的Python字符串加密解密方法,根据实际需要选择合适的加密方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值