-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptOnly.c
More file actions
67 lines (52 loc) · 1.22 KB
/
EncryptOnly.c
File metadata and controls
67 lines (52 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <stdlib.h>
#include <assert.h>
#include <gmp.h>
#define TRUE 1
#define FALSE 0
int encode(mpz_t a, mpz_t n, int blockSize, FILE *fp);
int main(int argc, char *argv[]){
mpz_t n;
mpz_init_set_str(n, "200033699955714283345172521584008468989639", 10);
mpz_mul(n, p, q);
mpz_t a;
mpz_init_set_str(a, "10098768900987679000910003", 10);
int blockSize = 3;
//File to be encrypted
FILE *fp = fopen("SampleText.txt", "r");
encode(a, n, blockSize, fp);
return 1;
}
int encode(mpz_t a, mpz_t n, int blockSize, FILE *fp){
FILE *fq = fopen("encrypted.txt", "w+");
mpz_t block;
int x[blockSize];
int done = FALSE;
while(!done){
mpz_init(block);
for( int i = 0; i < blockSize; i++ ){
x[i] = fgetc(fp);
if( x[i] == EOF){
for( int j = i; j<blockSize; j++){
x[j] = 0;
done = TRUE;
}
break;
}
}
for( int i = 0; i < blockSize; i++ ){
mpz_add_ui(block, block, (unsigned long) x[i]);
if( i < (blockSize-1)){
mpz_mul_2exp(block, block, (unsigned long) CHAR_BIT);
}
}
mpz_powm(block, block, a, n);
mpz_out_str(fq, 16, block);
fprintf(fq, "\n");
mpz_clear(block);
}
fclose(fq);
return TRUE;
}