Example #1
0
void bg_decrypt_block(bg_prikey key, FILE *in, FILE *out, mp_size_t len) {
	mpz_t y, xqmp, xpmq, pp, qq, negone;
	unsigned long int l;
	bbs_state *bbs;

	mpz_init(y);
	mpz_init(xqmp);
	mpz_init(xpmq);
	mpz_init(pp);
	mpz_init(qq);
	mpz_init_set_si(negone, -1);
	bbs = init_bbs(negone, key.n);
	bbs_gen(bbs, NULL, 8*len, 0);
	bbs_iter(bbs);
	bbs_close(bbs);
	l = bbs->len;
	mpz_inp_raw(y, in);

	mpz_add_ui(pp, key.p, 1);
	mpz_tdiv_q_ui(pp, pp, 4);
	mpz_pow_ui(pp, pp, l);
	mpz_powm_sec(pp, y, pp, key.p);

	mpz_add_ui(qq, key.q, 1);
	mpz_tdiv_q_ui(qq, qq, 4);
	mpz_pow_ui(qq, qq, l);
	mpz_powm_sec(qq, y, qq, key.q);

	mpz_mul(xqmp, key.q, pp);
	mpz_powm(pp, key.q, negone, key.p);
	mpz_mul(xqmp, xqmp, pp);

	mpz_mul(xpmq, key.p, qq);
	mpz_powm(qq, key.p, negone, key.q);
	mpz_mul(xpmq, xpmq, qq);

	mpz_add(y, xqmp, xpmq);
	mpz_mod(y, y, key.n);

	bbs = init_bbs(y, key.n);
	while(len && !feof(in)) len -= bg_xor(bbs, in, out, len);
	bbs_close(bbs);
}
Example #2
0
void bg_encrypt_block(bg_pubkey key, FILE *in, FILE *out, mp_size_t len) {
	mpz_t r;
	bbs_state *bbs;
	mp_bitcnt_t blen;

	mpz_init(r);
	blen = mpz_sizeinbase(key.n, 2);
	do {
		if(rand_bigint(r, blen)) {
			fprintf(stderr, "Failed to write, can't start block.\n");
			return;
		}
	} while(mpz_cmp_ui(r, 1) < 0 || mpz_cmp(key.n, r) < 0);

	bbs = init_bbs(r, key.n);
	bbs_gen(bbs, NULL, 8*len, 0);
	bbs_iter(bbs);
	mpz_out_raw(out, bbs->obj);
	bbs_close(bbs);

	bbs = init_bbs(r, key.n);
	while(len && !feof(in)) len -= bg_xor(bbs, in, out, len);
	bbs_close(bbs);
}
Example #3
0
int main(int argc,char** argv)
{
	BGInteger* bg1,*bg2;
	while(1)
	{
		scanf("%s",l_val);
		if(strcmp(l_val,"quit")==0)
		{
			break;
		}
		scanf("%s",op);
		scanf("%s",r_val);

		if(*l_val=='-')
		{
			bg1=bg_create_from_decstr(l_val+1);
			bg_self_negative(bg1);
		}
		else
		{
			bg1=bg_create_from_decstr(l_val);
		}
		if(*r_val=='-')
		{
			bg2=bg_create_from_decstr(r_val+1);
			bg_self_negative(bg2);
		}
		else
		{
			bg2=bg_create_from_decstr(r_val);
		}


		BGInteger* r=NULL;

		if(strcmp(op,"add")==0)
		{
			r=bg_plus(bg1,bg2);
		}
		else if(strcmp(op,"sub")==0)
		{
			r=bg_minus(bg1,bg2);
		}
		else if(strcmp(op,"mul")==0)
		{
			r=bg_mul(bg1,bg2);

		}
		else if(strcmp(op,"div")==0)
		{
			r=bg_div(bg1,bg2);

		}
		else if(strcmp(op,"mod")==0)
		{
			r=bg_mod(bg1,bg2);
		}
		else if(strcmp(op,"lshift")==0)
		{
			r=bg_lshift(bg1,bg2);
		}
		else if(strcmp(op,"rshift")==0)
		{
			r=bg_rshift(bg1,bg2);
		}
		else if(strcmp(op,"and")==0)
		{
			r=bg_and(bg1,bg2);
		}
		else if(strcmp(op,"or")==0)
		{
			r=bg_or(bg1,bg2);
		}
		else if(strcmp(op,"xor")==0)
		{
			r=bg_xor(bg1,bg2);
		}
		else if(strcmp(op,"negated")==0)
		{
			r=bg_negated(bg1);
		}
		else if (strcmp(op,"cmp")==0)
		{
			int value=bg_cmp(bg1,bg2);
			printf("%d\n",value);
			bg_free(bg1);
			bg_free(bg2);
			continue;
		}
		else
		{
			printf(" %s is invalid,please use right operator\n",op);
			bg_free(bg1);
			bg_free(bg2);
			break;
		}

		bg_print_dec(r);
		printf("\n");
		//bg_print_bin(r);
		//printf("\n");

		bg_free(bg1);
		bg_free(bg2);
		bg_free(r);
	}

	return 0;

}