This repository was archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathencrypt.py
69 lines (63 loc) · 2.13 KB
/
encrypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
from Crypto.PublicKey import *
from Crypto.Util.number import *
import os, sys
class Unbuffered(object):
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
def writelines(self, datas):
self.stream.writelines(datas)
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
sys.stdout = Unbuffered(sys.stdout)
def _encrypt(message, e, n):
m = bytes_to_long(message)
return long_to_bytes(pow(m, e, n))
def _decrypt(ciphertext, d, n):
ct = bytes_to_long(ciphertext)
return long_to_bytes(pow(ct, d, n) % 2)
def genkey(size):
p = getPrime(size/2)
q = getPrime(size/2)
e = 65537
phin = (p-1)*(q-1)
d = inverse(e, phin)
n = p*q
return (p, q, e, d, phin, n)
if __name__ == "__main__":
p, q, e, d, phin, n = genkey(1024)
# Make any flag file to run this challenge
flag = open("flag").read().strip()
print "Welcome to RSA encryption oracle!"
print "Here take your flag (in hex): ", _encrypt(flag, e, n).encode("hex")
print "Here take modulus: ", n
for i in range(1050):
print "RSA service"
print "[1] Encrypt"
print "[2] Decrypt"
option = int(raw_input("Enter your choice: "))
if option == 1:
try:
message = raw_input("Enter the message you want to encrypt (in hex): ").decode("hex")
except:
print "Enter proper hex chars"
exit(0)
ct = _encrypt(message, e, n)
print "Here take your ciphertext (in hex): ", ct.encode("hex")
print "\n\n"
elif option == 2:
try:
ciphertext = raw_input("Enter the ciphertext you want to decrypt (in hex): ").decode("hex")
except:
print "Enter proper hex chars"
exit(0)
msg = _decrypt(ciphertext, d, n)
print "Here take your plaintext (in hex): ", msg.encode("hex")
print "\n\n"
else:
print "Enter a valid option!"
print "Exiting..."