51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
import hmac
|
|
import hashlib
|
|
|
|
from Cryptodome.Cipher import AES
|
|
|
|
SQLITE_FILE_HEADER = "SQLite format 3\x00" # SQLite文件头
|
|
|
|
KEY_SIZE = 32
|
|
DEFAULT_PAGESIZE = 4096
|
|
DEFAULT_ITER = 64000
|
|
|
|
|
|
# 通过密钥解密数据库
|
|
def decrypt(key, filePath, decryptedPath):
|
|
password = bytes.fromhex(key.replace(" ", ""))
|
|
with open(filePath, "rb") as file:
|
|
blist = file.read()
|
|
|
|
salt = blist[:16]
|
|
byteKey = hashlib.pbkdf2_hmac("sha1", password, salt, DEFAULT_ITER, KEY_SIZE)
|
|
first = blist[16:DEFAULT_PAGESIZE]
|
|
|
|
mac_salt = bytes([(salt[i] ^ 58) for i in range(16)])
|
|
mac_key = hashlib.pbkdf2_hmac("sha1", byteKey, mac_salt, 2, KEY_SIZE)
|
|
hash_mac = hmac.new(mac_key, first[:-32], hashlib.sha1)
|
|
hash_mac.update(b'\x01\x00\x00\x00')
|
|
|
|
if hash_mac.digest() == first[-32:-12]:
|
|
print("Decryption Success")
|
|
else:
|
|
print("Password Error")
|
|
return False
|
|
|
|
newblist = [blist[i:i + DEFAULT_PAGESIZE] for i in range(DEFAULT_PAGESIZE, len(blist), DEFAULT_PAGESIZE)]
|
|
|
|
with open(decryptedPath, "wb") as deFile:
|
|
deFile.write(SQLITE_FILE_HEADER.encode())
|
|
t = AES.new(byteKey, AES.MODE_CBC, first[-48:-32])
|
|
decrypted = t.decrypt(first[:-48])
|
|
deFile.write(decrypted)
|
|
deFile.write(first[-48:])
|
|
|
|
for i in newblist:
|
|
t = AES.new(byteKey, AES.MODE_CBC, i[-48:-32])
|
|
decrypted = t.decrypt(i[:-48])
|
|
deFile.write(decrypted)
|
|
deFile.write(i[-48:])
|
|
|
|
return True
|
|
|