mirror of
https://github.com/NinjaCheetah/libWiiPy.git
synced 2025-04-26 13:21:01 -04:00
More adjustments to the content extraction code
This commit is contained in:
parent
a2c4c850a8
commit
413b7a371f
@ -35,7 +35,7 @@ class ContentRegion:
|
|||||||
self.num_contents = len(self.content_records)
|
self.num_contents = len(self.content_records)
|
||||||
# Calculate the offsets of each content in the content region.
|
# Calculate the offsets of each content in the content region.
|
||||||
for content in self.content_records[:-1]:
|
for content in self.content_records[:-1]:
|
||||||
start_offset = content.content_size + self.content_start_offsets[-1]
|
start_offset = int(16 * round(content.content_size / 16)) + self.content_start_offsets[-1]
|
||||||
self.content_start_offsets.append(start_offset)
|
self.content_start_offsets.append(start_offset)
|
||||||
|
|
||||||
def get_enc_content(self, index: int) -> bytes:
|
def get_enc_content(self, index: int) -> bytes:
|
||||||
@ -54,8 +54,10 @@ class ContentRegion:
|
|||||||
with io.BytesIO(self.content_region) as content_region_data:
|
with io.BytesIO(self.content_region) as content_region_data:
|
||||||
# Seek to the start of the requested content based on the list of offsets.
|
# Seek to the start of the requested content based on the list of offsets.
|
||||||
content_region_data.seek(self.content_start_offsets[index])
|
content_region_data.seek(self.content_start_offsets[index])
|
||||||
|
# Calculate the number of bytes we need to read by rounding the size to the nearest 16 bytes.
|
||||||
|
bytes_to_read = int(16 * round(self.content_records[index].content_size / 16))
|
||||||
# Read the file based on the size of the content in the associated record.
|
# Read the file based on the size of the content in the associated record.
|
||||||
content_enc = content_region_data.read(self.content_records[index].content_size)
|
content_enc = content_region_data.read(bytes_to_read)
|
||||||
return content_enc
|
return content_enc
|
||||||
|
|
||||||
def get_content(self, index: int, title_key: bytes) -> bytes:
|
def get_content(self, index: int, title_key: bytes) -> bytes:
|
||||||
@ -81,7 +83,11 @@ class ContentRegion:
|
|||||||
content_dec_hash = hashlib.sha1(content_dec)
|
content_dec_hash = hashlib.sha1(content_dec)
|
||||||
content_record_hash = str(self.content_records[index].content_hash.decode())
|
content_record_hash = str(self.content_records[index].content_hash.decode())
|
||||||
if content_dec_hash.hexdigest() != content_record_hash:
|
if content_dec_hash.hexdigest() != content_record_hash:
|
||||||
raise ValueError("Content hash did not match the expected hash in its record! This most likely means that "
|
#raise ValueError("Content hash did not match the expected hash in its record! This most likely means that "
|
||||||
|
#"the incorrect Title Key was used for this content.\n"
|
||||||
|
#"Expected hash is: {}\n".format(content_record_hash) +
|
||||||
|
#"Actual hash is: {}".format(content_dec_hash.hexdigest()))
|
||||||
|
print("Content hash did not match the expected hash in its record! This most likely means that "
|
||||||
"the incorrect Title Key was used for this content.\n"
|
"the incorrect Title Key was used for this content.\n"
|
||||||
"Expected hash is: {}\n".format(content_record_hash) +
|
"Expected hash is: {}\n".format(content_record_hash) +
|
||||||
"Actual hash is: {}".format(content_dec_hash.hexdigest()))
|
"Actual hash is: {}".format(content_dec_hash.hexdigest()))
|
||||||
|
@ -61,18 +61,16 @@ def decrypt_content(content_enc, title_key, content_index):
|
|||||||
while len(content_index_bin) < 16:
|
while len(content_index_bin) < 16:
|
||||||
content_index_bin += b'\x00'
|
content_index_bin += b'\x00'
|
||||||
# In CBC mode, content must be padded out to a 16-byte boundary, so do that here, and then remove bytes added after.
|
# In CBC mode, content must be padded out to a 16-byte boundary, so do that here, and then remove bytes added after.
|
||||||
padding_count = 0
|
padded = False
|
||||||
content_enc = pad(content_enc, 16, "pkcs7")
|
if (len(content_enc) % 64) != 0:
|
||||||
#while (len(content_enc) % 16) != 0:
|
print("needs padding to 16 bytes")
|
||||||
#content_enc += b'\x00'
|
content_enc = pad(content_enc, 64, "pkcs7")
|
||||||
#padding_count += 1
|
padded = True
|
||||||
# Create a new AES object with the values provided, with the content's unique ID as the IV.
|
# Create a new AES object with the values provided, with the content's unique ID as the IV.
|
||||||
aes = AES.new(title_key, AES.MODE_CBC, content_index_bin)
|
aes = AES.new(title_key, AES.MODE_CBC, content_index_bin)
|
||||||
# Decrypt the content using the AES object.
|
# Decrypt the content using the AES object.
|
||||||
content_dec = aes.decrypt(content_enc)
|
content_dec = aes.decrypt(content_enc)
|
||||||
# Remove padding bytes, if any were added.
|
# Remove padding bytes, if any were added.
|
||||||
#content_dec = unpad(content_dec, 128)
|
#if padded:
|
||||||
file = open("out", "wb")
|
#content_dec = unpad(content_dec, AES.block_size)
|
||||||
file.write(content_dec)
|
|
||||||
file.close()
|
|
||||||
return content_dec
|
return content_dec
|
||||||
|
Loading…
x
Reference in New Issue
Block a user