summaryrefslogtreecommitdiffstats
path: root/crypt.cpp
blob: 5f0d01d58a4032fdc68337e9ad946d77d0f33065 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * crypt.cpp
 *
 *
 */

#include "crypt.h"

#ifndef DEBUG
#define DEBUG
#endif

/* forward declaration of variables from crypt_args.h */
extern char *fname;
extern char *keyfname;
extern char *outfname;

struct rsakey_t
{
    long unsigned e, d, n;
};

/* parse pubkey xml file */
struct rsakey_t parse_key(FILE *file)
{
    struct rsakey_t key = { 0, 0, 0};

    /* used for line reading */
    size_t line_sz = 0;
    char *line = NULL;

    while (getline(&line, &line_sz, file) != -1)
    {
        sscanf(line, "\t<dvalue>%lu</dvalue>\n", &key.d);
        sscanf(line, "\t<evalue>%lu</evalue>\n", &key.e);
        sscanf(line, "\t<nvalue>%lu</nvalue>\n", &key.n);
    }

    if (line)
        free(line);

    return key;
}

int main(int argc, char **argv)
{
    if (crypt_args(argc, argv) == 0)
        cout << "read arguments successfully" << endl;

#ifdef DEBUG
    printf("debug: filename        = \"%s\"\n"
           "       key filename    = \"%s\"\n"
           "       result filename = \"%s\"\n",
           fname, keyfname, outfname);
#endif

    /* open file for reading and writing */
    FILE *fname_fl =  fopen(fname, "r");
    FILE *keyfname_fl = fopen(keyfname, "r");
    FILE *outfname_fl = fopen(outfname, "w");
    if (fname_fl == NULL) {
        fprintf(stderr, "fopen: \"%s\", %s\n", fname, strerror(errno));
        exit(EXIT_FAILURE);
    }
    if (keyfname_fl == NULL) {
        fprintf(stderr, "fopen: \"%s\", %s\n", keyfname, strerror(errno));
        exit(EXIT_FAILURE);
    }
    if (outfname_fl == NULL) {
        perror("fopen");
        exit(EXIT_FAILURE);
    }

    int mode = -1;
    struct rsakey_t key = parse_key(keyfname_fl);

    if (key.d == 0 && key.e && key.n)
    {
        cout << "public key (" << key.e << ", " << key.n
             << ") detected, will perform encryption" << endl;
        mode = 0;
    }
    else if (key.e == 0 && key.d && key.n)
    {
        cout << "private key (" << key.d << ", " << key.n
             << ") detected, will perform decryption" << endl;
        mode = 1;
    }
    else
    {
        fprintf(stderr, "could not determine mode (encryption/decryption)\n");
        exit(EXIT_FAILURE);
    }

    if (mode == 0) /* encrypt */
    {
        char *line = NULL;
        size_t len = 0;
        ssize_t read;

        unsigned int readin = 0;
        BigUnsigned calc = 0;
        string writeout;
        const char *outstr = NULL;
        while ((read = getline(&line, &len, fname_fl)) != -1)
        {
            sscanf(line, "%u\n", &readin);
            printf("read in: \"%u\"", readin);
            calc = modexp(BigUnsigned(readin), key.e, key.n);
            writeout = bigIntegerToString(calc);
            cout << ", encrypted: " << writeout << endl;
            outstr = writeout.c_str();
            fprintf(outfname_fl, "%s\n", outstr);
        }

        free(line);
    }
    else /* decrypt */
    {
        char *line = NULL;
        size_t len = 0;
        ssize_t read;

        char readin[100];
        BigUnsigned calc = 0;
        BigInteger tmpbig = 0;
        string writeout;
        string tmpstr;
        const char *outstr = NULL;
        while ((read = getline(&line, &len, fname_fl)) != -1)
        {
            sscanf(line, "%s\n", readin);
            printf("read in: \"%s\"", readin);
            string tmpstr(readin);
            tmpbig = stringToBigInteger(tmpstr);
            calc = modexp(tmpbig, key.d, key.n);
            writeout = bigIntegerToString(calc);
            cout << ", decrypted: " << writeout << endl;
            outstr = writeout.c_str();
            fprintf(outfname_fl, "%s\n", outstr);
        }

        free(line);
    }

    fclose(fname_fl);
    fclose(keyfname_fl);
    fclose(outfname_fl);

    return 0;
}