Skip to content

Instantly share code, notes, and snippets.

@artjomb
Last active February 28, 2016 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artjomb/9a27bbaf400c21398d44 to your computer and use it in GitHub Desktop.
Save artjomb/9a27bbaf400c21398d44 to your computer and use it in GitHub Desktop.
from Crypto.PublicKey import RSA
readsize = 127
writesize = 128
private_key = RSA.generate(writesize * 8)
public_key = private_key.publickey()
f = open('test04_kahoot.png','rb')
p = open('test04_kahoot.png.enc','wb')
i = 0
while True:
data = f.read(readsize)
if not data:
break
i += 1
enc_data = public_key.encrypt(data, 32)[0]
p.write(chr(len(enc_data)))
p.write(enc_data)
p.close()
f.close()
f = open('test04_kahoot.png.enc','rb')
p = open('test04_kahoot.dec.png','wb')
j = 0
while True:
length = f.read(1)
if not length:
break
data = f.read(ord(length))
j += 1
dec_data = private_key.decrypt(data)
p.write(dec_data[:readsize])
p.close()
f.close()
print(i, j)
from Crypto.PublicKey import RSA
readsize = 127
writesize = 128
private_key = RSA.generate(writesize * 8)
public_key = private_key.publickey()
f = open('test04_kahoot.png','rb')
p = open('test04_kahoot.png.enc','wb')
i = 0
while True:
data = f.read(readsize)
if not data:
break
i += 1
enc_data = public_key.encrypt(data, 32)[0]
while len(enc_data) < writesize:
enc_data = "\x00" + enc_data
p.write(enc_data)
p.close()
f.close()
f = open('test04_kahoot.png.enc','rb')
p = open('test04_kahoot.dec.png','wb')
j = 0
while True:
data = f.read(writesize)
if not data:
break
j += 1
dec_data = private_key.decrypt(data)
p.write(dec_data[:readsize])
p.close()
f.close()
print(i, j)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment