Skip to content

Commit 259f470

Browse files
committed
Encryption code in now optimized
1 parent 45d35c2 commit 259f470

File tree

2 files changed

+88
-96
lines changed

2 files changed

+88
-96
lines changed

Image/encrypted_image.jpg

-530 Bytes
Loading

encryption.cpp

+88-96
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,120 @@
1-
#include <stdio.h>
21
#include <bits/stdc++.h>
32
#include <opencv2/opencv.hpp>
43

54
using namespace cv;
65
using namespace std;
76

87
const int MAX=1e4+79;
9-
bool flag[513][513];
8+
9+
/**
10+
u is the control parameter for logistic chaotic map,also known as population rate
11+
Here u is taken 3.94
12+
x is the vector that contain the value generated by chaotic map
13+
The initial value of the logistic chaotic map is 0.4
14+
*/
1015

1116
int main()
1217
{
1318
Mat image;
19+
int i,l;
20+
double u=3.94;
1421
vector<pair<double,int >> x;
15-
double u=3.94,hold,temp; //u is the control parameter for chaotic map,also known as population rate
22+
Vec<unsigned char, 3> pixel;
1623

17-
image = imread( "Image/sample_image_grey.jpg", 1 );
18-
if ( !image.data )
19-
{
20-
cout<<"No image data \n";
21-
return -1;
22-
}
24+
image = imread("Image/sample_image_grey.jpg", 0 );
25+
if ( !image.data )
26+
{
27+
cout<<"No image data \n";
28+
return -1;
29+
}
2330

24-
x.push_back({0.4,0});
25-
memset(flag ,false,sizeof(flag));
31+
x.push_back({0.4,0});
2632

27-
for (int i = 1; i <= 511; ++i){
28-
temp=x[i-1].first;
29-
hold=u*temp*(1-temp);
30-
x.push_back({hold,i});
31-
}
32-
sort(x.begin(), x.end());
3333

34-
int i=1;
35-
int l;
34+
double temp;
35+
for (int i = 1; i <= 511; ++i){
36+
temp=u*x[i-1].first*(1-x[i-1].first);
37+
x.push_back({temp,i});
38+
}
3639

37-
imshow("Shubham@shaurya", image);
38-
waitKey(0);
40+
sort(x.begin(), x.end());
3941

40-
i=0;
41-
int holds[4];
42-
for(int r = 0; r < image.rows; ++r) {
43-
for(int c = 0; c < image.cols; ++c) {
44-
if(i>511)
45-
i=0;
46-
int temps= x[i].second;
42+
imshow("Original image", image);
43+
waitKey(0);
4744

48-
holds[0]= image.at<Vec3b>(r,temps)[0];
49-
image.at<Vec3b>(r,temps)[0]=image.at<Vec3b>(r,c)[0];
50-
image.at<Vec3b>(r,c)[0]=holds[0];
45+
i=0;
46+
for(int r = 0; r < image.rows; ++r) {
47+
for(int c = 0; c < image.cols; ++c) {
48+
if(i>511)
49+
i=0;
50+
int temps= x[i].second;
5151

52-
holds[1]= image.at<Vec3b>(r,temps)[1];
53-
image.at<Vec3b>(r,temps)[1]=image.at<Vec3b>(r,c)[1];
54-
image.at<Vec3b>(r,c)[1]=holds[1];
52+
pixel= image.at<Vec3b>(r,temps);
53+
image.at<Vec3b>(r,temps)=image.at<Vec3b>(r,c);
54+
image.at<Vec3b>(r,c)=pixel;
5555

56+
i++;
57+
}
58+
}
5659

57-
holds[2]= image.at<Vec3b>(r,temps)[2];
58-
image.at<Vec3b>(r,temps)[2]=image.at<Vec3b>(r,c)[2];
59-
image.at<Vec3b>(r,c)[2]=holds[2];
60+
imshow("permutated image", image);
61+
waitKey(0);
6062

61-
i++;
63+
for(int r = 0; r < image.rows; ++r) {
64+
for(int c = 0; c < image.cols; ++c) {
65+
if(i>100){
66+
i=1;
6267
}
68+
l=x[i].first*MAX;
69+
l=l%255;
70+
image.at<Vec3b>(r,c)[0]=image.at<Vec3b>(r,c)[0]^l;
71+
image.at<Vec3b>(r,c)[1]=image.at<Vec3b>(r,c)[1]^l;
72+
image.at<Vec3b>(r,c)[2]=image.at<Vec3b>(r,c)[2]^l;
73+
i++;
6374
}
75+
}
6476

65-
for(int r = 0; r < image.rows; ++r) {
66-
for(int c = 0; c < image.cols; ++c) {
67-
if(i>100){
68-
i=1;
69-
}
70-
l=x[i].first*MAX;
71-
l=l%255;
72-
image.at<Vec3b>(r,c)[0]=image.at<Vec3b>(r,c)[0]^l;
73-
image.at<Vec3b>(r,c)[1]=image.at<Vec3b>(r,c)[1]^l;
74-
image.at<Vec3b>(r,c)[2]=image.at<Vec3b>(r,c)[2]^l;
75-
i++;
76-
}
77-
}
78-
79-
80-
imwrite("Image/encrypted_image.jpg",image);
81-
imshow("Shubham@shaurya", image);
82-
waitKey(0);
83-
84-
i=1;
85-
for(int r = 0; r < image.rows; ++r) {
77+
imwrite("Image/encrypted_image.jpg",image);
78+
imshow("Encrypted image", image);
79+
waitKey(0);
80+
81+
i=1;
82+
for(int r = 0; r < image.rows; ++r) {
8683
for(int c = 0; c < image.cols; ++c) {
8784
if(i>100){
88-
i=1;
89-
}
90-
l=x[i].first*MAX;
91-
l=l%255;
92-
image.at<Vec3b>(r,c)[0]=image.at<Vec3b>(r,c)[0]^l;
93-
image.at<Vec3b>(r,c)[1]=image.at<Vec3b>(r,c)[1]^l;
94-
image.at<Vec3b>(r,c)[2]=image.at<Vec3b>(r,c)[2]^l;
95-
i++;
96-
}
97-
}
98-
99-
i=511;
100-
for(int r = image.rows-1; r >= 0; --r) {
101-
for(int c = image.cols-1; c >= 0 ; --c) {
102-
if(i<0)
103-
i=511;
104-
int temps= x[i].second;
105-
106-
holds[0]= image.at<Vec3b>(r,temps)[0];
107-
image.at<Vec3b>(r,temps)[0]=image.at<Vec3b>(r,c)[0];
108-
image.at<Vec3b>(r,c)[0]=holds[0];
109-
110-
holds[1]= image.at<Vec3b>(r,temps)[1];
111-
image.at<Vec3b>(r,temps)[1]=image.at<Vec3b>(r,c)[1];
112-
image.at<Vec3b>(r,c)[1]=holds[1];
113-
114-
115-
holds[2]= image.at<Vec3b>(r,temps)[2];
116-
image.at<Vec3b>(r,temps)[2]=image.at<Vec3b>(r,c)[2];
117-
image.at<Vec3b>(r,c)[2]=holds[2];
118-
119-
i--;
85+
i=1;
12086
}
87+
l=x[i].first*MAX;
88+
l=l%255;
89+
image.at<Vec3b>(r,c)[0]=image.at<Vec3b>(r,c)[0]^l;
90+
image.at<Vec3b>(r,c)[1]=image.at<Vec3b>(r,c)[1]^l;
91+
image.at<Vec3b>(r,c)[2]=image.at<Vec3b>(r,c)[2]^l;
92+
i++;
12193
}
94+
}
95+
96+
imshow("Decrepted Diffused image", image);
97+
waitKey(0);
98+
99+
i=511;
100+
for(int r = image.rows-1; r >= 0; --r) {
101+
for(int c = image.cols-1; c >= 0 ; --c) {
102+
if(i<0)
103+
i=511;
104+
int temps= x[i].second;
105+
106+
pixel= image.at<Vec3b>(r,temps);
107+
image.at<Vec3b>(r,temps)=image.at<Vec3b>(r,c);
108+
image.at<Vec3b>(r,c)=pixel;
109+
110+
i--;
111+
}
112+
}
113+
114+
namedWindow("Decrepted", WINDOW_AUTOSIZE );
115+
imshow("Decrepted", image);
116+
waitKey(0);
122117

123-
namedWindow("Shubham@shaurya", WINDOW_AUTOSIZE );
124-
imshow("Shubham@shaurya", image);
125-
waitKey(0);
118+
return 0;
126119

127-
return 0;
128-
}
120+
}

0 commit comments

Comments
 (0)