entschlüsseln Ich habe seit einiger Zeit damit zu kämpfen. Wenn ich mein Programm laufen, manchmal sehe ich diese Fehler:Fehler, die Nachricht mit OpenSSL RSA-Funktionen in C++
bad decrypt
140380701197976:error:0606506D:digital envelope
routines:EVP_DecryptFinal_ex:wrong final block length:evp_enc.c:518:
Manchmal sehe ich diese Fehler:
RSA operation error
139986632922776:error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error:rsa_pk1.c:273:
139986632922776:error:04065072:rsa
routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed:rsa_eay.c:602:
Error reading password from BIO
Error getting password
Und manchmal sehe ich keine Fehler überhaupt nicht! Ich benutze GalliumOS, eine Version von Ubuntu für Chrome-Hardware.
Was fehlt mir? Ich habe überall nachgesehen, und ich kann nichts finden, was sehr relevant ist. Als Referenz habe ich meinen Code unten angehängt. Ich glaube, ich habe den Fehler auf einen der letzten beiden Befehle reduziert, aber ich bin nicht positiv.
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string bob_keys, alice_plaintext, alice_encrypted, bob_decrypted;
// ----- USER INPUT -----
// TODO: switch to user input
bob_keys = "bob_keys.pem";
bob_decrypted = "bob_decrypted.txt";
alice_encrypted = "alice_encrypted.txt";
alice_plaintext = "alice_plaintext.txt";
// ----- CONFIDENTIALITY: MESSAGE ENCRYPTION -----
// generate session key
system("openssl rand -base64 64 -out key.bin");
// encrypt message using session key
system(("openssl enc -aes-128-cbc -salt -in " + alice_plaintext
+ " -out alice_plaintext.txt.enc -pass file:./key.bin").c_str());
// encrypt session key using Bob's public key
system(("openssl rsautl -encrypt -inkey " + bob_keys
+ " -pubin -in key.bin -out key.bin.enc").c_str());
// write encrypted message and encrypted session key to file
system(("cat alice_plaintext.txt.enc > " + alice_encrypted).c_str());
system(("echo >> " + alice_encrypted).c_str());
system(("cat key.bin.enc >> " + alice_encrypted).c_str());
// ----- CONFIDENTIALITY: MESSAGE DECRYPTION -----
// get encrypted message and encrypted session key from file (and remove newlines)
system(("head -1 " + alice_encrypted + " > message.bin.enc").c_str());
system("tr -d '\n' <message.bin.enc> temp.bin && mv temp.bin message.bin.enc");
system(("tail -1 " + alice_encrypted + " > key.bin.enc").c_str());
// decrypt the key using Bob's private key
system(("openssl rsautl -decrypt -inkey " + bob_keys
+ " -in key.bin.enc -out key.bin").c_str());
// decrypt the message using the decrypted key
system(("openssl enc -d -aes-128-cbc -in message.bin.enc -out "
+ bob_decrypted + " -pass file:./key.bin").c_str());
return 0;
}
Ihr Code ist nichts als Aufrufe an 'system'. Warum nicht einfach ein Shell-Skript verwenden? – dbush
Ja, das ist, was ich dachte ... aus irgendeinem Grund, unsere Lehrer wollen, dass wir eine C++ Datei einreichen, aber sie sind total cool mit uns, System-Anrufe zu machen. Ich stimme dem auch nicht zu, aber es funktioniert. – hockeysaint