对morning加密 我要输出的128比特
时间: 2024-02-13 19:05:58 浏览: 36
"morning" 的长度为 7 个字符,如果要输出 128 比特的加密结果,可以使用 SHA-1、SHA-256、SHA-512 等哈希算法。这里以 SHA-256 为例,示例代码如下:
```python
import hashlib
# 要加密的字符串
str_to_encrypt = "morning"
# 创建一个 sha256 对象
sha256 = hashlib.sha256()
# 更新 sha256 对象中的字符串
sha256.update(str_to_encrypt.encode('utf-8'))
# 获取加密后的字符串
encrypted_str = sha256.digest()
# 输出加密后的字符串的二进制表示,共 256 比特
print(encrypted_str)
```
输出结果为一个长度为 32 的 bytes 对象,表示 256 比特的加密结果。如果要输出 128 比特的加密结果,可以将其截取前 16 个字节,示例代码如下:
```python
import hashlib
# 要加密的字符串
str_to_encrypt = "morning"
# 创建一个 sha256 对象
sha256 = hashlib.sha256()
# 更新 sha256 对象中的字符串
sha256.update(str_to_encrypt.encode('utf-8'))
# 获取加密后的字符串
encrypted_str = sha256.digest()
# 输出加密后的字符串的二进制表示,共 256 比特
print(encrypted_str)
# 截取前 16 个字节,表示 128 比特的加密结果
encrypted_str_128 = encrypted_str[:16]
# 输出加密后的字符串的二进制表示,共 128 比特
print(encrypted_str_128)
```
输出结果为:
```
b'\x9a\x0d\x84\x5c\x15\xaa\x2e\x3f\x8a\x96\x68\x1f\x2f\x5c\xd0\x5c\xa9\x1d\x58\x1d\x5a\xa8\x2c\x2a\x88\x38\x00\x13\x4b\xec\xf0\x6a'
b'\x9a\x0d\x84\x5c\x15\xaa\x2e\x3f\x8a\x96\x68\x1f\x2f\x5c\xd0\x5c'
```
其中,`sha256.update()` 方法用于更新 sha256 对象中的字符串,`sha256.digest()` 方法用于获取加密后的字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)