-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncriptar_Mensaje.py
More file actions
84 lines (56 loc) · 2.19 KB
/
Encriptar_Mensaje.py
File metadata and controls
84 lines (56 loc) · 2.19 KB
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
import numpy as np
import math
import cv2
#Message to encrypt
message = input("Message: ")
#Image to encrypt in
image = input("Name of image: ")
#Channel
color = int(input("Channel: "))
#List that will store every value for each letter
code = []
#Codification matrix
codificationMatrix = np.array([[0,1,1],[1,0,1],[1,0,0]])
#Iterating letter by letter in the message
for letter in message:
#Archive that has the values of each letter in the alphabet
archive = open("codigo2.txt", "r")
#Iterating each line in the archive
for line in archive.readlines():
#If a letter from the archive match with the current letter in the message
if letter == line[0]:
#Adding to the list the code for that letter
code.append(int(line[1:4]))
archive.close()
#Condition if the matrix is full
if len(message) < 3*math.ceil(len(message)/3):
#For each character that is missing, it fills with spaces
for missing in range(3*math.ceil(len(message)/3)-len(message)):
code.append(99)
#Storing the length of the message
lenMessage = len(code)
print(lenMessage)
#Reshape and convert the list to a matrix of 3 x n dimensions
matrixMessage = np.array(code).reshape(math.ceil(len(message)/3),3)
matrixMessage = np.transpose(matrixMessage)
#Multiplying the codification matrix by the matrix that has the message
codificatedMatrix = codificationMatrix.dot(matrixMessage)
m,n = codificatedMatrix.shape
print(codificatedMatrix)
#Convert to list to iterate it
matrixList = codificatedMatrix.tolist()
#Load an image
imageToEncrypt = cv2.imread(image, 1)
#Store the number of columns and color in the first pixel
imageToEncrypt[1,1,1] = n
imageToEncrypt[1,1,0] = color
PosX = 100
PosY = 100
pixel = [0,0,0]
#Iterating the codificated matrix
for i in range(0,m):
for j in range(0,n):
#Assignment of the values to the Green pixel
imageToEncrypt[PosX+20*i, PosY+20*j, color] = matrixList[i][j]
#Storing the image with the message already encrypted
cv2.imwrite("pruebaencriptada.png", imageToEncrypt)