Last active
May 26, 2023 15:19
-
-
Save HarHarLinks/eb1fdbfddb22c854da05f38885445dda to your computer and use it in GitHub Desktop.
decrypt matrix attachments v2 https://spec.matrix.org/v1.7/client-server-api/#sending-encrypted-attachments
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
#!/usr/bin/env python3 | |
# import ... | |
k = event.key["k"] | |
key = base64.urlsafe_b64decode(k + "=" * (-len(k) % 4)) | |
iv = base64.urlsafe_b64decode(event.iv + "=" * (-len(event.iv) % 4)) | |
cypher = AES.new(key, AES.MODE_CTR, nonce=iv[:8]) | |
print(cypher.decrypt(response.body)) |
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
#!/usr/bin/env bash | |
# i haven't yet found a base64 tool that decodes unpadded url-safe data all at once | |
# this is a dirty solution that seems to work with zsh | |
padded = ${unpadded}$(printf '=%.0s' {1..$(expr $(echo -n ${unpadded} | wc -c) % 4)}) | |
openssl enc -d -aes-256-ctr -in encryptedImage.png.enc -out decryptedImage.png -iv "$(echo -n $padded_iv | basenc --base64url -d | xxd -p )" -K "$(echo -n $padded_k | basenc --base64url -d | xxd -p -c 0)" -p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment