2 //g++ -O3 one_round_new.cpp pixmap_io.o -o one_round_new -std=c++11
12 /*#include <cryptopp/hex.h>
13 #include <cryptopp/sha.h>
14 #include <cryptopp/osrng.h>
15 #include <cryptopp/secblock.h>
20 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
21 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
25 //using namespace CryptoPP;
37 typedef unsigned char uchar;
42 struct timeval tstart;
43 gettimeofday(&tstart,0);
44 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
47 double TimeStop(double t)
51 gettimeofday(&tend,0);
52 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
61 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
63 for(int i=0;i<size_tab;i++) {
64 inv_perm_tabs[tab[i]] = i;
71 void rc4key(uchar *key, uchar *sc, int size_DK) {
73 for(int i=0;i<256;i++) {
79 for(int i0=0; i0<256; i0++) {
80 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
89 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
95 for (int i=0;i<len;i++) {
98 for (int it = 0; it < rp; it++) {
100 for(int i0 = 0; i0<len; i0++) {
101 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
110 void prga(uchar *sc, int ldata, uchar *r) {
114 for (int it=0; it<ldata; it++) {
116 j0 = (j0 + sc[i0])&0xFF;
120 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
130 void encrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
133 uchar *X=new uchar[h2];
134 uchar *fX=new uchar[h2];
136 for(int it=0;it<len;it++) {
138 int ind2=Pbox[it]*h2;
140 for(int a=0;a<h2;a++) {
144 for(int a=0;a<h2;a++){
149 for(int a=0;a<h2;a++) {
154 for(int a=0;a<h2;a++) {
155 seq_out[ind1+a]=Sbox2[fX[a]];
158 for(int a=0;a<h2;a++) {
159 RM1[a]=RM1[PboxRM[a]];
170 void decrypt(uchar* seq_in, uchar *seq_out, int len,uchar* RM1,int *Pbox, int *PboxRM, uchar *Sbox1, uchar *Sbox2, int debug) {
173 uchar *fX=new uchar[h2];
176 uchar *Inv_Sbox1=new uchar[256];
177 inverse_tables(Sbox1,256,Inv_Sbox1);
179 uchar *Inv_Sbox2=new uchar[256];
180 inverse_tables(Sbox2,256,Inv_Sbox2);
185 for(int it=0;it<len;it++) {
188 int ind2=Pbox[it]*h2;
193 for(int a=0;a<h2;a++) {
194 fX[a]=seq_in[ind1+a];
196 for(int a=0;a<h2;a++) {
197 fX[a]=Inv_Sbox2[fX[a]];
199 for(int a=0;a<h2;a++) {
203 for(int a=0;a<h2;a++) {
204 RM1[a]=RM1[PboxRM[a]];
207 for(int a=0;a<h2;a++) {
208 seq_out[ind2+a]=Inv_Sbox1[fX[a]];
219 void Dynamickeygenerationnew(uchar *Secretkey, uchar *counter) {
228 uchar *data_R, *data_G, *data_B;
229 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
235 int imsize=width*height*3;
236 uchar* seq= new uchar[imsize];
237 uchar* seq2= new uchar[imsize];
239 int oneD=width*height;
240 for(int i=0;i<oneD;i++) {
242 seq[oneD+i]=data_G[i];
243 seq[2*oneD+i]=data_B[i];
250 int total_len=imsize;
252 int len= total_len/h2;
256 uchar *mix=new uchar[256];
261 for (int i = 0; i < 256 ; i++) {
262 // mix[i]=(int)secret_key.BytePtr()[i]^(int)mycounter.BytePtr()[i];
263 mix[i]=Secretkey[i]^counter[i];
267 SHA512().CalculateDigest(digest, mix, 256);
272 for (int i = 0; i < 64 ; i++) {
281 rc4key(DK, Sbox1, 16);
284 rc4key(&DK[16], Sbox2, 16);
289 rc4key(&DK[32], sc, 16);
291 uchar outd[2*(h * h)];
292 prga(sc, 2*(h * h), outd);
297 for(int i=0;i<h2;i++){
308 for (int i = 48; i < 64; i++)
312 int *Pbox=new int[len];
313 int *PboxRM=new int[h2];
315 rc4keyperm(keyp, len, rp, Pbox, 16);
317 printf("len %d\n",len);
318 for(int i=0;i<len;i++) {
319 printf("%d \n",Pbox[i]);
322 rc4keyperm(RM2, h2, rp, PboxRM, h2);
324 for(int i=0;i<h2;i++){
330 double t=TimeStart();
335 encrypt(seq, seq2,len,RM1,Pbox,PboxRM,Sbox1,Sbox2,0);
339 cout<<"Time encrypt "<<time<<endl;
342 for(int i=0;i<oneD;i++) {
344 data_G[i]=seq2[oneD+i];
345 data_B[i]=seq2[2*oneD+i];
347 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
353 decrypt(seq2,seq,len,RM2,Pbox,PboxRM,Sbox1,Sbox2,0);
357 cout<<"Time decrypt "<<time<<endl;
360 for(int i=0;i<oneD;i++) {
362 data_G[i]=seq[oneD+i];
363 data_B[i]=seq[2*oneD+i];
365 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
376 cout << "Hello, World!" << endl;
386 uchar Secretkey[key_size];
388 uchar counter[key_size];
390 for(int i=0;i<key_size;i++) {
391 Secretkey[i]=lrand48()&0xFF;
392 counter[i]=lrand48()&0xFF;
395 Dynamickeygenerationnew(Secretkey, counter);