-
-
Save xct/cc72a9bd0775bfebb112538a662e2be2 to your computer and use it in GitHub Desktop.
Decrypts pgadmin4 encrypted strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import hashlib | |
from Crypto import Random | |
from Crypto.Cipher import AES | |
padding_string = b'}' | |
def pad(key): | |
"""Add padding to the key.""" | |
global padding_string | |
str_len = len(key) | |
# Key must be maximum 32 bytes long, so take first 32 bytes | |
if str_len > 32: | |
return key[:32] | |
# If key size id 16, 24 or 32 bytes then padding not require | |
if str_len == 16 or str_len == 24 or str_len == 32: | |
return key | |
# Convert bytes to string (python3) | |
if not hasattr(str, 'decode'): | |
padding_string = padding_string.decode() | |
# Add padding to make key 32 bytes long | |
return key + ((32 - str_len % 32) * padding_string) | |
def decrypt(ciphertext, key): | |
""" | |
Decrypt the AES encrypted string. | |
Parameters: | |
ciphertext -- Encrypted string with AES method. | |
key -- key to decrypt the encrypted string. | |
""" | |
global padding_string | |
ciphertext = base64.b64decode(ciphertext) | |
iv = ciphertext[:AES.block_size] | |
cipher = AES.new(pad(key), AES.MODE_CFB, iv) | |
decrypted = cipher.decrypt(ciphertext[AES.block_size:]) | |
return decrypted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment