Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, char **argv)
- {
- FILE *infile;
- FILE *outfile;
- char ch = '\0';
- char newch = '\0';
- int x = 0;
- int pwsMask = 0;
- int Magic = 1163;
- int r = 0;
- if(argc != 5){
- printf("The syntax of the command is incorrect.\n");
- return 0;
- }
- else{
- if(argv[3][1] != 'p'){
- printf("You need to include a password.\n");
- return 0;
- }
- //Start reading input file name.
- infile = fopen(argv[1],"rb");
- if(infile == NULL){
- printf("Unable to open input file name.\n\%s\n",argv[1]);
- return 0;
- }
- //See if we can write output file
- outfile = fopen(argv[2],"wb");
- if(outfile == NULL){
- printf("Unable to write output file name.\n\%s\n",argv[2]);
- return 0;
- }
- //Build a sum of all the chars in the password.
- while(x < strlen(argv[4])){
- pwsMask+= (int)argv[4][x];
- x++;
- }
- //Mask pwsMask with Magic and multiply by the length of the password.
- pwsMask = (pwsMask ^ Magic) * strlen(argv[4]);
- //Reset x
- x = 0;
- //Start reading and encoding the file data.
- while(!feof(infile)){
- //Read input char
- ch = fgetc(infile);
- //If not at end of the input file start encrypting.
- if(!feof(infile)){
- //Get random number from pwsMask and MOD by 255
- r = rand() * pwsMask % 0xff;
- //XOR ch with r
- newch = (ch ^ r);
- //Write to the output file name.
- fputc(newch,outfile);
- //INC x
- x++;
- }
- }
- //Close both file handles
- fclose(outfile);
- fclose(infile);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement