python 获取电脑的C盘序列号+当前日期,将其加密;然后解密回原来的样子
时间: 2024-03-23 09:39:29 浏览: 21
好的,我可以为您提供解决方案。
首先,您需要安装pycryptodome库,它提供了AES加密和解密的功能。您可以使用以下命令进行安装:
```
pip install pycryptodome
```
接下来,您可以使用以下代码来获取C盘序列号和当前日期,并将其加密:
```python
import os
import hashlib
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from datetime import datetime
# 获取C盘序列号
serial_number = hashlib.md5(os.popen("wmic diskdrive get serialnumber").read().encode('utf-8')).hexdigest()
# 获取当前日期
current_date = datetime.now().strftime('%Y-%m-%d')
# 加密函数
def encrypt(key, plaintext):
cipher = AES.new(key, AES.MODE_CBC)
ciphertext = cipher.encrypt(pad(plaintext.encode('utf-8'), AES.block_size))
return cipher.iv + ciphertext
# 加密C盘序列号和当前日期
key = b'This is a key123'
encrypted_data = encrypt(key, serial_number + current_date)
# 打印加密后的数据
print("加密后的数据:", encrypted_data)
```
最后,您可以使用以下代码来解密数据并还原为原始的C盘序列号和日期:
```python
# 解密函数
def decrypt(key, ciphertext):
iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = unpad(cipher.decrypt(ciphertext[AES.block_size:]), AES.block_size)
return plaintext.decode('utf-8')
# 解密数据并还原为原始的C盘序列号和日期
decrypted_data = decrypt(key, encrypted_data)
original_serial_number = decrypted_data[:32]
original_date = decrypted_data[32:]
# 打印还原后的数据
print("还原后的C盘序列号:", original_serial_number)
print("还原后的日期:", original_date)
```
希望这可以帮助到您!