dimanche 2 juin 2019

C++ sharing variable between switch cases for encryption decryption program

Running a c++ program for AES encryption/decryption on Ubuntu and have run into a need to share data between case statements.

Case 3 runs what I need in case 4 because I want to give the user an option to encrypt or decrypt. Should I rethink my whole architecture here or am I missing something simple?

I want to be able to decrypt the results I get from case 3. Case 3 should output to a file encrypted binary where I will want to open that file and decrypt its contents in case 4. Any advice here on correctly accessing the binary data within the file for case 4 would also be helpful.

cout << "For ENCRYPTION enter 3\nFor DECRYPTION enter 4\nTo terminate the program enter 5" << endl; cin>> a;

            switch(a)
            { 
                case 3:
                {
                    //file1.getkey();

                    cout << "Enter data to the file" << endl;
                    cin >> text;

                    // The StreamTransformationFilter removes
                    //  padding as required.
                    StringSource s(text, true, 
                        new StreamTransformationFilter(e,
                            new StringSink(cipher), 
                                StreamTransformationFilter::PKCS_PADDING
                        ) // StreamTransformationFilter
                    ); // StringSource

                    ArraySource as(key, sizeof(key), true, new FileSink(filenaam));

                    // Pretty print
                    encoded.clear();
                    StringSource(cipher, true,
                        new HexEncoder(
                            new StringSink(encoded)
                        ) // HexEncoder
                    ); // StringSource
                    cout << "cipher text: " << encoded << endl;   

                    /*ofstream InputData(filenaam);
                    if (InputData.is_open())
                      {
                        InputData << encoded << endl;
                        InputData.close();
                      }*/

                 return 0;

                }
                break;
                case 4:
                { 
                    std::ifstream file(filenaam, std::ios::binary | std::ios::ate);
                    std::streamsize size = file.tellg();
                    file.seekg(0, std::ios::beg);

                    std::vector<char> buffer(size);
                    if (file.read(buffer.data(), size))
                    {
                        /* worked! */
                    }
                    //FileSource file(filenaam, true);

                    string content( (std::istreambuf_iterator<char>(file) ),
                       (std::istreambuf_iterator<char>()    ) );

                    StringSource ss( filestr, true, 
                        new StreamTransformationFilter( e,
                            new StringSink( decryptedtext ),
                            StreamTransformationFilter::NO_PADDING
                        ) // StreamTransformationFilter      
                    ); // StringSource

                    cout << "\n";
                    cout << filestr;

                    CBC_Mode< AES >::Decryption d;
                    d.SetKeyWithIV(key, sizeof(key), iv);
                    // The StreamTransformationFilter removes
                    //  padding as required.
                    StringSource s(decryptedtext, true, 
                        new StreamTransformationFilter(d,
                            new StringSink(recovered)
                        ) // StreamTransformationFilter
                    ); // StringSource

                    cout << "\n";
                    cout << decryptedtext;
                    cout << "\n";
                    cout << "recovered text: " << recovered << endl;

                    //decrypt(file1);

                    return 0;

                 }
                 break;
                 case 5:
                 {
                    string o = filenaam;
                    string email;

                    cout << "enter email to send the file to";
                    cin >> email;

                    return 0;
                 }
                 break;

Currently running case 4 will not be able to see the results of variables from case 3 therefore I am left without anything to decrypt for my decryption case.

Aucun commentaire:

Enregistrer un commentaire