Current location - Quotes Website - Signature design - C language file encryption and decryption
C language file encryption and decryption
According to your needs, the previous code has been modified.

# Contains? & ltstdio.h & gt

# Contains? & ltstring.h & gt

# Contains? & ltstdlib.h & gt

# Contains? & lttime.h & gt

const? Not signed? int? Maximum key length? =? 1000;

int? Code (char? const? * data file? Charles? const? * key fill);

int? Decoding (char? const? * data file? Charles? const? * keyfile);

int? loadKey(char? const? * key file,? int? * key,? Not signed? int? Size);

int? saveKey(char? const? * key file,? int? * key,? Not signed? int? Size);

int? generateKey(int? * key,? Not signed? int? Size);

int? main(int? argc,? Charles? const? *argv[])

{

Charles? Data file []? =? ”encrypted . txt”;

Charles? keyfile[]? =? ”key . txt”;

int? retcode,? Choice,? Cycle? =? 1;

Charles? ch[5]? =? {'\0'};

while( 1)

{

printf(" 1。 ? Encryption. \ n ");

printf("2。 ? Decryption. \ n ");

printf("3。 ? Quit \ n ");

Printf ("Select? ( 1,2,3):");

fgets(ch,sizeof(ch),? stdin);

sscanf(ch,? "%d", & select);

Switch (selection)

{

Case? 1:?

retcode? =? Coding (data file, keyfile);

What if? (retcode? ! =? 0)? Printf ("Error, %d\0", retcode);

Break;

Case? 2:

retcode? =? Decoding (key file);

What if? (retcode? ! =? 0)? Printf ("Error, %d\0", retcode);

Break;

Case? 3:

Cycle? =? 0;

Break;

Default value:

Break;

}

What if? (0? ==? Cycle)? Break;

}

Return? 0;

}

int? generateKey(int? * key,? Not signed? int? Size)?

{

Charles? str[]= " 0 123456789 abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz,。 /; \ " ' & lt& gt? ";

size_t? str_len? =? sizeof(str)/sizeof(str[0]);

int? Me;

srand(time(NULL));

For what? (me? =? 0; ? Me? & lt? Size; ? ++i)

keys[i]? =? str[rand()? %? str _ len];

Return? 0;

}

int? loadKey(char? const? * key file,? int? * key,? Not signed? int? Size)

{

int? Me? =? 0;

Documents? * pfile

int? retcode? =? 0;

pfile? =? Fopen (key file, "R");

What if? (pfile? ==? NULL)? Return? - 1;

What time? (? ! feof(pfile)? )? {

What if? (me? & lt? Size)

fscanf(pfile," %d?" ,? & ampkeys[i++]);

other

Break;

}

fclose(pfile);

Return? Me;

}

int? saveKey(char? const? * key file,? int? * key,? Not signed? int? Size)?

{

Documents? * pfile

int? Me;

pfile? =? Fopen (key file, "w");

What if? (pfile? ==? NULL)? Return? - 1;

For (me? =? 0; ? Me? & lt? Size; ? ++i)? {

fprintf(pfile," %d?" ,? keys[I]);

}

fclose(pfile);

Return? 0;

}

int? Code (char? const? * data file? Charles? const? *keyfile)?

{

Charles? Original [MAX_KEY_LENGTH]? =? {'\0'};

Charles? Encrypted [MAX_KEY_LENGTH]? =? {'\0'};

int? Me? Size;

int? keys[MAX _ KEY _ LENGTH];

Documents? *pdatafile,? * pkeyfile

pkeyfile? =? Fopen (key file, "w");

What if? (NULL? ==? pkeyfile)? Return? - 1;

fclose(pkey file);

puts("? Input? Message: ");

Get (original);

Size? =? Strlen (original);

What if? (0? ! =? GenerateKey (key, size)? Return? -2;

What if? (0? ! =? SaveKey(keyfile, key? Size)? )? Return? -3;

pdatafile? =? Fopen (data file, "W");

What if? (NULL? ==? pdatafile)? Return? -4;

For what? (me? =? 0; ? Me? & lt? Size; ? ++i)? {

Encryption [i]? =? Original [me]? +? keys[I];

Fputc (encryption [i],? pdata file);

Fputc (encryption [i],? stdout);

}

printf(" \ n ");

fclose(pdatafile);

Return? 0;

}

int? Decoding (char? const? * data file? Charles? const? * key file)

{

Documents? *pdatafile,? * pkeyfile

int? keys[MAX_KEY_LENGTH]? =? {0};

Charles? Original [MAX_KEY_LENGTH]? =? {'\0'};

Charles? Encrypted [MAX_KEY_LENGTH]? =? {'\0'};

int? Me? Size;

pkeyfile? =? Fopen (key file, "R");

What if? (NULL? ==? pkeyfile)? Return? - 1;

fclose(pkey file);

pdatafile? =? Fopen (data file, "R");

What if? (NULL? ==? pdatafile)? Return? -2;

Fscanf(pdatafile, "%s", encrypted);

fclose(pdatafile);

Size? =? LoadKey(keyfile, key? Maximum key length);

What if? (size? & lt? 1)? Return? -3;

For what? (me? =? 0; ? Me? & lt? Strlen (encryption); ? ++i)? {

Original [me]? =? Encryption [I]- key [I];

fputc(original[i],? stdout);

}

printf(" \ n ");

Return? 0;

} Running result:

1. encryption.

2. Decryption.

Step 3 quit.

Select (1, 2, 3): 1

Enter message:

This is a test!

╓┐? ╞L? ╗ù|t▄╬╢╒è

1. encryption.

2. Decryption.

Step 3 quit.

Selection (1, 2,3): 2

This is a test!

1. encryption.

2. Decryption.

Step 3 quit.

Selection (1, 2,3): 3