]> xmof Git - DeDRM.git/commitdiff
Fix python2 issues in kgenpids and kindlekey
authorNoDRM <no_drm123@protonmail.com>
Thu, 3 Aug 2023 08:50:06 +0000 (10:50 +0200)
committerNoDRM <no_drm123@protonmail.com>
Thu, 3 Aug 2023 09:26:05 +0000 (11:26 +0200)
CHANGELOG.md
DeDRM_plugin/kgenpids.py
DeDRM_plugin/kindlekey.py

index 3bb9848d41494bfb00e22aa9ead7ff34f6a1160d..b5537cdfa88ff46a208b15e539cb47d505fc749d 100644 (file)
@@ -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).
index 31cb6a5c49ed4a20e83008bfbab03569a6a4f924..d11c3dabbca32bb404231d7121c5597ce1ec76e7 100644 (file)
@@ -53,11 +53,17 @@ def SHA1(message):
 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
@@ -84,8 +90,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
@@ -97,7 +106,8 @@ def encodePID(hash):
     global charMap3
     PID = b''
     for position in range (0,8):
-        PID += bytes([charMap3[getSixBitsFromBitField(hash,position)]])
+        PID += bytes(bytearray([charMap3[getSixBitsFromBitField(hash,position)]]))
+        
     return PID
 
 # Encryption table used to generate the device PID
@@ -134,7 +144,7 @@ def generateDevicePID(table,dsn,nbRoll):
         index = (index+1) %8
     for counter in range (0,8):
         index = ((((pid[counter] >>5) & 3) ^ pid[counter]) & 0x1f) + (pid[counter] >> 7)
-        pidAscii += bytes([charMap4[index]])
+        pidAscii += bytes(bytearray([charMap4[index]]))
     return pidAscii
 
 def crc32(s):
@@ -150,7 +160,7 @@ def checksumPid(s):
     for i in (0,1):
         b = crc & 0xff
         pos = (b // l) ^ (b % l)
-        res += bytes([charMap4[pos%l]])
+        res += bytes(bytearray([charMap4[pos%l]]))
         crc >>= 8
     return res
 
@@ -161,14 +171,17 @@ def pidFromSerial(s, l):
     crc = crc32(s)
     arr1 = [0]*l
     for i in range(len(s)):
-        arr1[i%l] ^= s[i]
+        if sys.version_info[0] == 2:
+            arr1[i%l] ^= ord(s[i])
+        else: 
+            arr1[i%l] ^= s[i]
     crc_bytes = [crc >> 24 & 0xff, crc >> 16 & 0xff, crc >> 8 & 0xff, crc & 0xff]
     for i in range(l):
         arr1[i] ^= crc_bytes[i&3]
     pid = b""
     for i in range(l):
         b = arr1[i] & 0xff
-        pid += bytes([charMap4[(b >> 7) + ((b >> 5 & 3) ^ (b & 0x1f))]])
+        pid += bytes(bytearray([charMap4[(b >> 7) + ((b >> 5 & 3) ^ (b & 0x1f))]]))
     return pid
 
 
index 14e4ed187d15bf1a820e33596747439a44b7dcd5..e8a0e65b5b2d54c9d84d3d2fe197be89c8be25cb 100644 (file)
@@ -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