Exemplo n.º 1
0
int main(int argc, char *argv[]) {

	NTL::ZZ p(NTL::INIT_VAL, argv[1]);
	NTL::ZZ q(NTL::INIT_VAL, argv[2]);

	rsa_keys_tuple keys;

	if (argc == 5) {
		keys = gen_keys(p, q, atoi(argv[4]));
	} else {
		keys = gen_keys(p, q);
	}


	rsa_key private_key, public_key;

	public_key = std::get<0>(keys);
	private_key = std::get<1>(keys);

	std::cout << "Public Key: ";
	print_rsa_key(public_key);

	std::cout << "Private Key: ";
	print_rsa_key(private_key);

	return 0;
}
Exemplo n.º 2
0
int  main(void)
{
    large_digit t;
    large_digit i;
    large_digit shift;
    large_digit u, v, m;
    large_digit e, d, n;
    large_digit p, q, psi;

    srand(time(NULL));

#if 0
    large_digit d1, d2;
    d1.read_digit("48702C12C0FC3EC2456769127A09C444");
    d2.read_digit("BC72B98A8FE6392F");

    d1 % d2;
    return 0;
#endif

    DWORD keygen_start = GetTickCount();
    prime_product(p);
    prime_product(q);
    while (p == q) {
        fprintf(stderr, "p == q\n");
        prime_product(q);
    }

    n = p * q;
    psi = (p - 1) * (q - 1);

    char buf[1024];
    printf("p = %s\n", p.write_digit(buf, 1000));
    printf("q = %s\n", q.write_digit(buf, 1000));

    e = 65537;
    while (egcd(psi, e)) {
        rand_product(e);
        e %= psi;
    }

    d = edura(e, psi);
    print_rsa_key(e, d, n);
    DWORD keygen_finish = GetTickCount();
    printf("time use: %d\n", keygen_finish - keygen_start);
    getchar();

    i = n;
    shift = 1;
    int ij = 0, jj = 0;
    while (i != 0) {

        jj = 0;
        t = shift;
        while (t != 0) {
            rand_product(m);
            m &= (shift - 1);
            m |= shift;
            m |= 1;
            m %= n;

            DWORD crypt0 = GetTickCount();
            v = expr_mod(m, e, n);
            DWORD crypt1 = GetTickCount();
            u = expr_mod(v, d, n);
            DWORD crypt2 = GetTickCount();
            if (m != u) {
                char mbuf[1024], ubuf[1024], vbuf[1024];
                m.write_digit(mbuf, sizeof(mbuf));
                u.write_digit(ubuf, sizeof(ubuf));
                v.write_digit(vbuf, sizeof(vbuf));
                printf ("m = %s, u = %s, v = %s\n", mbuf, ubuf, vbuf);
                assert(0);
            }

            printf("i = %d, j = %d et = %d, dt = %d\n", ij, jj, crypt1 - crypt0, crypt2 - crypt1);
            t >>= 1;
            jj++;
        }

        shift <<= 1;
        i >>= 1;
        ij++;
    }

    return 0;
}