Skip to content

Commit

Permalink
Fix python2 issues in kgenpids and kindlekey
Browse files Browse the repository at this point in the history
  • Loading branch information
noDRM committed Aug 3, 2023
1 parent e82d2b5 commit 88b37d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,4 @@ This is v10.0.9, a release candidate for v10.1.0. I don't expect there to be maj

- Fix a bug where decrypting a 40-bit RC4 pdf with R=2 didn't work.
- Fix a bug where decrypting a 256-bit AES pdf with V=5 didn't work.
- Fix bugs in kgenpids.py and kindlekey.py that caused it to fail on Python 2 (#380).
13 changes: 10 additions & 3 deletions DeDRM_plugin/kgenpids.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ def decode(data,map):
def getTwoBitsFromBitField(bitField,offset):
byteNumber = offset // 4
bitPosition = 6 - 2*(offset % 4)
return bitField[byteNumber] >> bitPosition & 3

if sys.version_info[0] == 2:
return ord(bitField[byteNumber]) >> bitPosition & 3
else:
return bitField[byteNumber] >> bitPosition & 3

# Returns the six bits at offset from a bit field
def getSixBitsFromBitField(bitField,offset):
offset *= 3
Expand All @@ -97,7 +100,11 @@ def encodePID(hash):
global charMap3
PID = b''
for position in range (0,8):
PID += bytes([charMap3[getSixBitsFromBitField(hash,position)]])
if sys.version_info[0] == 2:
PID += charMap3[getSixBitsFromBitField(hash,position)]
else:
PID += bytes([charMap3[getSixBitsFromBitField(hash,position)]])

return PID

# Encryption table used to generate the device PID
Expand Down
12 changes: 9 additions & 3 deletions DeDRM_plugin/kindlekey.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,17 @@ def primes(n):
def encode(data, map):
result = b''
for char in data:
value = char
if sys.version_info[0] == 2:
value = ord(char)
else:
value = char

Q = (value ^ 0x80) // len(map)
R = value % len(map)
result += bytes([map[Q]])
result += bytes([map[R]])

result += bytes(bytearray([map[Q]]))
result += bytes(bytearray([map[R]]))

return result

# Hash the bytes in data and then encode the digest with the characters in map
Expand Down

0 comments on commit 88b37d2

Please sign in to comment.