Реализую алгоритм шифрования ГОСТ. В общем, бьюсь над следующей проблемой. После выполнения шфрования, файл раздувается в десятки раз. Вроде прогоняю по шагам уже, и всё равно не пойму. Может не туда смотрю?
Код | #include "stdafx.h" #include <math.h> #include <stdlib.h> #include <stdio.h> #include <iostream>
void main() { FILE *file; FILE *stream;
int Larray[4]; int Rarray[4]; int key[32]={1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,8,7,6,5,4,3,2,1}; int sb[8][16]= {4,10,9,2,13,8,0,14,6,11,1,12,7,15,5,3, 14,11,4,12,6,13,15,10,2,3,8,1,0,7,5,9, 5,8,1,13,10,3,4,2,14,15,12,7,6,0,9,11, 7,13,10,1,0,8,9,15,14,4,6,12,11,2,5,3, 6,12,7,1,5,15,13,8,4,10,9,14,0,3,11,2, 4,11,10,0,7,2,1,13,3,6,8,5,9,12,15,14, 13,11,4,1,3,15,5,9,0,10,14,7,6,8,2,12, 1,15,13,0,5,7,10,4,9,2,3,14,6,11,8,12}; int piece[8]; int countL=0; int countR=0; int j; unsigned long int L=0; unsigned long int R=0; unsigned long int tmpLR=0; unsigned long int tmpL=0; unsigned long int tmp=0;
file=fopen("123.png","rb"); fseek(file,0,SEEK_END); L=ftell(file); fclose(file); if(L%8!=0) { R=L; for(int i=0;i<8;i++) { R++; if(R%8==0) { R=R-L; file=fopen("123.png","ab"); for(int a=0;a<R;a++) { fputc(R,file); } fclose(file); L=0; R=0;
break; } } }
file=fopen("123.png","rb"); stream=fopen("out","ab"); while((tmp=fgetc(file))!=EOF) { if(countL<4) { Larray[countL]=tmp; countL++; } else
if(countL>3&&countL<8) { Rarray[countR]=tmp; countR++; countL++; } if(countL==4) { j=3; tmp=0; L=0;
for(int i=0; i<=3; i++) { tmp=Larray[i]; tmpLR=tmp<<(8*j); L=L^tmpLR; j--; }
tmpLR=0; }
if(countL==8) { j=3; tmp=0; R=0;
for(int i=0; i<=3; i++) { tmp=Rarray[i]; tmpLR=tmp<<(8*j); R=R^tmpLR; j--; }
tmpLR=0; } if(countL==8) { for(int i=0;i<32;i++) { int a=7; unsigned long int pieceMask=4026531840; unsigned long int pieceTmp=0; if(i==0) { tmpL=L; } else { tmpL=R; }
R=(R+key[i])%4294967296; for(j=0;j<8;j++) { pieceTmp=R&pieceMask; pieceTmp=pieceTmp>>4*a; a--; piece[j]=pieceTmp; pieceMask=pieceMask>>4; }
for(a=0;a<8;a++) { piece[a]=sb[a][piece[a]]; }
j=7; tmp=0; R=0;
for(a=0;a<8;a++) { tmp=piece[a]; tmpLR=tmp<<(4*j); R=R^tmpLR; j--; } tmpLR=0; R=(R<<11)|(R>>21); R=tmpL^R; } j=3; tmp=0; unsigned long int mask=4278190080;
for(int i=0;i<4;i++) { tmp=L&mask; mask=mask>>8; tmp=tmp>>(8*j); j--; fputc(tmp,stream); }
j=3; tmp=0; mask=4278190080;
for(int i=0;i<4;i++) { tmp=R&mask; mask=mask>>8; tmp=tmp>>(8*j); j--; fputc(tmp,stream); } } } fclose(stream); fclose(file); printf("All done!"); getchar(); }
|
Подазрение, всё же, вызывают вот эти строки:
Код | R=(R<<11)|(R>>21); R=tmpL^R;
|
Из-за сдвига влево может в старшие биты записывается инфа, но из-за типа переменной, она должна обрезаться.
В остальном отдельные блоки вроде тестировал всё в норме. Может кто занимался этим вопросом? Подскажите плз. Прошу прощения если пишу криво, пишу так чтобы потом сам разобрался что написал=) Спасибо за понимание и подсказки. |