//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(); }
int encode_in_ev(Gzb64* gzb64, struct evbuffer* input) { int contiguous; unsigned char* input_block; while(evbuffer_get_length(input) > 0) { contiguous = evbuffer_get_contiguous_space(input); input_block = evbuffer_pullup(input, contiguous); if( contiguous < evbuffer_get_length(input) ) { encode_in(gzb64, &input_block[0], contiguous, false); evbuffer_drain(input, contiguous); } else { /* last (only) block */ encode_in(gzb64, &input_block[0], contiguous, true); evbuffer_drain(input, contiguous); } } return 0; }
inline bool encode(const char *data, uint32_t dataSize, const std::string& outFile, const std::string& pass, bool isOffice2013, const std::string& masterKey, int spinCount) { std::string encryptedPackage; ms::EncryptionInfo info; const cybozu::crypto::Cipher::Name cipherName = isOffice2013 ? cybozu::crypto::Cipher::N_AES256_CBC : cybozu::crypto::Cipher::N_AES128_CBC; const cybozu::crypto::Hash::Name hashName = isOffice2013 ? cybozu::crypto::Hash::N_SHA512 : cybozu::crypto::Hash::N_SHA1; encode_in(encryptedPackage, info, std::string(data, dataSize), cipherName, hashName, spinCount, pass, masterKey); const std::string encryptionInfoStr = info.addHeader(info.toXml(isOffice2013)); dprintf("encryptionInfoStr size=%d\n", (int)encryptionInfoStr.size()); ms::cfb::CompoundFile cfb; ms::makeDataSpace(cfb.dirs, encryptedPackage, encryptionInfoStr); std::string outData; makeLayout(outData, cfb); { std::ofstream ofs(outFile.c_str(), std::ios::binary); ofs.write(outData.c_str(), outData.size()); if (!ofs) throw cybozu::Exception("ms:encode:write") << outData; } return true; }