--- /dev/null
+//gcc pixmap_io.c -c
+//g++ -O3 one_round_light_v2.cpp pixmap_io.o -o one_round_light_v2 -std=c++11
+
+#include <iostream>
+#include <list>
+#include<math.h>
+#include<stdlib.h>
+#include<stdio.h>
+#include <fstream>
+#include <sys/time.h>
+
+/*#include <cryptopp/hex.h>
+#include <cryptopp/sha.h>
+#include <cryptopp/osrng.h>
+#include <cryptopp/secblock.h>
+*/
+
+
+extern "C" {
+ int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
+ void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
+}
+
+
+//using namespace CryptoPP;
+using namespace std;
+
+
+int key_size=256;
+
+
+const int h=64;
+const int h2=h*h;
+
+
+
+typedef unsigned char uchar;
+
+
+double TimeStart()
+{
+ struct timeval tstart;
+ gettimeofday(&tstart,0);
+ return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
+}
+
+double TimeStop(double t)
+{
+ struct timeval tend;
+
+ gettimeofday(&tend,0);
+ t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
+ return (t);
+}
+
+
+
+
+
+
+void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
+
+ for(int i=0;i<size_tab;i++) {
+ inv_perm_tabs[tab[i]] = i;
+ }
+
+}
+
+
+void inverse_tables2(int *tab, int size_tab,int *inv_perm_tabs) {
+
+ for(int i=0;i<size_tab;i++) {
+ inv_perm_tabs[tab[i]] = i;
+ }
+
+}
+
+
+void rc4key(uchar *key, uchar *sc, int size_DK) {
+
+ for(int i=0;i<256;i++) {
+ sc[i]=i;
+ }
+
+
+ uchar j0 = 0;
+ for(int i0=0; i0<256; i0++) {
+ j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
+ uchar tmp = sc[i0];
+ sc[i0] = sc[j0 ];
+ sc[j0] = tmp;
+ }
+}
+
+
+
+void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
+
+ //sc=1:len;
+
+
+
+ for (int i=0;i<len;i++) {
+ sc[i]=i;
+ }
+ for (int it = 0; it < rp; it++) {
+ int j0 = 1;
+ for(int i0 = 0; i0<len; i0++) {
+ j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
+ int tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ }
+
+ }
+}
+
+void prga(uchar *sc, int ldata, uchar *r) {
+ uchar i0=0;
+ uchar j0=0;
+
+ for (int it=0; it<ldata; it++) {
+ i0 = ((i0+1)%255);
+ j0 = (j0 + sc[i0])&0xFF;
+ uchar tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ r[it]=sc[(sc[i0]+sc[j0])&0xFF];
+ }
+}
+
+
+
+
+
+
+
+void encrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, int debug) {
+
+
+ uchar *X=new uchar[h2];
+ uchar *Y=new uchar[h2];
+ uchar *fX=new uchar[h2];
+ uchar *gY=new uchar[h2];
+
+ for(int it=0;it<len;it++) {
+ int ind1=it*h2;
+ int ind2=Pbox[it]*h2;
+
+ uchar *p1=X;
+ uchar *p2=&seq[ind1];
+ for(int a=0;a<h2;a+=4) {
+ X[a]=seq[ind1+a];
+ X[a+1]=seq[ind1+a+1];
+ X[a+2]=seq[ind1+a+2];
+ X[a+3]=seq[ind1+a+3];
+
+ }
+
+
+
+ p1=Y;
+ p2=&seq[ind2];
+ for(int a=0;a<h2;a+=4) {
+ Y[a]=seq[ind2+a];
+ Y[a+1]=seq[ind2+a+1];
+ Y[a+2]=seq[ind2+a+2];
+ Y[a+3]=seq[ind2+a+3];
+
+ }
+ //memcpy(p1,p2,h2);
+
+ p1=fX;
+ p2=X;
+ for(int a=0;a<h2;a+=4){
+ fX[a]=Sbox1[X[a]];
+ fX[a+1]=Sbox1[X[a+1]];
+ fX[a+2]=Sbox1[X[a+2]];
+ fX[a+3]=Sbox1[X[a+3]];
+ }
+
+ for(int a=0;a<h2;a+=4){
+ gY[a]=Sbox2[Y[a]];
+ gY[a+1]=Sbox2[Y[a+1]];
+ gY[a+2]=Sbox2[Y[a+2]];
+ gY[a+3]=Sbox2[Y[a+3]];
+ }
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=fX[a]^RM1[a]^Y[a];
+ fX[a+1]=fX[a+1]^RM1[a+1]^Y[a+1];
+ fX[a+2]=fX[a+2]^RM1[a+2]^Y[a+2];
+ fX[a+3]=fX[a+3]^RM1[a+3]^Y[a+3];
+ }
+ for(int a=0;a<h2;a+=4){
+ gY[a]=gY[a]^RM3[a];
+ gY[a+1]=gY[a+1]^RM3[a+1];
+ gY[a+2]=gY[a+2]^RM3[a+2];
+ gY[a+3]=gY[a+3]^RM3[a+3];
+ }
+
+ for(int a=0;a<h2;a+=4) {
+ seq[ind1+a]=Sbox2[fX[a]];
+ seq[ind1+a+1]=Sbox2[fX[a+1]];
+ seq[ind1+a+2]=Sbox2[fX[a+3]];
+ seq[ind1+a+3]=Sbox2[fX[a+3]];
+ }
+ for(int a=0;a<h2;a+=4){
+ seq[ind2+a]=Sbox1[gY[a]];
+ seq[ind2+a+1]=Sbox1[gY[a+1]];
+ seq[ind2+a+2]=Sbox1[gY[a+2]];
+ seq[ind2+a+3]=Sbox1[gY[a+3]];
+ }
+ }
+
+
+}
+
+
+/*
+void decrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, int debug) {
+
+
+ uchar *fX=new uchar[h2];
+ uchar *gY=new uchar[h2];
+
+
+ uchar *Inv_Sbox1=new uchar[256];
+ inverse_tables(Sbox1,256,Inv_Sbox1);
+
+ uchar *Inv_Sbox2=new uchar[256];
+ inverse_tables(Sbox2,256,Inv_Sbox2);
+
+
+
+
+ for(int it=len-1;it>=0;it--) {
+ int ind1=it*h2;
+ int ind2=Pbox[it]*h2;
+
+
+
+ for(int a=0;a<h2;a++) {
+ fX[a]=seq[ind1+a];
+ }
+
+
+ for(int a=0;a<h2;a++) {
+ fX[a]=Inv_Sbox2[fX[a]];
+ }
+
+ for(int a=0;a<h2;a++) {
+ fX[a]=fX[a]^RM1[a];
+ }
+
+
+ for(int a=0;a<h2;a++) {
+ gY[a]=seq[ind2+a];
+ }
+
+ for(int a=0;a<h2;a++) {
+ gY[a]=Inv_Sbox1[gY[a]];
+ }
+ for(int a=0;a<h2;a++) {
+ gY[a]=gY[a]^RM3[a];
+ }
+
+// for(int a=0;a<h2;a++) {
+// gY[a]=Inv_Sbox2[gY[a]];
+// }
+
+ for(int a=0;a<h2;a++) {
+ fX[a]=fX[a]^gY[a];
+ }
+
+ for(int a=0;a<h2;a++) {
+ seq[ind1+a]=Inv_Sbox1[fX[a]];
+ }
+ for(int a=0;a<h2;a++) {
+ seq[ind2+a]=Inv_Sbox2[gY[a]];
+ }
+ }
+
+
+}
+
+
+*/
+
+
+void decrypt(uchar* seq,int len,uchar* RM1,uchar *RM2,uchar *RM3,int *Pbox, uchar *Sbox1, uchar *Sbox2, int debug) {
+
+
+ uchar *fX=new uchar[h2];
+ uchar *gY=new uchar[h2];
+
+
+ uchar *Inv_Sbox1=new uchar[256];
+ inverse_tables(Sbox1,256,Inv_Sbox1);
+
+ uchar *Inv_Sbox2=new uchar[256];
+ inverse_tables(Sbox2,256,Inv_Sbox2);
+
+/* int *Inv_Pbox=new int[len];
+ inverse_tables2(Pbox,len,Inv_Pbox);
+
+ for(int i=0;i<len;i++) {
+ cout<<Pbox[Inv_Pbox[i]]<<" ";
+ }
+ exit(0);
+*/
+
+
+
+ for(int it=len-1;it>=0;it--) {
+ int ind1=it*h2;
+ int ind2=Pbox[it]*h2;
+
+
+
+
+
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=Inv_Sbox2[seq[ind1+a]];
+ fX[a+1]=Inv_Sbox2[seq[ind1+a+1]];
+ fX[a+2]=Inv_Sbox2[seq[ind1+a+2]];
+ fX[a+3]=Inv_Sbox2[seq[ind1+a+3]];
+ }
+
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=fX[a]^RM1[a];
+ fX[a+1]=fX[a+1]^RM1[a+1];
+ fX[a+2]=fX[a+2]^RM1[a+2];
+ fX[a+3]=fX[a+3]^RM1[a+3];
+ }
+
+
+ for(int a=0;a<h2;a+=4) {
+ gY[a]=Inv_Sbox1[seq[ind2+a]];
+ gY[a+1]=Inv_Sbox1[seq[ind2+a+1]];
+ gY[a+2]=Inv_Sbox1[seq[ind2+a+2]];
+ gY[a+3]=Inv_Sbox1[seq[ind2+a+3]];
+ }
+ for(int a=0;a<h2;a+=4) {
+ gY[a]=Inv_Sbox2[gY[a]^RM3[a]];
+ gY[a+1]=Inv_Sbox2[gY[a+1]^RM3[a+1]];
+ gY[a+2]=Inv_Sbox2[gY[a+2]^RM3[a+2]];
+ gY[a+3]=Inv_Sbox2[gY[a+3]^RM3[a+3]];
+ }
+
+
+ for(int a=0;a<h2;a+=4) {
+ fX[a]=fX[a]^gY[a];
+ fX[a+1]=fX[a+1]^gY[a+1];
+ fX[a+2]=fX[a+2]^gY[a+2];
+ fX[a+3]=fX[a+2]^gY[a+3];
+ }
+
+ for(int a=0;a<h2;a+=4) {
+ seq[ind1+a]=Inv_Sbox1[fX[a]];
+ seq[ind1+a+1]=Inv_Sbox1[fX[a+1]];
+ seq[ind1+a+2]=Inv_Sbox1[fX[a+2]];
+ seq[ind1+a+3]=Inv_Sbox1[fX[a+3]];
+ }
+
+
+ for(int a=0;a<h2;a+=4) {
+ seq[ind2+a]=gY[a];
+ seq[ind2+a+1]=gY[a+1];
+ seq[ind2+a+2]=gY[a+2];
+ seq[ind2+a+3]=gY[a+3];
+ }
+ }
+
+
+}
+
+
+
+void Dynamickeygenerationnew(uchar *Secretkey, uchar *counter) {
+ int size = 64;
+ uchar DK[size];
+
+
+
+
+ int width;
+ int height;
+ uchar *data_R, *data_G, *data_B;
+ load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
+
+
+
+
+
+ int imsize=width*height*3;
+ uchar* seq= new uchar[imsize];
+
+ int oneD=width*height;
+ for(int i=0;i<oneD;i++) {
+ seq[i]=data_R[i];
+ seq[oneD+i]=data_G[i];
+ seq[2*oneD+i]=data_B[i];
+ }
+
+
+
+
+
+ int total_len=imsize;
+ int rp=1;
+ int len= total_len/h2;
+
+
+
+ uchar *mix=new uchar[256];
+
+
+
+
+ for (int i = 0; i < 256 ; i++) {
+// mix[i]=(int)secret_key.BytePtr()[i]^(int)mycounter.BytePtr()[i];
+ mix[i]=Secretkey[i]^counter[i];
+ }
+
+/* byte digest[64];
+ SHA512().CalculateDigest(digest, mix, 256);
+*/
+
+
+ cout<<"hash "<<endl;
+ for (int i = 0; i < 64 ; i++) {
+// DK[i]=digest[i];
+ DK[i]=mix[i];
+ }
+
+
+
+
+ uchar Sbox1[256];
+ rc4key(DK, Sbox1, 16);
+
+ uchar Sbox2[256];
+ rc4key(&DK[16], Sbox2, 16);
+
+
+
+ uchar sc[256];
+ rc4key(&DK[32], sc, 16);
+
+ uchar outd[2*(h * h)];
+ prga(sc, 2*(h * h), outd);
+
+
+ uchar RM1[h*h];
+ uchar RM2[h*h];
+ uchar RM3[h*h];
+ for(int i=0;i<h2;i++){
+ RM1[i]=outd[i];
+ RM2[i]=outd[i+h2];
+ RM3[i]=RM1[i]^RM2[i];
+ }
+
+
+
+
+
+
+ uchar keyp[16];
+ for (int i = 48; i < 64; i++)
+ keyp[i-48] = DK[i];
+
+ cout<<len<<endl;
+ int *Pbox=new int[len];
+
+ rc4keyperm(keyp, len, rp, Pbox, 16);
+
+
+
+
+ double time=0;
+ double t=TimeStart();
+
+ int i;
+ for(i=0;i<100;i++)
+ {
+ encrypt(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,0);
+ }
+
+ time+=TimeStop(t);
+ cout<<"Time encrypt "<<time<<endl;
+
+
+ for(int i=0;i<oneD;i++) {
+ data_R[i]=seq[i];
+ data_G[i]=seq[oneD+i];
+ data_B[i]=seq[2*oneD+i];
+ }
+ store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
+
+
+ time=0;
+ t=TimeStart();
+ for(i=0;i<100;i++) {
+ decrypt(seq,len,RM1,RM2,RM3,Pbox,Sbox1,Sbox2,0);
+ }
+
+ time+=TimeStop(t);
+ cout<<"Time decrypt "<<time<<endl;
+
+
+ for(int i=0;i<oneD;i++) {
+ data_R[i]=seq[i];
+ data_G[i]=seq[oneD+i];
+ data_B[i]=seq[2*oneD+i];
+ }
+ store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
+
+
+
+
+
+}
+
+
+
+int main() {
+ cout << "Hello, World!" << endl;
+
+
+
+
+
+ int seed=time(NULL);
+ cout<<seed<<endl;
+ srand48(seed);
+
+ uchar Secretkey[key_size];
+
+ uchar counter[key_size];
+
+ for(int i=0;i<key_size;i++) {
+ Secretkey[i]=lrand48()&0xFF;
+ counter[i]=lrand48()&0xFF;
+ }
+
+ Dynamickeygenerationnew(Secretkey, counter);
+
+ return 0;
+}