Skip to content

Commit fcab6af

Browse files
author
Enraged
authored
Fixed Multiple Encoding Issues
Running the script as is throws errors due to the handling of the plaintext variable which can appear as a string or as a binary string when handed over to the encrypt_string function. Additionally, use of the base64 library is corrected.
1 parent 3632ab3 commit fcab6af

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

chapter09/ie_exfil.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import random
66
import zlib
7+
import base64
78

89
from Crypto.PublicKey import RSA
910
from Crypto.Cipher import PKCS1_OAEP
@@ -31,24 +32,26 @@ def wait_for_browser(browser):
3132

3233

3334
def encrypt_string(plaintext):
34-
chunk_size = 256
35+
chunk_size = 208
36+
if isinstance(plaintext, (str)):
37+
plaintext = plaintext.encode()
3538
print("Compressing: %d bytes" % len(plaintext))
3639
plaintext = zlib.compress(plaintext)
3740
print("Encrypting %d bytes" % len(plaintext))
3841

3942
rsakey = RSA.importKey(public_key)
4043
rsakey = PKCS1_OAEP.new(rsakey)
41-
encrypted = ""
44+
encrypted = b""
4245
offset = 0
4346

4447
while offset < len(plaintext):
45-
chunk = plaintext[offset:offset + 256]
48+
chunk = plaintext[offset:offset + chunk_size]
4649
if len(chunk) % chunk_size != 0:
47-
chunk += " " * (chunk_size - len(chunk))
50+
chunk += b" " * (chunk_size - len(chunk))
4851
encrypted += rsakey.encrypt(chunk)
4952
offset += chunk_size
5053

51-
encrypted = encrypted.encode("base64")
54+
encrypted = base64.b64encode(encrypted)
5255
print("Base64 encoded crypto: %d" % len(encrypted))
5356
return encrypted
5457

0 commit comments

Comments
 (0)