Основы криптографии

Дипломная работа - Компьютеры, программирование

Другие дипломы по предмету Компьютеры, программирование



^= f(n1+key[7]);

n1 ^= f(n2+key[6]);

n2 ^= f(n1+key[5]);

n1 ^= f(n2+key[4]);

n2 ^= f(n1+key[3]);

n1 ^= f(n2+key[2]);

n2 ^= f(n1+key[1]);

n1 ^= f(n2+key[0]);

n2 ^= f(n1+key[7]);

n1 ^= f(n2+key[6]);

n2 ^= f(n1+key[5]);

n1 ^= f(n2+key[4]);

n2 ^= f(n1+key[3]);

n1 ^= f(n2+key[2]);

n2 ^= f(n1+key[1]);

n1 ^= f(n2+key[0]);

n2 ^= f(n1+key[7]);

n1 ^= f(n2+key[6]);

n2 ^= f(n1+key[5]);

n1 ^= f(n2+key[4]);

n2 ^= f(n1+key[3]);

n1 ^= f(n2+key[2]);

n2 ^= f(n1+key[1]);

n1 ^= f(n2+key[0]);

out[0] = n2;

out[1] = n1;

}

/*

* The GOST "Output feedback" standard. It seems closer morally

* to the counter feedback mode some people have proposed for DES.

* The avoidance of the short cycles that are possible in OFB seems

* like a Good Thing.

*

* Calling it the stream mode makes more sense.

*

* The IV is encrypted with the key to produce the initial counter value.

* Then, for each output block, a constant is added, modulo 2^32-1

* (0 is represented as all-ones, not all-zeros), to each half of

* the counter, and the counter is encrypted to produce the value

* to XOR with the output.

*

* Len is the number of blocks. Sub-block encryption is

* left as an exercise for the user. Remember that the

* standard defines everything in a little-endian manner,

* so you want to use the low bit of gamma[0] first.

*

* OFB is, of course, self-inverse, so there is only one function.

*/

/* The constants for addition */

#define C1 0x01010104

#define C2 0x01010101

(word32 const *in, word32 *out, int len,

word32 const iv[2], word32 const key[8])

{

word32 temp[2]; /* Counter */

word32 gamma[2]; /* Output XOR value */

/* Compute starting value for counter */

gostcrypt(iv, temp, key);

while (len--) {

temp[0] += C2;

if (temp[0] < C2) /* Wrap modulo 2^32? */

temp[0]++; /* Make it modulo 2^32-1 */

temp[1] += C1;

if (temp[1] < C1) /* Wrap modulo 2^32? */

temp[1]++; /* Make it modulo 2^32-1 */

gostcrypt(temp, gamma, key);

*out++ = *in++ ^ gamma[0];

*out++ = *in++ ^ gamma[1];

}

}

/*

* The CFB mode is just what you'd expect. Each block of ciphertext y[] is

* derived from the input x[] by the following pseudocode:

* y[i] = x[i] ^ gostcrypt(y[i-1])

* x[i] = y[i] ^ gostcrypt(y[i-1])

* Where y[-1] is the IV.

*

* The IV is modified in place. Again, len is in *blocks*.

*/

(word32 const *in, word32 *out, int len,

word32 iv[2], word32 const key[8])

{

while (len--) {

gostcrypt(iv, iv, key);

iv[0] = *out++ ^= iv[0];

iv[1] = *out++ ^= iv[1];

}

}

(word32 const *in, word32 *out, int len,

word32 iv[2], word32 const key[8])

{

word32 t;

while (len--) {

gostcrypt(iv, iv, key);

t = *out;

*out++ ^= iv[0];

iv[0] = t;

t = *out;

*out++ ^= iv[1];

iv[1] = t;

}

}

/*

* The message suthetication code uses only 16 of the 32 rounds.

* There *is* a swap after the 16th round.

* The last block should be padded to 64 bits with zeros.

* len is the number of *blocks* in the input.

*/(word32 const *in, int len, word32 out[2], word32 const key[8])

{

register word32 n1, n2; /* As named in the GOST */

n1 = 0;

n2 = 0;

while (len--) {

n1 ^= *in++;

n2 = *in++;

/* Instead of swapping halves, swap names each round */

n2 ^= f(n1+key[0]);

n1 ^= f(n2+key[1]);

n2 ^= f(n1+key[2]);

n1 ^= f(n2+key[3]);

n2 ^= f(n1+key[4]);

n1 ^= f(n2+key[5]);

n2 ^= f(n1+key[6]);

n1 ^= f(n2+key[7]);

n2 ^= f(n1+key[0]);

n1 ^= f(n2+key[1]);

n2 ^= f(n1+key[2]);

n1 ^= f(n2+key[3]);

n2 ^= f(n1+key[4]);

n1 ^= f(n2+key[5]);

n2 ^= f(n1+key[6]);

n1 ^= f(n2+key[7]);

}

out[0] = n1;

out[1] = n2;

}

#ifdef TEST

#include

#include

/* Designed to cope with 15-bit rand() implementations */

#define RAND32 ((word32)rand() << 17 ^ (word32)rand() << 9 ^ rand())

(void)

{

word32 key[8];

word32 plain[2];

word32 cipher[2];

int i, j;

kboxinit();

printf("GOST 21847-89 test driver.\n");

for (i = 0; i < 1000; i++) {

for (j = 0; j < 8; j++)

key[j] = RAND32;

plain[0] = RAND32;

plain[1] = RAND32;

printf("\r", i);

fflush(stdout);

gostcrypt(plain, cipher, key);

for (j = 0; j < 99; j++)

gostcrypt(cipher, cipher, key);

for (j = 0; j < 100; j++)

gostdecrypt(cipher, cipher, key);

if (plain[0] != cipher[0] || plain[1] != cipher[1]) {

fprintf(stderr, "\nError! i = %d\n", i);

return 1;

}

}

printf("All tests passed.\n");

return 0;

}

#endif /* TEST */

Простое XOR

//**************************************

//INCLUDE files for :crypt.c

//**************************************

#include

#include

#include

#include

//**************************************

// Name: crypt.c

// Description:Encrypts file and outputs it to a file or stdout

// By:

//

//

// Inputs:crypt1.exe

//

//**************************************

#define cypherbits 256 /* encryption strength in bits */

#define cypher cypherbits/(sizeof(int)*8)seed[cypher];modseed() {x;(x=1;x<cypher;x++)seed[x]=seed[x]*0x81772012+seed[x]+0x49122035+seed[x+1];(x=1;x<cypher;x++)seed[0]=seed[0]^seed[x];

}seedsum() {n,x;=0x80379251;(x=0;x8)&255)^(n&255));

}strequ(char *s1,char *s2) {p;=0;((s1[p]==s2[p])&&(s1[p]!=0)&&(s2[p]!=0))p++;(s1[p]==s2[p])return(1); else return(0);

}main(int argc,char *argv[]) {banner[]="\x43\x6f\x64\x65\x64\x20\x62\x79\x20\x70\x61\x69"

"\x6e\x74\x20\x6f\x66\x20\x44\x61\x6c\x4e"

"\x75\x74\x20\x28\x70\x61\x69\x6e\x74\x40\x6c\x65\x65\x74\x2e\x63\x6f\x6d\x29";buf[2048];p,r,l,i,t,s,x;b,c,pct,lpct;*infile=NULL,*outfile=NULL;(stderr, "%s\n", banner);(argc!=4){(stderr,"use: %s \n",argv[0]);(-1);

}(strequ(argv[1],"stdin"))infile=stdin; else((infile=fopen(argv[1],"r"))==NULL){(stderr,"failed to open %s\n",argv[1]);(-1);

}(strequ(argv[2],"stdout"))outfile=stdout; else((outfile=fopen(argv[2],"w"))==NULL){(stderr,"failed to create %s\n",argv[2]);(-1);

}(infile!=stdin) {(infile,0,SEEK_END);=ftell(infile);(infile);

} else l=0;=l;=0;=0;(l<1)fprintf(stderr,"Encrypting data.. (%d bit cypher)\n",cypher*sizeof(int)*8);fprintf(stderr,"Encrypting %d bytes.. (%d bit cypher)\n",l,cypher*sizeof(int)*8);(seed,sizeof(seed));;=0;(argv[3][p]!=0){;[0]=seed[0]+argv[3][p];;++;

}=0;(l>0){('[',stderr);=(l/sizeof(buf));(l-x*sizeof(buf)!=0)x+=1;(x>38)x=38;(p=0;p<x;p++) fputc(32,stderr);(']',stderr);(13,stderr);('[',stderr);(stderr);

}=1;(c){=fread(&buf,1,sizeof(buf),infile);(r>0) {

t+=r;

if(l>0){

lpct=pct;

pct=t/(l/x);

if(pct>lpct) {

fputc(88+32*i,stderr);

fflush(stderr);

i=1-i;

}

} else {

fputc(88+32*i,stderr);

fflush(stderr);

i=1-i;

}

p=0;

while(p<r) {

modseed();

buf[p]=buf[p]^seedsum();

p++;

}

if(fwrite(&buf,1,r,outfile)!=r) {

fprintf(stderr,"\nerror writing data\n");

exit(-1);

}

} else c=0;

}(l>0)fputc(']',stderr);(stderr,"\nDone. Wrote %d bytes.\n",t);

}

FileCoding

Данная программа осуществляет простое (без сжатия) кодирование любого файла по алгоритму ГОСТ. ГОСТ был выбрано по нескольким причинам- одна из которых, возможное принятие данного алгоритма шифрования в качестве основного, Соединенными Штатами Америки, а следовательно (так как он уже ?/p>