-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding back subtitle decrypt modules (#18)
* Adding back subtitle decrypt modules * Adding cryptography library * isort and black format * poetry update * Add timeout to requests
- Loading branch information
Showing
8 changed files
with
1,269 additions
and
1,017 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from base64 import b64decode, b64encode | ||
|
||
from cryptography.hazmat.backends import default_backend | ||
from cryptography.hazmat.primitives import padding | ||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | ||
|
||
|
||
class AESCipher: | ||
def __init__(self, key, initialization_vector): | ||
self.key = key.encode("utf8") | ||
self.initialization_vector = initialization_vector.encode("utf8") | ||
|
||
def __get_cipher(self): | ||
return Cipher(algorithms.AES(self.key), modes.CBC(self.initialization_vector), backend=default_backend()) | ||
|
||
def decrypt(self, encrypted_text): | ||
cipher = self.__get_cipher() | ||
decryptor = cipher.decryptor() | ||
decrypted_text = decryptor.update(b64decode(encrypted_text)) + decryptor.finalize() | ||
unpadder = padding.PKCS7(128).unpadder() | ||
decrypted_text = unpadder.update(decrypted_text) + unpadder.finalize() | ||
decrypted_text = decrypted_text.rstrip(b"\0").decode("utf8") | ||
return decrypted_text | ||
|
||
def encrypt(self, text): | ||
cipher = self.__get_cipher() | ||
padder = padding.PKCS7(128).padder() | ||
padded_data = padder.update(text.encode("utf8")) + padder.finalize() | ||
encryptor = cipher.encryptor() | ||
encrypted = encryptor.update(padded_data) + encryptor.finalize() | ||
return b64encode(encrypted).decode("utf8") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pysrt | ||
|
||
from kisskh_downloader.helper.aes_cipher import AESCipher | ||
|
||
|
||
class SubtitleDecrypter: | ||
def __init__(self, key, initialization_vector): | ||
self.cipher = AESCipher(key, initialization_vector) | ||
|
||
def decrypt_subtitles(self, file_path): | ||
subs = pysrt.open(file_path) | ||
|
||
for sub in subs: | ||
decrypted_text = self.cipher.decrypt(sub.text) | ||
sub.text = decrypted_text | ||
|
||
return subs |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from kisskh_downloader.helper.aes_cipher import AESCipher | ||
|
||
|
||
def test_decrypt(): | ||
text = "Hello There" | ||
key = "7846286482638268" | ||
initialization_vector = "4628745283719461" | ||
|
||
aes_cipher = AESCipher(key=key, initialization_vector=initialization_vector) | ||
encrypted_text = aes_cipher.encrypt(text) | ||
assert aes_cipher.decrypt(encrypted_text) == text |