Crypto++
-
Abend
Ich hab mich etwas mit der Crypto++ Library auseinader gesetzt und habe gemerkt, dass ich schnell an meine Grenzen gestoßen bin, da die Dokumentation für meine Begriffe recht dürftig ist.
Wenn ich mit dem erstellten Programm Dateien, welche größer als ca. 3Kb sind, verschlüssel (egal, wieviel bit der Schlüssel hat) kommt es zu einem Programmabsturz mit folgender Meldung:
http://img140.imageshack.us/my.php?image=screenshootwe0.pngEs liegt sehr nahe, dass ich irgendwelche wichtigen Parameter vergessen habe, die mir den Umgang mit größeren Dateien ermöglichen.
Wie gesagt, bei Dateien < 3KB funktioniert das Programm tadellos.
#ifdef _DEBUG #pragma comment (lib, "cryptlibd") #else #pragma comment (lib, "cryptlib") #endif #include <rsa.h> #include <hex.h> #include <files.h> #include <osrng.h> #include <iostream> #include <cstring> bool generate_keys(const std::string& public_key_file_name, const std::string& private_key_file_name, unsigned long bits) { // Zufallszahlengenerator CryptoPP::AutoSeededRandomPool rng; // Schlüsselerzeugung // privater Schlüssel CryptoPP::RSAES_OAEP_SHA_Decryptor decryptor(rng, bits); CryptoPP::HexEncoder private_file(new CryptoPP::FileSink(private_key_file_name.c_str())); decryptor.DEREncode(private_file); private_file.MessageEnd(); // öffentlicher Schlüssel CryptoPP::RSAES_OAEP_SHA_Encryptor encryptor(decryptor); CryptoPP::HexEncoder public_file(new CryptoPP::FileSink(public_key_file_name.c_str())); encryptor.DEREncode(public_file); public_file.MessageEnd(); // TODO: prüfen, ob Keys erstellt wurden return(true); } bool encrypt_file(const std::string& public_key_file_name, const std::string& source_file_name, const std::string& destination_file_name) { // TODO: prüfen, ob Key existiert // TODO: prüfen, ob Key gültig ist // TODO: prüfen, ob Quelldatei existiert // TODO: prüfen, ob Verschlüsslung erfolgreich war // Schlüssel laden CryptoPP::FileSource public_file(public_key_file_name.c_str(), true, new CryptoPP::HexDecoder); CryptoPP::RSAES_OAEP_SHA_Encryptor encryptor(public_file); // Zufallszahlengenerator CryptoPP::AutoSeededRandomPool rng; // Zieldatei laden, Verschlüsseln und in Quelldatei abspeichern CryptoPP::FileSource(source_file_name.c_str(), true, new CryptoPP::PK_EncryptorFilter(rng, encryptor, new CryptoPP::HexEncoder(new CryptoPP::FileSink(destination_file_name.c_str())))); return(true); } bool decrypt_file(const std::string& private_key_file_name, const std::string& source_file_name, const std::string& destination_file_name) { // TODO: prüfen, ob Key existiert // TODO: prüfen, ob Key gültig ist // TODO: prüfen, ob Quelldatei existiert // TODO: prüfen, ob Entschlüsslung erfolgreich war CryptoPP::FileSource private_file(private_key_file_name.c_str(), true, new CryptoPP::HexDecoder); CryptoPP::RSAES_OAEP_SHA_Decryptor decryptor(private_file); // Zufallszahlengenerator CryptoPP::AutoSeededRandomPool rng; // Zieldatei laden, Entschlüsseln und in Quelldatei abspeichern CryptoPP::FileSource(source_file_name.c_str(), true, new CryptoPP::HexDecoder(new CryptoPP::PK_DecryptorFilter(rng, decryptor, new CryptoPP::FileSink(destination_file_name.c_str())))); return(true); } /* bool encrypt_string(const std::string& public_key_file_name, const std::string& source_file_name, const std::string& destination_file_name) { return(false); } bool decrypt_string(const std::string& private_key_file_name, const std::string& source_file_name, const std::string& destination_file_name) { return(false); } */ int main(void) { int choice; while(1) { std::cout << "\n[0] - cancel\n" << "[1] - generate keys\n" << "[2] - encnryt Message\n" << "[3] - decrypt Message\n" << "[4] - help\n"; std::cin >> choice; switch(choice) { // Beenden case 0: return 0; // Schlüssel erzeugen case 1: { std::string pbkf, pvkf; unsigned long key_strength; std::cout << "path and name of the public key file\n"; std::cin >> pbkf; std::cout << "path and name of the private key file\n"; std::cin >> pvkf; std::cout << "key strength in bits; recommended values[1024, 2048, 4092, 8192, 16384]\n"; std::cin >> key_strength; generate_keys(pbkf, pvkf, key_strength); std::cout << "keys were created successfully\n"; break; } // Verschlüsseln case 2: { std::string pbkf, sf, df; std::cout << "path and name of the public key file\n"; std::cin >> pbkf; std::cout << "path and name of the source file\n"; std::cin >> sf; std::cout << "path and name of the destination file\n"; std::cin >> df; encrypt_file(pbkf, sf, df); std::cout << "file were encrypted successfully\n"; break; } // Entschlüsseln case 3: { std::string pvkf, sf, df; std::cout << "path and name of the private key file\n"; std::cin >> pvkf; std::cout << "path and name of the source file\n"; std::cin >> sf; std::cout << "path and name of the destination file\n"; std::cin >> df; decrypt_file(pvkf, sf, df); std::cout << "file were decrypted successfully\n"; break; } case 4: break; } } return 0; }MangaMan