Last active
June 11, 2024 14:01
-
-
Save RamadhanAmizudin/342c999455079faa48d63632ea7d9dd7 to your computer and use it in GitHub Desktop.
Python script to decrypt file encrypted by Monaca
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
# Blogpost: https://rz.my/2020/12/decrypting-monaca-encrypt-plugin.html | |
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | |
from cryptography.hazmat.primitives import padding | |
from cryptography.hazmat.backends import default_backend | |
from base64 import b64decode, b64encode | |
import argparse | |
def encrypt(key, iv, data): | |
cipher = Cipher(algorithms.AES(key), modes.CBC(iv)) | |
encryptor = cipher.encryptor() | |
encrypted = encryptor.update(data) + encryptor.finalize() | |
return encrypted | |
def decrypt(key, iv, data): | |
cipher = Cipher(algorithms.AES(key), modes.CBC(iv)) | |
decryptor = cipher.decryptor() | |
plain = decryptor.update(data) + decryptor.finalize() | |
return plain | |
def parsekey(hash): | |
key = bytes.fromhex(hash) | |
iv = [ord(s) for s in hash[0:16]] | |
return key, bytes(iv) | |
def isEncrypted(data): | |
header = [24, 8, 77, 69, 1, 13, 10, 0] | |
def main(): | |
parser = argparse.ArgumentParser(description="Decrypt file encrypted by MonacaEncrypt Plugin") | |
parser.add_argument("-k", "--key", help="Key Hash", action="store", default=False, required=True) | |
parser.add_argument("-f", "--file", help="File to decrypt", action="store", default=False, required=True) | |
args = parser.parse_args() | |
key,iv = parsekey(args.key) | |
with open(args.file, 'rb') as f: | |
data = f.read() | |
print(decrypt(key, iv, data[8:]).decode("utf-8", 'backslashreplace')) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment