Пример #1
0
//TODO: Impement RSA
void RSA()
{
    llong p = get_prime_number(1, 30'000);
                               llong q = get_prime_number(1, 30'000);

    llong n = p * q;
    llong fi = (p - 1) * (q - 1);

    // Get c and d
    llong d{0};
    llong gcd{0}, x{0}, y{0}, c{0};

    while (true) {
        d = get_prime_number(1, fi -1);
        generalized_euclid(d, fi, gcd, x, y);
        if (gcd == 1) {
            break;
        }
    }
    generalized_euclid(d, fi, gcd, x, y);

    if (x < 0) {
        c = fi + x;
    } else {
        c = x;
    }
    // Open file w/ message
    std::ifstream msg("example", std::ios::binary | std::ios::in);
    if (!msg.is_open()) {
        std::cerr << "File not found" << std::endl;
    }

    llong m{0}, e{0};

    // Encode
    std::ofstream encode_out("rsa_encode", std::ios::binary | std::ios::out);
    while (msg.read((char*)&m, sizeof(char))) {
        e = pow_module(m, d, n);
        encode_out.write((char*)&e, sizeof(llong));
    }
    msg.close();
    encode_out.close();

    // Decode
    std::ifstream encode_in("rsa_encode", std::ios::binary | std::ios::in);
    std::ofstream decode_out("rsa_decode", std::ios::binary | std::ios::out);
    while (encode_in.read((char*)&e, sizeof(llong))) {
        m = pow_module(e, c, n);
        decode_out.write((char*)&m, sizeof(char));
    }
    encode_in.close();
    decode_out.close();
}
Пример #2
0
int encode_out_ev(Gzb64* gzb64, struct evbuffer* output)
{

	struct evbuffer_iovec v[2];
	int n, i, written;
	size_t n_to_add = BUFF_SIZE;

	/* Reserve BUFF_SIZE bytes.*/
	n = evbuffer_reserve_space(output, n_to_add, v, 2);
	if (n<=0)
	   return -1; /* Unable to reserve the space for some reason. */

	for (i=0; i<n && n_to_add > 0; ++i) {
	   size_t len = v[i].iov_len;
	   if (len > n_to_add) /* Don't write more than n_to_add bytes. */
		  len = n_to_add;
	   if ( (written = encode_out(gzb64, v[i].iov_base, len)) < 0) {
		  /* If there was a problem during data generation, we can just stop
		     here; no data will be committed to the buffer. */
		  return -1;
	   }
	   /* Set iov_len to the number of bytes we actually wrote, so we
		  don't commit too much. */
	   v[i].iov_len = written;
	}

	/* We commit the space here.  Note that we give it 'i' (the number of
	   vectors we actually used) rather than 'n' (the number of vectors we
	   had available. */
	if (evbuffer_commit_space(output, v, i) < 0)
	   return -1; /* Error committing */

	/* recurse if some bytes were retrieved */
	if( written > 0 )
		return encode_out_ev(gzb64, output);
	
	resetEncoder(gzb64);

	return 0;
}