From: couturie Date: Mon, 25 Sep 2017 07:20:12 +0000 (+0200) Subject: new X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/commitdiff_plain/b10cad1b1a4f87f61815936361592e99bffa1b2a?ds=inline new --- diff --git a/IDA/lena.png b/IDA/lena.png new file mode 100644 index 0000000..5806c58 Binary files /dev/null and b/IDA/lena.png differ diff --git a/IDA/test_mat.cpp b/IDA/test_mat.cpp new file mode 100644 index 0000000..254b03c --- /dev/null +++ b/IDA/test_mat.cpp @@ -0,0 +1,243 @@ +// g++ -std=c++11 -O4 -msse2 -msse3 -msse4 -fopenmp -O3 test_mat.cpp -o test_mat -I /home/couturie/tools/armadillo-6.200.5/include/ -lc -lm -lpthread -lgfortran -DMAX_STACK_ALLOC=2048 -Wall -m64 -DF_INTERFACE_GFORT -fPIC -DSMP_SERVER -DNO_WARMUP -DMAX_CPU_NUMBER=8 -DNO_AFFINITY -UCOMPLEX -DDOUBLE -I/home/couturie/tools/openblas/include -I/home/couturie/Downloads/OpenBLAS-0.2.15/ /home/couturie/tools/openblas/lib/libopenblas_haswellp-r0.2.15.a + + + +#include +#include +#include + +#include +#include +#include + + +#include // std::cout, std::fixed +#include +#include + +using namespace arma; +using namespace std; +int key_size=256; + +typedef unsigned char byte; + + +void rc4key(byte *key, byte *sc, int size_DK) { + + for(int i=0;i<256;i++) { + sc[i]=i; + } + + + byte j0 = 0; + for(int i0=0; i0<256; i0++) { + j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF; + byte tmp = sc[i0]; + sc[i0] = sc[j0 ]; + sc[j0] = tmp; + } +} + + +void prga(byte *sc, int ldata, byte *r) { + byte i0=0; + byte j0=0; + + for (int it=0; it readFile(int n, int k, int& sizeFile, int &lc) { + + ifstream stream("/home/couturie/ajeter/lena.png", ios::in | ios::binary | ios::ate); + sizeFile=stream.tellg(); + cout< contents2(n*lc,0); + vector contents((istreambuf_iterator(stream)), istreambuf_iterator()); + copy ( contents.begin(), contents.end(), contents2.begin() ); + + + + + Mat matData(&contents2[0],1,contents2.size()); + + cout << "file size: " << contents2.size() << endl; + + matData.reshape(n,lc); + cout< rm1(RM1,1,1,q*n*n/4); + rm1.reshape(n/2,n/2,q); + + cout<<"rm1"< IDAmat(n,n,q); + Cube invIDAmat(n,n,q); + +// Construction of the IDA matrices + for(int i=0;i sub(n,n); + sub.submat(0,0,n/2-1,n/2-1)=rm1.slice(i); + sub.submat(0,n/2,n/2-1,n-1)=rm1.slice(i)+eye>(n/2,n/2); + sub.submat(n/2,0,n-1,n/2-1)=rm1.slice(i)-eye>(n/2,n/2); + sub.submat(n/2,n/2,n-1,n-1)=rm1.slice(i); + +/* cout<<"sub "<::from(sub); + cout< sub2(n,n); + Mat rm2=conv_to>::from(rm1.slice(i)); + sub2.submat(0,0,n/2-1,n/2-1)=rm2; + sub2.submat(0,n/2,n/2-1,n-1)=-rm2-eye>(n/2,n/2); + sub2.submat(n/2,0,n-1,n/2-1)=-rm2+eye>(n/2,n/2); + sub2.submat(n/2,n/2,n-1,n-1)=rm2; + + cout<<"det"<::from(sub2); + cout< M=readFile(n,k,sizeFile,lc); + +// Ma data; +// data.load("/home/couturie/ajeter/lena.png"); + + /* Mat M=conv_to>::from(randi(k,l)); + M=abs(M); + M=M - floor(M/255)*255;*/ + +/* cout<<"M"< C=IDAmat.slice(0)*M; + M.save("M.bin",raw_binary); + Mat D=invIDAmat.slice(0)*C; + + + Mat D2=conv_to>::from(D); +// cout<<"D2"< d2=D2.row(0); + byte* vec=(byte*)d2.memptr(); + + FILE* pFile = fopen ("myfile.png", "wb"); + fwrite (vec , sizeof(byte), sizeFile, pFile); + fclose (pFile); +} diff --git a/IDA/test_mat2.cpp b/IDA/test_mat2.cpp new file mode 100644 index 0000000..e61ecac --- /dev/null +++ b/IDA/test_mat2.cpp @@ -0,0 +1,264 @@ +// g++ -std=c++11 -O4 -msse2 -msse3 -msse4 -fopenmp -O3 test_mat2.cpp -o test_mat2 -I /home/couturie/tools/armadillo-6.200.5/include/ -lc -lm -lpthread -lgfortran -DMAX_STACK_ALLOC=2048 -Wall -m64 -DF_INTERFACE_GFORT -fPIC -DSMP_SERVER -DNO_WARMUP -DMAX_CPU_NUMBER=8 -DNO_AFFINITY -UCOMPLEX -DDOUBLE -I/home/couturie/tools/openblas/include -I/home/couturie/Downloads/OpenBLAS-0.2.15/ /home/couturie/tools/openblas/lib/libopenblas_haswellp-r0.2.15.a + + + +#include +#include +#include + +#include +#include +#include + + +#include // std::cout, std::fixed +#include +#include + +using namespace arma; +using namespace std; +int key_size=256; + +typedef unsigned char byte; + + +void rc4key(byte *key, byte *sc, int size_DK) { + + for(int i=0;i<256;i++) { + sc[i]=i; + } + + + byte j0 = 0; + for(int i0=0; i0<256; i0++) { + j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF; + byte tmp = sc[i0]; + sc[i0] = sc[j0 ]; + sc[j0] = tmp; + } +} + + +void prga(byte *sc, int ldata, byte *r) { + byte i0=0; + byte j0=0; + + for (int it=0; it readFile(int n, int k, int& sizeFile, int &lc) { + + ifstream stream("/home/couturie/ajeter/lena.png", ios::in | ios::binary | ios::ate); +// ifstream stream("/home/couturie/ajeter/lena_small.png", ios::in | ios::binary | ios::ate); + sizeFile=stream.tellg(); + cout< contents2(n*lc,0); + vector contents((istreambuf_iterator(stream)), istreambuf_iterator()); + copy ( contents.begin(), contents.end(), contents2.begin() ); + + + + + Mat matData(&contents2[0],1,contents2.size()); + + cout << "file size: " << contents2.size() << endl; + + matData.reshape(n,lc); + cout<& IDAmat, Cube &invIDAmat, int q, int n, byte* DK) { + + byte Sbox[256]; + byte RM1[q*n*n/4]; + + + rc4key(DK, Sbox, 8); + prga(Sbox, q*n*n/4, RM1); + Cube rm1(RM1,1,1,q*n*n/4); + rm1.reshape(n/2,n/2,q); + + cout<<"rm1"< sub(n,n); + sub.submat(0,0,n/2-1,n/2-1)=rm1.slice(i); + sub.submat(0,n/2,n/2-1,n-1)=rm1.slice(i)+eye>(n/2,n/2); + sub.submat(n/2,0,n-1,n/2-1)=rm1.slice(i)-eye>(n/2,n/2); + sub.submat(n/2,n/2,n-1,n-1)=rm1.slice(i); + +/* cout<<"sub "<::from(sub); + cout< sub2(n,n); + Mat rm2=conv_to>::from(rm1.slice(i)); + sub2.submat(0,0,n/2-1,n/2-1)=rm2; + sub2.submat(0,n/2,n/2-1,n-1)=-rm2-eye>(n/2,n/2); + sub2.submat(n/2,0,n-1,n/2-1)=-rm2+eye>(n/2,n/2); + sub2.submat(n/2,n/2,n-1,n-1)=rm2; + + cout<<"det"<::from(sub2); + cout< IDAmat(n,n,q); + Cube invIDAmat(n,n,q); + buildFullRankIDA(IDAmat, invIDAmat, q, n, &DK[0]); + + + // rc4key(&DK[8], sc, 16); + + + + + + + arma_rng::set_seed(time(NULL)); + + + int lc; + int sizeFile; + Mat M=readFile(n,k,sizeFile,lc); + +// Ma data; +// data.load("/home/couturie/ajeter/lena.png"); + + /* Mat M=conv_to>::from(randi(k,l)); + M=abs(M); + M=M - floor(M/255)*255;*/ + +/* cout<<"M"< C=IDAmat.slice(0)*M; + M.save("M.bin",raw_binary); +// Mat D=invIDAmat.slice(0)*C; + + + mat A=conv_to::from(IDAmat.slice(0).submat(0,0,k-1,n-1)); + mat B=conv_to::from(C.submat(0,0,k-1,lc-1)); + + mat D=solve(A,B); + + + + Mat D2=conv_to>::from(D); +// cout<<"D2"< d2=D2.row(0); + byte* vec=(byte*)d2.memptr(); + + FILE* pFile = fopen ("myfile.png", "wb"); + fwrite (vec , sizeof(byte), sizeFile, pFile); + fclose (pFile); +}