-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecryptOnly.c
More file actions
64 lines (49 loc) · 1.23 KB
/
DecryptOnly.c
File metadata and controls
64 lines (49 loc) · 1.23 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
#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 decode(mpz_t b, mpz_t n, int blockSize, FILE *fq);
int main(int argc, char *argv[]){
mpz_t n;
mpz_init_set_str(n, "200033699955714283345172521584008468989639", 10);
mpz_t b;
mpz_init_set_str(b, "54299300950841826990071853678997985400035", 10);
int blockSize = 3;
//File to be decrypted
FILE *fq = fopen("encrypted.txt", "r");
decode(b, n, blockSize, fq);
return 1;
}
int decode(mpz_t b, mpz_t n, int blockSize, FILE *fq){
FILE *fr = fopen("decrypted.txt", "w+");
assert(fr != NULL);
mpz_t y;
mpz_init(y);
mpz_t scanner;
mpz_init(scanner);
mpz_t mask;
unsigned long maskbits = pow(2, CHAR_BIT) -1;
mpz_init_set_ui(mask, maskbits);
mpz_t chunk;
//int done = FALSE;
int x[blockSize];
while(!feof(fq)){
mpz_init(chunk);
gmp_fscanf(fq, "%Zx", scanner);
mpz_powm(y, scanner, b, n);
for( int i = 0; i < blockSize; i++){
mpz_and(chunk, mask, y);
x[i] = (int)mpz_get_ui(chunk);
mpz_tdiv_q_2exp(y, y, (unsigned long) CHAR_BIT);
}
for( int i = (blockSize -1); i >= 0; i--){
fprintf(fr, "%c", x[i]);
}
mpz_clear(chunk);
}
return TRUE;
}