void main(void){ int i =0; int j =0; int bits = 128; paillier_prvkey_t* prvkey; paillier_pubkey_t* pubkey; char* my_str = "cookies"; char* my_str2; char* my_pubkey; char* my_prvkey; paillier_plaintext_t* my_plain_txt; paillier_plaintext_t* my_plain_txt2; paillier_ciphertext_t* my_enc_txt; void* my_enc_export[128]; my_plain_txt = paillier_plaintext_from_str(my_str); paillier_keygen( bits, &pubkey, &prvkey, &paillier_get_rand_devurandom); my_enc_txt = paillier_enc(NULL, pubkey, my_plain_txt, &paillier_get_rand_devurandom); my_plain_txt2 = paillier_dec(NULL, pubkey, prvkey, my_enc_txt); my_str2 = paillier_plaintext_to_str(my_plain_txt2); my_pubkey = paillier_pubkey_to_hex( pubkey ); my_prvkey = paillier_prvkey_to_hex( prvkey ); printf("my pub key is : %s\n", my_pubkey); printf("my prv key is : %s\n", my_prvkey); return; }
int main(int argc, char **argv) { FILE *fpairing, *ftag, *fdata, *fresult, *fplain, *fkey, *fcipher, *fpub; pairing_t pairing; paillier_pubkey_t *pub; paillier_prvkey_t *priv; element_t g, h, u, sig1, sig2, sig3, temp_pow, m, g1, g2; element_t public_key, tag, tag_prod; element_t secret_key; paillier_get_rand_t get_rand; paillier_ciphertext_t *cipher1, *cipher2; paillier_plaintext_t *plain1, *plain2; mpz_t pub_n, a, b, data2, nsquare; int count = 0, val=5; pairing_init_set_str(pairing, param_str); //mpz_init_set_str(data_sum, "0", 10); plain1 = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); plain2 = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); cipher1 = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); cipher2 = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); //pbc_demo_pairing_init(pairing, argc, argv); element_init_G1(g1, pairing); element_init_G1(g2, pairing); element_init_G2(g, pairing); element_init_G2(public_key, pairing); element_init_G1(u, pairing); element_init_G1(temp_pow, pairing); element_init_G2(public_key, pairing); element_init_G1(h, pairing); element_init_G1(m, pairing); element_init_G1(sig1, pairing); element_init_G1(sig2, pairing); element_init_G1(sig3, pairing); element_init_G1(tag, pairing); element_init_G1(tag_prod, pairing); element_init_Zr(secret_key, pairing); // mpz_init(pub_n); char *len; mpz_init(a); mpz_init(b); mpz_init(data2); printf("Short signature test\n"); len = (char *)malloc(2048*sizeof(char)); if((fpub = fopen("pub.txt", "r"))) { pub = (paillier_pubkey_t*) malloc(sizeof(paillier_pubkey_t)); priv = (paillier_prvkey_t*) malloc(sizeof(paillier_prvkey_t)); mpz_init(pub->n_squared); mpz_init(pub->n); fgets(len, 1000, fpub); mpz_init_set_str(pub->p, len, 10); fgets(len, 1000, fpub); mpz_init_set_str(pub->q, len, 10); fgets(len, 1000, fpub); mpz_init_set_str(pub->n_plusone, len, 10); //printf("value of nplusone : \n"); //mpz_out_str(stdout, 10, pub->n_plusone); paillier_keygen(&pub, &priv, get_rand, 0); pub->bits = mpz_sizeinbase(pub->n, 2); fclose(fpub); } //setting already known pairing parameters if((fpairing = fopen("pairing.txt", "r"))) { fgets(len, 1000, fpairing); //printf("\n %s\n", len); element_set_str(g, len, 10); //element_printf(" g = %B\n", g); fgets(len, 1000, fpairing); //printf("\n %s\n", len); element_set_str(u, len, 10); //element_printf("\n u= %B\n", u); fgets(len, 1000, fpairing); element_set_str(secret_key, len, 10); //element_printf(" secretkey %B\n",secret_key); fgets(len, 1000, fpairing); element_set_str(public_key, len, 10); //element_printf(" publickey %B\n", public_key); fgets(len, 1000, fpairing); element_set_str(h, len, 10); //element_printf(" \nh = %B\n", h); fgets(len, 1000, fpairing); mpz_init_set_str(pub_n, len, 10); //printf("\n n = "); //mpz_out_str(stdout, 10, pub_n); fclose(fpairing); } element_set1(tag_prod); ftag = fopen("./tag/output5.txt", "r"); fgets(len, 1000, ftag); element_set_str(g1, len, 10); element_printf("\ng1 = %B\n", g1); fclose(ftag); ftag = fopen("./tag/output6.txt", "r"); fgets(len, 1000, ftag); element_set_str(g2, len, 10); element_printf("\ng2 = %B\n", g2); fclose(ftag); fplain = fopen("./split/output5.txt", "r"); fgets(len, 1000, fplain); // printf("\nlen %s", len); mpz_set_str(a, len, 10); //element_printf("\na = %Zd\n", a); fclose(fplain); fplain = fopen("./split/output6.txt", "r"); fgets(len, 1000, fplain); mpz_set_str(b, len, 10); fcipher = fopen("./cipher/copy1/output5.txt", "r"); fgets(len, 1000, fcipher); mpz_init_set_str(cipher1->c, len, 10); fclose(fcipher); fcipher = fopen("./cipher/copy1/output6.txt", "r"); fgets(len, 1000, fcipher); mpz_init_set_str(cipher2->c, len, 10); fclose(fcipher); paillier_mul(pub, cipher2, cipher2, cipher1); plain1 = paillier_dec(plain1, pub, priv, cipher2); //tag mpz_t an; mpz_init(an); mpz_init(nsquare); // mpz_mul(an, a, pub_n); mpz_mul(nsquare, pub_n, pub_n); element_pow_mpz(temp_pow,u, plain1->m); element_mul(temp_pow, temp_pow, h); element_pow_zn(sig1, temp_pow, secret_key); element_printf("\n signature of plain = %B\n", sig1); //mpz_mul(an, b, pub_n); // mpz_mul(nsquare, pub_n, pub_n); element_pow_mpz(temp_pow,u, b); element_mul(temp_pow, temp_pow, h); element_pow_zn(sig2, temp_pow, secret_key); element_printf("\n signature of b = %B\n", sig2); //element_printf("\nb = %Zd\n", b); fclose(fplain); mpz_add(a, a, b); // mpz_mod(a, a, pub_n); // mpz_mul(a, a, pub_n); // mpz_mod(a, a, nsquare); count = 2; element_pow_mpz(temp_pow,u, a); mpz_set_ui(data2, count); // itoa(count, len, 10);+ //element_printf(" \nh = %B\n", h); element_pow_mpz(h, h, data2); element_mul(temp_pow, temp_pow, h); //element_printf("\n h. u^bN = %B\n", temp_pow); element_pow_zn(sig3, temp_pow, secret_key); element_printf("\n sig 3 %B\n", sig3); element_mul(g2, g2, g1); element_printf("\n Direct Product %B\n", g2); element_mul(sig2, sig1, sig2); element_printf("\n Direct Product %B\n", sig2); return 0; }
int main (int argc, char** argv) { paillier_pubkey_t* pkey; paillier_prvkey_t* skey; paillier_keygen(128,&pkey,&skey,&paillier_get_rand_devrandom); void *context = zmq_ctx_new (); struct opts options; parse_options(argc,argv, &options); if(options.size <= 0 || options.scale <= 0 || !options.fileset){ fprintf(stderr,"Size and scale must be greater than 0 and file must be set\n"); exit(EXIT_FAILURE); } struct classify_data data; data.pub = pkey; data.prv = skey; data.maxcol = options.size; data.scale_factor = options.scale; data.texts = (paillier_plaintext_t**)malloc(options.size*sizeof(paillier_plaintext_t*)); data.col = 0; data.correct = 0; data.total = 0; init_rand(data.rand,&paillier_get_rand_devurandom,pkey->bits / 8 + 1); // Socket to talk to server gmp_printf("n: %Zd, lambda: %Zd\n",pkey->n,skey->lambda); void *requester = zmq_socket (context, ZMQ_REQ); zmq_connect (requester, "ipc:///tmp/karma"); char* pubkeyhex = paillier_pubkey_to_hex(pkey); s_send(requester,pubkeyhex); char* recv = s_recv(requester); free(recv); free(pubkeyhex); data.socket = requester; char* file = options.file; FILE* fp; struct csv_parser p; char buf[1024]; size_t bytes_read; if(csv_init(&p,0)) { fprintf(stderr, "Failed to initialize parser\n"); exit(EXIT_FAILURE); } fp = fopen(file,"rb"); if(!fp){ fprintf(stderr,"Failed to open classify file %s\n",strerror(errno)); exit(EXIT_FAILURE); } while ((bytes_read=fread(buf,1,1024,fp)) > 0){ if(!csv_parse(&p,buf,bytes_read,field_parsed,row_parsed,&data)){ fprintf(stderr, "Failed to parse file: %s\n",csv_strerror(csv_error(&p))); } } csv_fini(&p,field_parsed,row_parsed,&data); //fini took care of freeing the plaintexts csv_free(&p); free(data.texts); gmp_randclear(data.rand); printf("Correct(%i)/Total(%i) = %f\n",data.correct,data.total,data.correct/(data.total+0.0)); sleep (2); zmq_close (requester); zmq_ctx_destroy (context); return 0; }
void test_comparison() { Log::i("(Test) # Test: Comparison"); // questions Log::i("(Test) - Questions"); std::set<Question> questions; for (int i = 0; i < Helper::GenerateRandom(10) + 2; i++) { Question q("Question #" + std::to_string(i)); questions.insert(q); } test_generic(questions); // keys Log::i("(Test) - Keys"); std::set<CKeyID> keys; for (int i = 0; i < Helper::GenerateRandom(10) + 2; i++) { SignKeyPair skp; SignKeyStore::genNewSignKeyPair(static_cast<Role>(Helper::GenerateRandom(4)), skp); keys.insert(skp.second.GetID()); SignKeyStore::removeSignKeyPair(skp.second.GetID()); } test_generic(keys); // encrypted ballots Log::i("(Test) - Encrypted Ballots"); std::set<EncryptedBallot> eballots; std::set<TalliedBallots> tballots; int nTrustees = 4; paillier_pubkey_t* publicKey = NULL; paillier_partialkey_t** privateKeys = NULL; paillier_keygen(256, nTrustees, nTrustees, &publicKey, &privateKeys, paillier_get_rand_devurandom); for (int i = 0; i < Helper::GenerateRandom(2) + 2; i++) { PLAINTEXT_SELECTION choice = PLAINTEXT_SELECTION::FIRST; if (Helper::GenerateRandom() > 0.5) choice = PLAINTEXT_SELECTION::SECOND; paillier_ciphertext_proof_t* cipher = paillier_enc_proof(publicKey, choice, paillier_get_rand_devurandom, NULL); EncryptedBallot ballot; ballot.questionID = Helper::GenerateRandom160(); ballot.answer = cipher; eballots.insert(ballot); int j = Helper::GenerateRandom(nTrustees - 1); paillier_partialdecryption_proof_t* proof = paillier_dec_proof(publicKey, privateKeys[j], cipher, paillier_get_rand_devurandom, NULL); TalliedBallots ballots; ballots.questionID = Helper::GenerateRandom160(); ballots.answers = proof; tballots.insert(ballots); } test_generic(eballots); Log::i("(Test) - Tallied Ballots"); test_generic(tballots); paillier_freepubkey(publicKey); paillier_freepartkeysarray(privateKeys, nTrustees); BOOST_FOREACH(EncryptedBallot b, eballots) paillier_freeciphertextproof(b.answer); BOOST_FOREACH(TalliedBallots b, tballots) paillier_freepartdecryptionproof(b.answers); }
int main(int argc, char *argv[]) { DIR *d; FILE *fpub, *fciph, *fplain, *fprod, *fkey; paillier_pubkey_t *pub; paillier_plaintext_t *plain, *plain_sum, *plain_temp; paillier_prvkey_t *priv; paillier_get_rand_t get_rand; paillier_ciphertext_t *cipher, *cipher_prod, *cipher_total; int count = 0, count1=0, val = 163000, no_of_copies = 10; long int key_int, key_int1, key_rnd; struct dirent *dir; char* len; mpz_t rndKey, randNum, rnd,rnd_sum, rndno, sum, pl_sum; /* Hold our random numbers */ mpz_t rndBnd; /* Bound for mpz_urandomm */ mpz_t key, key1; //key for PRF mpz_t plain_total, plain_nomod; gmp_randstate_t gmpRandState, gmpRandState1, gmpRandState2; /* Random generator state object */ mpz_init(sum); mpz_init(pl_sum); mpz_init(rnd); mpz_init(rndno); mpz_init(rndBnd); mpz_init(randNum); mpz_init(key); mpz_init(key1); mpz_init(rnd_sum); mpz_init(plain_total); mpz_init(rnd); mpz_init(rndKey); mpz_init(plain_nomod); mpz_set_ui(plain_total, 0); plain = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); plain_sum = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); plain_temp = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); cipher_prod = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); cipher_total = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); cipher = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); len = (char *)malloc(2048*sizeof(char)); //mpz_init(cipher_prod->c); mpz_init(cipher_total->c); mpz_set_ui(rnd_sum, 0); mpz_init_set_str(cipher_prod->c, "1", 10); mpz_init_set_str(cipher_total->c, "1", 10); mpz_init_set_str(plain_sum->m, "0", 10); // printf("\nloading pailler keys"); if((fpub = fopen("pub.txt", "r"))) { pub = (paillier_pubkey_t*) malloc(sizeof(paillier_pubkey_t)); priv = (paillier_prvkey_t*) malloc(sizeof(paillier_prvkey_t)); mpz_init(pub->n_squared); mpz_init(pub->n); fgets(len, 1000, fpub); mpz_init_set_str(pub->p, len, 10); fgets(len, 1000, fpub); mpz_init_set_str(pub->q, len, 10); fgets(len, 1000, fpub); mpz_init_set_str(pub->n_plusone, len, 10); //printf("value of nplusone : \n"); //mpz_out_str(stdout, 10, pub->n_plusone); paillier_keygen(&pub, &priv, get_rand, 0); pub->bits = mpz_sizeinbase(pub->n, 2); fclose(fpub); } gmp_randinit_default(gmpRandState); mpz_set_str(rndBnd, "163000", 10); mpz_set_ui(rndKey, time(NULL)); gmp_randseed(gmpRandState, rndKey); fkey = fopen("prf_key.txt", "w"); mpz_urandomm(rndno, gmpRandState, pub->n); mpz_set(key, rndno); element_fprintf(fkey, "%Zd\n", key); mpz_urandomm(rndno, gmpRandState, pub->n); mpz_set(key1, rndno); element_fprintf(fkey, "%Zd", key1); fclose(fkey); gmp_randseed(gmpRandState, key); //mpz_out_str(stdout, 10, rnd); //****Opening files to read files and encrypt***** int i, j; mpz_t rand_val[val], rand_init[no_of_copies], mpz_result[no_of_copies]; char fileName[1000], file[1000]; //strcpy(fileName, "./cipher/copy3/output"); mpz_set_str(sum, "0", 10); mpz_set_str(pl_sum, "0", 10); for(j =0; j < no_of_copies; j++) { mpz_init(rand_init[j]); mpz_urandomm(rand_init[j], gmpRandState, pub->n); mpz_add(sum, sum, rand_init[j]); // mpz_mod(sum, sum, pub->n); //printf("\n 1st rand \n"); //mpz_out_str(stdout, 10, rand_init[j]); } // printf("\nsum\n"); // mpz_out_str(stdout, 10, sum); mpz_set_str(pl_sum, "0", 10); gmp_randseed(gmpRandState, key1); for(j =0; j < no_of_copies; j++) { mpz_set_ui(cipher_prod->c, 1); for(i = 0; i < val; i++) { sprintf(fileName, "./cipher/copy%d/output", (j+1)); if(j == 0) { do mpz_urandomm(randNum, gmpRandState, rndBnd); while(mpz_cmp_ui(randNum,0) ==0); mpz_init(rand_val[i]); mpz_set(rand_val[i], randNum); //printf("\n2ndrand\n"); //mpz_out_str(stdout, 10, randNum); // printf("random\n"); // mpz_out_str(stdout, 10, rand_val[i]); } else { mpz_set(randNum, rand_val[i]); } // sprintf(num, "output%d.txt", (j+1)); mpz_get_str(file, 10, randNum); strcat(fileName, file); strcat(fileName,".txt"); count = 0; // printf("\n%s", fileName); //mpz_out_str(stdout, 10, randNum); if(!(fciph = fopen(fileName, "r"))) { printf("\n not able to read %s", fileName); // fputs("not possible to read file!\n", stderr); } else { fgets(len, 1000, fciph); mpz_init_set_str(cipher->c, len, 10); //if(strstr(dir->d_name, "output") != NULL) //{ // printf("\ncipher\n"); //mpz_out_str(stdout, 10, cipher->c); paillier_mul(pub, cipher_prod, cipher_prod, cipher); // printf("\ncipher after product\n"); //mpz_out_str(stdout, 10, cipher_prod->c); //} fclose(fciph); } if(j == 0) { strcpy(fileName, "./split/output"); strcat(fileName, file); strcat(fileName,".txt"); if(!(fplain = fopen(fileName, "r"))) { printf("\n not read %s", fileName); count1++; } else { fgets(len, 1000, fplain); // printf("\npleain sum\n"); // mpz_out_str(stdout, 10, plain_sum->m); mpz_init_set_str(plain->m, len, 10); // if(strstr(dir->d_name, "output") != NULL) //{ mpz_add(plain_sum->m, plain_sum->m, plain->m); //mpz_mod(pla in_sum->m, plain_sum->m, pub->n); //} fclose(fplain); } } } mpz_powm(cipher_prod->c, cipher_prod->c, rand_init[j], pub->n_squared); paillier_mul(pub, cipher_total, cipher_total, cipher_prod); mpz_mul(plain_temp->m, plain_sum->m, rand_init[j]); mpz_add(plain_total, plain_total, plain_temp->m); mpz_mod(plain_total, plain_total, pub->n); } plain = paillier_dec(plain, pub, priv, cipher_total); printf("\n decrypt \n"); mpz_out_str(stdout, 10, plain->m); // printf("\n plain total \n"); //mpz_out_str(stdout, 10, plain_total); printf("\n plain text total\n"); mpz_out_str(stdout, 10, plain_sum->m); mpz_mul(pl_sum, plain_sum->m, sum); mpz_mul(pl_sum, pl_sum, pub->n); mpz_mul(pl_sum, pl_sum, priv->lambda); // mpz_mod(pl_sum, pl_sum, pub->n); printf("\n plain text * rnd\n"); mpz_out_str(stdout, 10, pl_sum); if((fprod = fopen("result/cipher_result.txt", "w"))) { printf("\nWriting the result to file \n"); gmp_fprintf(fprod, "%Zd\n", cipher_total->c); fclose(fprod); } return 0; }
int main(int argc, char *argv[]) { ///list all the files in the directory/// DIR *d; FILE *fpub, *fpriv, *fciph, *fplain, *ftag, *fpairing, *ftemp, *frand;//, *fp6, *fp7; paillier_pubkey_t *pub; paillier_prvkey_t *priv; paillier_get_rand_t get_rand; paillier_plaintext_t *plain; paillier_ciphertext_t *cipher, *cipher_copy; paillier_tag* tag; mpz_t tag_sig, *rand_prf; gmp_randstate_t rand; char *len; struct stat st= {0}; unsigned char *data; int count=0, count1=0, gbytes, n, no_copies=10; struct dirent *dir; ///pairing parameters pairing_t pairing; //pairing_t p; //printf("setting pairing parameters\n"); //pairing_init_set_str(pairing, param_str); // printf("after pairing setup\n"); element_t g, h, u, temp_pow, test1, test2; element_t public_key, sig; element_t secret_key; ///end of pairing parameters //initialize pairing parametrs pbc_demo_pairing_init(pairing, argc, argv); element_init_G2(g, pairing); element_init_G1(u, pairing); element_init_G1(test1, pairing); element_init_G2(test2, pairing); element_init_G1(temp_pow, pairing); element_init_G2(public_key, pairing); // element_from_hash(h, "hashofmessage", 13); element_init_G1(h, pairing); element_init_G1(sig, pairing); element_init_Zr(secret_key, pairing); //end of pairing parameters initialization //set up pairing parameters //generate system parameters element_random(g); // n = pairing_length_in_bytes_x_only_G1(pairing); // data = pbc_malloc(n); // gbytes = pairing_length_in_bytes_G2(pairing); // printf(" \n g in bytes %d \n", gbytes); // element_printf("system parameter g = %B\n", g); //generate private key element_random(secret_key); //generate u element_random(u); //calculating hash of a file name and mapping it to element in group G1 // element_from_hash(h, "FileName", 8); element_random(h); //element_printf("private key = %B\n", secret_key); //compute corresponding public key element_pow_zn(public_key, g, secret_key); //element_printf("public key = %B\n", public_key); //end of setup tag = (paillier_tag*) malloc(sizeof(paillier_tag)); plain = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); cipher = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); mpz_init(plain->m); mpz_init(tag->t); mpz_init(cipher->c); mpz_init(tag_sig); rand_prf = (mpz_t*) malloc(n*sizeof(mpz_t)); len = (char *)malloc(2048*sizeof(char)); //****paillier key generation**** if(!(fpub = fopen("pub.txt", "r"))) { //fputs("Not able to read public key file!\n", stderr); paillier_keygen(&pub, &priv, get_rand,450); //fclose(fpub); fpub = fopen("pub.txt", "w"); gmp_fprintf(fpub, "%Zd\n", pub->p); gmp_fprintf(fpub, "%Zd\n", pub->q); gmp_fprintf(fpub, "%Zd\n", pub->n_plusone); //***Writing private keys into a file*** fpriv = fopen("priv.txt", "w"); gmp_fprintf(fpriv, "%Zd\n", priv->lambda); gmp_fprintf(fpriv, "%Zd\n", priv->x); fclose(fpriv); //****End of writing private key in a file*** } else { printf("\n in else"); pub = (paillier_pubkey_t*) malloc(sizeof(paillier_pubkey_t)); priv = (paillier_prvkey_t*) malloc(sizeof(paillier_prvkey_t)); mpz_init(pub->n_squared); mpz_init(pub->n); fgets(len, 1000, fpub); mpz_init_set_str(pub->p, len, 10); fgets(len, 1000, fpub); mpz_init_set_str(pub->q, len, 10); fgets(len, 1000, fpub); mpz_init_set_str(pub->n_plusone, len, 10); //printf("value of nplusone : \n"); //mpz_out_str(stdout, 10, pub->n_plusone); paillier_keygen(&pub, &priv, get_rand, 0); pub->bits = mpz_sizeinbase(pub->n, 2); } fclose(fpub); //****end of paillier key generation**** //printf("writing pairing parameters to a file\n"); //writing pairing keys to file fpairing = fopen("pairing.txt", "w"); /* n = pairing_length_in_bytes_compressed_G2(pairing); data = pbc_malloc(n); element_to_bytes_compressed(data, g); element_printf(" decomp g %B\n", g); element_from_bytes_compressed(test2, data); element_printf(" decomp g %B\n", test2); */ //writing compressed g to file element_fprintf(fpairing, "%B\n", g); // element_printf(" g = %B\n", g); /*n = pairing_length_in_bytes_compressed_G1(pairing); data = pbc_malloc(n); element_to_bytes_compressed(data, u); element_printf(" decomp g %B\n", u); element_from_bytes_compressed(test1, data); element_printf(" decomp g %B\n", test1); //writing compressed u to file */ element_fprintf(fpairing, "%B\n", u); //element_printf(" u = %B\n", u); //writing secret key to file element_fprintf(fpairing, "%B\n", secret_key); //element_printf(" sk = %B\n", secret_key); // printf("secret key = %s\n",secret_key); /* n = pairing_length_in_bytes_compressed_G2(pairing); data = pbc_malloc(n); element_to_bytes_compressed(data, public_key); //writing compressed public key to file */ element_fprintf(fpairing, "%B\n", public_key); //element_printf("pk = %B\n", public_key); /* n = pairing_length_in_bytes_compressed_G1(pairing); data = pbc_malloc(n); element_to_bytes_compressed(data, h); element_printf(" decomp g %B\n", h); element_from_bytes_compressed(test1, data); element_printf(" decomp g %B\n", test1); //writing compressed h to file */ element_fprintf(fpairing, "%B\n", h); //element_printf("h = %B\n", h); //writing n to file gmp_fprintf(fpairing, "%Zd\n", pub->n); fclose(fpairing); //end of writing pairing keys to file cipher_copy = (paillier_ciphertext_t*)malloc(no_copies*sizeof(paillier_ciphertext_t)); frand = fopen("rand.txt","w"); int i; init_rand(rand, get_rand, pub->bits / 8 + 1); for(i = 0; i< no_copies; i++) { mpz_init(rand_prf[i]); do mpz_urandomb(rand_prf[i], rand, pub->bits); while( mpz_cmp(rand_prf[i], pub->n) >= 0 ); gmp_fprintf(frand, "%Zd\n", rand_prf[i]); //printf("\nrandom : \n"); //mpz_out_str(stdout, 10, rand_prf[i]); } fclose(frand); //****Opening files to read files and encrypt***** d = opendir("./split"); if (d) { while ((dir = readdir(d)) != NULL) { //printf("%s\n", dir->d_name); char fileName[1000], copy[1000]; strcpy(fileName, "./split/"); strcat(fileName,dir->d_name); //printf("\nfile name %s", fileName); if(!(fplain = fopen(fileName, "r"))) { printf("\n not able to read %s", fileName); // fputs("not possible to read file!\n", stderr); count1++; } else { //printf("\n able to read %s", fileName); fgets(len, 2048, fplain); mpz_init_set_str(plain->m, len, 10); // mpz_out_str(stdout, 10, plain->m); fclose(fplain); //Writing cipher text to files strcpy(fileName, "./cipher/"); //strcat(fileName,dir->d_name); //printf("\nfilename %s",fileName); paillier_enc(tag, cipher_copy, pub,plain, get_rand, no_copies, rand_prf); // mpz_out_str(stdout, 10, tag->t); int j; for(j=0;j < no_copies; j++) { char num[20]; strcpy(copy, fileName); sprintf(num, "copy%d/", (j+1)); // strcat(copy, ); strcat(copy, num); if(stat(copy, &st) == -1) mkdir(copy,0777); strcat(copy,dir->d_name); if(!(fciph = fopen(copy, "w"))) { printf("\nnot able to open file for writing cipher text %s", copy); } else { // printf("\nbefore enc"); gmp_fprintf(fciph, "%Zd\n", cipher_copy[j].c); fclose(fciph); } } //writing tags to files strcpy(fileName, "./tag/"); strcat(fileName,dir->d_name); //printf("\nfilename %s",fileName); if(!(ftag = fopen(fileName, "w"))) { printf("not able to open file for writing tag %s", fileName); } else { element_pow_mpz(temp_pow,u, tag->t); element_mul(temp_pow, temp_pow, h); element_pow_zn(sig, temp_pow, secret_key); element_fprintf(ftag, "%B", sig); fclose(ftag); } } count++; } closedir(d); } printf("\nTotal number of files : %d, unreadable files %d", count, count1); return 0; }
void generate_key(int bit_length, paillier_pubkey_t** public_key, paillier_prvkey_t** private_key) { paillier_keygen(bit_length, public_key, private_key, &paillier_get_rand_devurandom); }