int main(int argc, char **argv) { if (argc < 3) { printf("Usage: %s inputfile outputfile [pubkeyfile]\n", argv[0]); exit(0); } char *ptxtfname = argv[1]; char *ctxtfname = argv[2]; char *pkeyfname; if (argc != 4) { pkeyfname = "id_rsa.pub"; } else { pkeyfname = argv[3]; } FILE *fptxt = fopen(ptxtfname, "r"); FILE *fctxt = fopen(ctxtfname, "w"); FILE *fpkey = fopen(pkeyfname, "r"); pubkey pkey; mpz_init(pkey.n); mpz_init(pkey.e); gmp_fscanf(fpkey, "%Zd", &(pkey.n)); gmp_fscanf(fpkey, "%Zd", &pkey.e); int c = getc(fptxt); int count = 0; mpz_t next; mpz_init(next); while (c != EOF) { encrypt_char(next, c, pkey); gmp_fprintf(fctxt, "%Zd\n", next); c = getc(fptxt); } }
void encrypt_file(char *file_in, char *file_out, char *key, int key_size) { int i; string input, output; char *out_content; //Précond. if(file_in == NULL || file_out == NULL || key == NULL){ fprintf(stderr, "[encrypt_file] Erreur dans les paramètres\n"); exit(EXIT_SUCCESS); } //Traitement input = readstring(file_in); if((out_content = (char *)malloc(input.length * sizeof(char))) == NULL){ perror("[encrypt_file] Erreur dans l'allocation de la chaîne de résultat"); exit(MEM_ERROR); } //Chiffrage des caractères du fichier d'entrée for(i = 0; i < input.length; i++){ out_content[i] = encrypt_char(input.content[i], key[i%key_size]); } output.content = out_content; output.length = input.length; writestring(file_out, output); //Nettoyage free(out_content); free(input.content); }
char * encipher(const char * key, char * plain_text) { char *buf = (char *) malloc(strlen(plain_text) + 1); char *p_buf = buf; const char *p_plain_text = plain_text, *p_key = key; size_t previous_cipher = 0; for(; *p_plain_text != '\0'; ++p_plain_text, ++p_buf) { if (is_passthrough(*p_plain_text)) { // for passthrough characters, nothing's changed; neither does p_key // increase *p_buf = *p_plain_text; continue; } previous_cipher = encrypt_char(*p_plain_text, *p_key, previous_cipher); *p_buf = char_set[previous_cipher]; ++p_key; } *p_buf = '\0'; return buf; }