def decrypt(file): file_content = file.read() body = re.search('"Body": "(\w+)"', file_content) if body is None: try: e2b = base64.b64decode(file_content) data = re.search(b'0x;(.*?)x;0', e2b).group(1) array = base64.b64decode(data) salt, IV, array2 = getKeys(array) bytesl = PBKDF2(NEW_PBKDF2_key, salt, 32, 1000) rijndael_cbc = RijndaelCbc(key=bytesl, iv=IV, padding=ZeroPadding(32), block_size=32) return rijndael_cbc.decrypt(array2).decode() except binascii.Error: return -1 else: body = body.group(1) des_object = DES.new(static_key, DES.MODE_CBC, static_IV) array = [] for i in range(int(len(body) / 2)): array.append(binascii.unhexlify(body[i * 2:i * 2 + 2])) string = base64.b64decode(b''.join(array)) b64 = des_object.decrypt(string).replace(b'\x04', b'') e2b = base64.b64decode(b64) data = re.search(b'x0;(.*?)0;x', e2b).group(1) array = base64.b64decode(data) salt, IV, array2 = getKeys(array) bytesl = PBKDF2(OLD_PBKDF2_key, salt, 32, 1000) rijndael_cbc = RijndaelCbc(key=bytesl, iv=IV, padding=ZeroPadding(32), block_size=32) return rijndael_cbc.decrypt(array2).decode()
def decrypt(key, block_size, cipher_message): """ AES key,iv are the same mode cbc pkcs7 :param key: :param content: :return: """ kc = key['kc'] iv = key['iv'] ciphertext = cipher_message['ciphertext'] rijndael_cbc = RijndaelCbc( key=kc, # if it is a string we can use base64.b64decode(key), iv=iv, padding=ZeroPadding(block_size), block_size=block_size ) return rijndael_cbc.decrypt(ciphertext)
def decrypt(self, encrypted_data, previously_processed_data_index=None) -> bytes: if previously_processed_data_index is None: length = len(self.old_decrypt) if length % self.BLOCK_SIZE == 0: previously_processed_data_index = length else: previously_processed_data_index = int( self.BLOCK_SIZE * math.floor(length / self.BLOCK_SIZE)) if previously_processed_data_index % self.BLOCK_SIZE != 0: previously_processed_data_index = int( self.BLOCK_SIZE * math.ceil(previously_processed_data_index / self.BLOCK_SIZE)) remaining_data = self.old_decrypt[previously_processed_data_index:] if self.old_decrypt != '': self.decrypt_init_vector = self.old_decrypt[ previously_processed_data_index - self.BLOCK_SIZE:previously_processed_data_index] self.old_decrypt = encrypted_data cbc = RijndaelCbc(key=self.key, iv=self.decrypt_init_vector, padding=ZeroPadding(self.BLOCK_SIZE), block_size=self.BLOCK_SIZE) decrypt = cbc.decrypt(encrypted_data) return decrypt
def decrypt(file): key = 'ay$a5%&j' IV = 'abc@9879' PBKDF2_key = '0THISISOBmodedByForlaxNIGGAs' file_content = file.read() body = re.search('"Body": "(\w+)"', file_content) if body is None:return -1 else:body = body.group(1) des_object = DES.new(key, DES.MODE_CBC, IV) array = [] for i in range(int(len(body)/2)): array.append(binascii.unhexlify(body[i*2:i*2+2])) string = base64.b64decode(b''.join(array)) b64 = des_object.decrypt(string).replace(b'\x04', b'') e2b = base64.b64decode(b64) data = re.search(b'x0;(.*?)0;x', e2b).group(1) array = base64.b64decode(data) salt = array[:32] IV = array[32:64] array2 = array[64:] bytesl = PBKDF2(PBKDF2_key,salt,32, 1000) rijndael_cbc = RijndaelCbc( key=bytesl, iv=IV, padding=ZeroPadding(32), block_size=32 ) return rijndael_cbc.decrypt(array2).decode()
def decrypt(self, encText, previouslyProcessedData=None): if previouslyProcessedData is None: l = len(self.oldDecrypt) if l % BLOCK_SIZE == 0: previouslyProcessedData = l else: previouslyProcessedData = int(BLOCK_SIZE * math.floor(l / BLOCK_SIZE)) #print previouslyProcessedData # previouslyProcessedData was passed by the parent: it means that a frame was decoded and there was some data left. This does not include the padding zeros if previouslyProcessedData % BLOCK_SIZE != 0: previouslyProcessedData = int( BLOCK_SIZE * math.ceil(previouslyProcessedData / BLOCK_SIZE)) remainingData = self.oldDecrypt[previouslyProcessedData:] if self.oldDecrypt != b'': self.decryptIV = self.oldDecrypt[ previouslyProcessedData - BLOCK_SIZE:previouslyProcessedData] self.oldDecrypt = encText # save current block toDecrypt = truncate_multiple(remainingData + encText, BLOCK_SIZE) decryptor = RijndaelCbc(self.key, self.decryptIV, padding=ZeroPadding(BLOCK_SIZE), block_size=BLOCK_SIZE) return decryptor.decrypt(toDecrypt)
def encrypt(self, plainText): encryptor = RijndaelCbc(self.key, self.encryptIV, padding=ZeroPadding(BLOCK_SIZE), block_size=BLOCK_SIZE) encText = encryptor.encrypt(plainText) self.encryptIV = encText[-BLOCK_SIZE:] return encText
def encrypt(self, plain_data: Union[str, bytes]) -> bytes: if isinstance(plain_data, str): plain_data = bytes(plain_data, encoding="latin_1") cbc = RijndaelCbc(key=self.key, iv=self.encrypt_init_vector, padding=ZeroPadding(self.BLOCK_SIZE), block_size=self.BLOCK_SIZE) encrypted_data = cbc.encrypt(plain_data) self.encrypt_init_vector = encrypted_data[-self.BLOCK_SIZE:] return encrypted_data
def encrypt(iv, key, block_size, plaintext): rijndael_cbc = RijndaelCbc( key=key, # if it is a string we can use base64.b64decode(key), iv=iv, padding=ZeroPadding(block_size), block_size=block_size) ciphertext = rijndael_cbc.encrypt(plaintext) return ciphertext
def encrypt(key, block_size, plaintext): iv = key['iv'] kc = key['kc'] ki = key['ki'] rijndael_cbc = RijndaelCbc( key=kc, # if it is a string we can use base64.b64decode(key), iv=iv, padding=ZeroPadding(block_size), block_size=block_size) ciphertext = rijndael_cbc.encrypt(plaintext) rijndael_cbc = RijndaelCbc( key=ki, # if it is a string we can use base64.b64decode(key), iv=iv, padding=ZeroPadding(block_size), block_size=block_size) MAC = rijndael_cbc.encrypt(plaintext)[-block_size:] return ciphertext, MAC
def decrypt(iv, key, block_size, ciphertext): """ AES key,iv are the same mode cbc pkcs7 :param key: :param content: :return: """ rijndael_cbc = RijndaelCbc( key=key, # if it is a string we can use base64.b64decode(key), iv=iv, padding=ZeroPadding(block_size), block_size=block_size) return rijndael_cbc.decrypt(ciphertext)
async def from_submission(cls, data_enc: str, iv: str, osu_ver: str, phash: str) -> None: """Create a score object from an osu! submission string.""" cbc = RijndaelCbc(f'osu!-scoreburgr---------{osu_ver}', iv=base64.b64decode(iv).decode('latin_1'), padding=ZeroPadding(32), block_size=32) data = cbc.decrypt( base64.b64decode(data_enc).decode('latin_1')).decode().split(':') if len(data) != 18: plog('Received an invalid score submission.', Ansi.LRED) return s = cls() if len(map_md5 := data[0]) != 32: return
async def from_submission(cls, data_b64: str, iv_b64: str, osu_ver: str, pw_md5: str) -> Optional['Score']: """Create a score object from an osu! submission string.""" iv = b64decode(iv_b64).decode('latin_1') data_aes = b64decode(data_b64).decode('latin_1') aes_key = f'osu!-scoreburgr---------{osu_ver}' aes = RijndaelCbc(aes_key, iv, ZeroPadding(32), 32) # score data is delimited by colons (:). data = aes.decrypt(data_aes).decode().split(':') if len(data) != 18: log('Received an invalid score submission.', Ansi.LRED) return s = cls() if len(map_md5 := data[0]) != 32: return
def test_rijndael_cbc(self): key = 'qBS8uRhEIBsr8jr8vuY9uUpGFefYRL2HSTtrKhaI1tk=' iv = 'kByhT6PjYHzJzZfXvb8Aw5URMbQnk6NM+g3IV5siWD4=' rijndael_cbc = RijndaelCbc(key=base64.b64decode(key), iv=base64.b64decode(iv), padding=ZeroPadding(32), block_size=32) plain_text = b'Mahdi' padded_text = plain_text.ljust(32, b'\x1b') cipher = rijndael_cbc.encrypt(padded_text) cipher_text = base64.b64encode(cipher) self.assertEqual(cipher_text, b'1KGc0PMt52Xbell+2y9qDJJp/Yy6b1JR1JWI3f9ALF4=') self.assertEqual(rijndael_cbc.decrypt(cipher), padded_text) # Long blocks plain_text = b'lorem' * 50 padded_text = plain_text.ljust(32, b'\x1b') cipher = rijndael_cbc.encrypt(padded_text) self.assertEqual(rijndael_cbc.decrypt(cipher), padded_text)
def test_zero_padding(self): padding = ZeroPadding(block_size=16) # Full length source = b'loremipsumdolors' encoded_source = padding.encode(source) self.assertEqual(encoded_source, source) self.assertEqual(len(encoded_source), 16) self.assertEqual(padding.decode(encoded_source), source) # Length 2 source = b'hi' encoded_source = padding.encode(source) self.assertEqual(encoded_source, source + b'\x00' * 14) self.assertEqual(len(encoded_source), 16) self.assertEqual(padding.decode(encoded_source), source) # Length 1 source = b'h' encoded_source = padding.encode(source) self.assertEqual(encoded_source, source + b'\x00' * 15) self.assertEqual(len(encoded_source), 16) self.assertEqual(padding.decode(encoded_source), source) # Zero length self.assertEqual(padding.decode(b''), b'') # Wrong value to decode with self.assertRaises(AssertionError): padding.decode(b'no-padding')
def test_zero_padding(self) -> None: padding = ZeroPadding(block_size=16) # Full length source = b"loremipsumdolors" encoded_source = padding.encode(source) self.assertEqual(encoded_source, source) self.assertEqual(len(encoded_source), 16) self.assertEqual(padding.decode(encoded_source), source) # Length 2 source = b"hi" encoded_source = padding.encode(source) self.assertEqual(encoded_source, source + b"\x00" * 14) self.assertEqual(len(encoded_source), 16) self.assertEqual(padding.decode(encoded_source), source) # Length 1 source = b"h" encoded_source = padding.encode(source) self.assertEqual(encoded_source, source + b"\x00" * 15) self.assertEqual(len(encoded_source), 16) self.assertEqual(padding.decode(encoded_source), source) # Zero length self.assertEqual(padding.decode(b""), b"") # Wrong value to decode with self.assertRaises(AssertionError): padding.decode(b"no-padding")