-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.cpp
121 lines (97 loc) · 2.48 KB
/
encryption.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <bits/stdc++.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
const int MAX=1e4+79;
/**
u is the control parameter for logistic chaotic map,also known as population rate
Here u is taken 3.94
x is the vector that contain the value generated by chaotic map
The initial value of the logistic chaotic map is 0.4
*/
int main()
{
Mat image;
int i,l;
double u=3.94;
vector<pair<double,int >> x;
Vec<unsigned char, 3> pixel;
image = imread("Image/sample_image_grey.jpg", 0 );
if ( !image.data )
{
cout<<"No image data \n";
return -1;
}
x.push_back({0.4,0});
double temp;
for (int i = 1; i <= 511; ++i){
temp=u*x[i-1].first*(1-x[i-1].first);
x.push_back({temp,i});
}
sort(x.begin(), x.end());
imshow("Original image", image);
waitKey(0);
i=0;
for(int r = 0; r < image.rows; ++r) {
for(int c = 0; c < image.cols; ++c) {
if(i>511)
i=0;
int temps= x[i].second;
pixel= image.at<Vec3b>(r,temps);
image.at<Vec3b>(r,temps)=image.at<Vec3b>(r,c);
image.at<Vec3b>(r,c)=pixel;
i++;
}
}
imwrite("Image/encryption/permutated_image.jpg",image);
imshow("permutated image", image);
waitKey(0);
for(int r = 0; r < image.rows; ++r) {
for(int c = 0; c < image.cols; ++c) {
if(i>100){
i=1;
}
l=x[i].first*MAX;
l=l%255;
image.at<Vec3b>(r,c)[0]=image.at<Vec3b>(r,c)[0]^l;
image.at<Vec3b>(r,c)[1]=image.at<Vec3b>(r,c)[1]^l;
image.at<Vec3b>(r,c)[2]=image.at<Vec3b>(r,c)[2]^l;
i++;
}
}
imwrite("Image/encryption/encrypted_image.jpg",image);
imshow("Encrypted image", image);
waitKey(0);
i=1;
for(int r = 0; r < image.rows; ++r) {
for(int c = 0; c < image.cols; ++c) {
if(i>100){
i=1;
}
l=x[i].first*MAX;
l=l%255;
image.at<Vec3b>(r,c)[0]=image.at<Vec3b>(r,c)[0]^l;
image.at<Vec3b>(r,c)[1]=image.at<Vec3b>(r,c)[1]^l;
image.at<Vec3b>(r,c)[2]=image.at<Vec3b>(r,c)[2]^l;
i++;
}
}
imshow("Decrepted Diffused image", image);
waitKey(0);
i=511;
for(int r = image.rows-1; r >= 0; --r) {
for(int c = image.cols-1; c >= 0 ; --c) {
if(i<0)
i=511;
int temps= x[i].second;
pixel= image.at<Vec3b>(r,temps);
image.at<Vec3b>(r,temps)=image.at<Vec3b>(r,c);
image.at<Vec3b>(r,c)=pixel;
i--;
}
}
namedWindow("Decrypted", WINDOW_AUTOSIZE );
imshow("Decrypted", image);
waitKey(0);
return 0;
}