-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharnoldcat_client.py
97 lines (75 loc) · 4.57 KB
/
arnoldcat_client.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import socket
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import cv2
import numpy as np
import base64
from math import log
def ArnoldCatTransform(img, num):
rows, cols, ch = img.shape
n = rows
img_arnold = np.zeros([rows, cols, ch])
for x in range(0, rows):
for y in range(0, cols):
img_arnold[x][y] = img[(x+y)%n][(x+2*y)%n]
return img_arnold
def ArnoldCatDecryption(imageName, key):
img = imageName
rows, cols, ch = img.shape
dimension = rows
decrypt_it = dimension
if (dimension%2==0) and 5**int(round(log(dimension/2,5))) == int(dimension/2):
decrypt_it = 3*dimension
elif 5**int(round(log(dimension,5))) == int(dimension):
decrypt_it = 2*dimension
elif (dimension%6==0) and 5**int(round(log(dimension/6,5))) == int(dimension/6):
decrypt_it = 2*dimension
else:
decrypt_it = int(12*dimension/7)
for i in range(key,decrypt_it):
img = ArnoldCatTransform(img, i)
return img
def decrypt_key(private_key, encrypted_key):
private_key_bytes = RSA.import_key(base64.b64decode(private_key))
cipher = PKCS1_OAEP.new(private_key_bytes)
decrypted_key = int.from_bytes(cipher.decrypt(encrypted_key), byteorder='big')
return decrypted_key
def receive_image(client_socket):
# Receive the encrypted Arnold Cat key
private_key = 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDElbNLYnVoN/+yb5aqlUL2UV28dg3QaLBt7RLJf+2Zpn7ie07jctF0bsoHdZ4v6ppvryGWC0h/RSTnznRjZt0Wh9gOdqYmaydvpU8hPXUkckNU5s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NMxBw0swxKJBS1Fq++tsC7gaDin8+nucAB3JAoGAPHvyWbP4niKNcpF19CUABktavSi9APKbzCt7D26bCftYtJmddVd9ndxg04Ihrw26Y8ii+vIXUgb6IN8fNLtvNbiz3WOOp2LLfem+E1/enM/aDIe2yZ4VCOhTnSkmCyoYkBSoW+QS88XltpFou+6pRmMJnEKG5moYMzFEKE4LfpUCgYA5NweVA+E1hLPQWdUSLrUa5uKZmsW9EPRYcmIOsK4RlE4/6JYRAZtV1d7F8d/1VgkHYg0D8XpFT5BPlA/jeNGTbATcLk8rRX0AoUnACuw4G0xc3k1T0otigZCv2jxu6lptI8hqdlE16C6OCyBxgKFeEpmbkHvrBYetE3HyOFkl4Q=='
output1 = client_socket.recv(2048)
encrypted_key = output1
decrypted_key = decrypt_key(private_key, encrypted_key)
# Send acknowledgment before receiving the second output
client_socket.send("ACK".encode())
# Receive the second output
output2 = client_socket.recv(4096)
encrypted_img = output2
# Perform Arnold Cat Decryption
img = cv2.imdecode(np.frombuffer(encrypted_img, dtype=np.uint8), cv2.IMREAD_COLOR)
print(img)
decrypted_img = ArnoldCatDecryption(img, decrypted_key)
return decrypted_img
def main():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('10.12.202.202', 12346))
# Ask the user to enter the name of the image
image_name = input("Enter the name of the image (e.g., test.png): ")
client_socket.send(image_name.encode())
#private_key = open('private.pem', 'rb').read()
#public_key = open('public.pem', 'rb').read()
#client_socket.send(public_key)
# Receive the encrypted image and decrypt it
decrypted_img = receive_image(client_socket)
# Display the decrypted image
#cv2.imshow("Decrypted Image", decrypted_img)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
# Save the decrypted image to a file
decrypted_image_name = "decrypted_image.png"
cv2.imwrite(decrypted_image_name, decrypted_img)
print(f"Decrypted image saved as {decrypted_image_name}")
# Close the connection
client_socket.close()
if __name__ == "__main__":
main()