SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdio.h> | |
2 | #include <stdlib.h> | |
3 | #include <time.h> | |
4 | #include <string.h> | |
5 | ||
6 | ||
7 | int generate_mask(int len_c,unsigned char *mask) | |
8 | { | |
9 | srand (time(NULL)); | |
10 | int i; | |
11 | for(i=0;i<len_c;++i) | |
12 | mask[i]=rand()% 255 + 1; // randomn key between 1 and 255, extented ascii | |
13 | ||
14 | mask[len_c]='\0'; | |
15 | ||
16 | return 0; | |
17 | } | |
18 | ||
19 | ||
20 | int generate_encryption(unsigned char *cipher,unsigned char *mask,unsigned char *crypted,int len_c) | |
21 | { | |
22 | ||
23 | int i; | |
24 | for(i=0;i<len_c;++i) | |
25 | { | |
26 | if( (mask[i]+cipher[i]) > 255 ) // is the result superior to the table ? | |
27 | crypted[i]=(mask[i]+cipher[i]) % 255; // if yes, we make a modulo | |
28 | else | |
29 | crypted[i]=mask[i]+cipher[i]; | |
30 | } | |
31 | ||
32 | crypted[len_c]='\0'; | |
33 | ||
34 | return 0; | |
35 | } | |
36 | ||
37 | int main(int argc, char *argv[]) | |
38 | { | |
39 | if(argc == 2) | |
40 | { | |
41 | int len_c=strlen(argv[1]); | |
42 | unsigned char *mask=(unsigned char *)malloc(sizeof(char)*len_c+1); | |
43 | unsigned char *crypted=(unsigned char *)malloc(sizeof(char)*len_c+1); | |
44 | ||
45 | ||
46 | generate_mask(len_c,mask); | |
47 | ||
48 | generate_encryption((unsigned char *)argv[1],mask,crypted,len_c); | |
49 | ||
50 | printf("Message: %s\n Mask: %s\n Crypted: %s\n",argv[1],mask,crypted); | |
51 | free(mask); | |
52 | free(crypted); | |
53 | } | |
54 | ||
55 | else | |
56 | fprintf(stderr,"Usage: ./<executable> <text to encrypt>"); |