背景介绍:本地安装的数据库密码忘记了,但是之前有通过navicate 连接过数据库,所以就想通过navicate里记录连接信息找回数据库密码。
在网上收到了一些通过navicate信息找回密码的帖子,但是都是通过运行PHP 文件实现的。我在实际操作时发现通过他们的步骤在网上运行PHP文件根本得不到结果。
通过另一个在线PHP工具运行之后,直接报错
后面我就想既然代码是有的,那就把代码转成python 语言运行试试。最后成功将navicate里的密文转成密码。下面整理所有步骤如下:
1.获取navivate里的记录的连接信息
a.点击文件-》导出连接
b.导出需要的数据库连接信息
c.用记事本打开导出的文件,找到密码加密之后的信息
2.使用python 执行代码,获取数据库连接密码
执行前需要安装pycryptodome库
```python
pip install pycryptodome
然后运行下面的代码
```python
from Crypto.Cipher import AES, Blowfish
from Crypto.Util.Padding import pad, unpad
from Crypto.Hash import SHA1
import binascii
class NavicatPassword:
def __init__(self, version=12):
self.version = version
self.aes_key = b'libcckeylibcckey'
self.aes_iv = b'libcciv libcciv '
self.blow_string = '3DC5CA39'
self.blow_key = SHA1.new(b'3DC5CA39').digest()
self.blow_iv = binascii.unhexlify('d9c7c3c8870d64bd')
def encrypt(self, string):
result = None
if self.version == 11:
result = self.encrypt_eleven(string)
elif self.version == 12:
result = self.encrypt_twelve(string)
return result
def encrypt_eleven(self, string):
round_len = len(string) // 8
left_length = len(string) % 8
result = b''
current_vector = self.blow_iv
for i in range(round_len):
temp = self.encrypt_block(self.xor_bytes(string[8 * i: 8 * (i + 1)], current_vector))
current_vector = self.xor_bytes(current_vector, temp)
result += temp
if left_length:
current_vector = self.encrypt_block(current_vector)
result += self.xor_bytes(string[8 * round_len:], current_vector)
return binascii.hexlify(result).upper().decode()
def encrypt_block(self, block):
cipher = Blowfish.new(self.blow_key, Blowfish.MODE_ECB)
return cipher.encrypt(block)
def decrypt_block(self, block):
cipher = Blowfish.new(self.blow_key, Blowfish.MODE_ECB)
return cipher.decrypt(block)
def xor_bytes(self, str1, str2):
return bytes(a ^ b for a, b in zip(str1, str2))
def encrypt_twelve(self, string):
cipher = AES.new(self.aes_key, AES.MODE_CBC, iv=self.aes_iv)
encrypted = cipher.encrypt(pad(string.encode(), AES.block_size))
return binascii.hexlify(encrypted).upper().decode()
def decrypt(self, string):
result = None
if self.version == 11:
result = self.decrypt_eleven(string)
elif self.version == 12:
result = self.decrypt_twelve(string)
return result
def decrypt_eleven(self, upper_string):
string = binascii.unhexlify(upper_string.lower())
round_len = len(string) // 8
left_length = len(string) % 8
result = b''
current_vector = self.blow_iv
for i in range(round_len):
encrypted_block = string[8 * i: 8 * (i + 1)]
temp = self.xor_bytes(self.decrypt_block(encrypted_block), current_vector)
current_vector = self.xor_bytes(current_vector, encrypted_block)
result += temp
if left_length:
current_vector = self.encrypt_block(current_vector)
result += self.xor_bytes(string[8 * round_len:], current_vector)
return result.decode()
def decrypt_twelve(self, upper_string):
string = binascii.unhexlify(upper_string.lower())
cipher = AES.new(self.aes_key, AES.MODE_CBC, iv=self.aes_iv)
decrypted = unpad(cipher.decrypt(string), AES.block_size)
return decrypted.decode()
# 需要指定版本两种,11或12
# navicat_password = NavicatPassword(11)
# 这里我指定的12的版本,原先指定的11,执行之后的密码是乱码
navicat_password = NavicatPassword(12)
# 解密
decode = navicat_password.decrypt('你的密码信息')
print(decode)
执行完之后就能得到数据库的密码啦