0

I'm trying to encrypt an rsa public key that I have generated using libary poco crypto. The idea came from these answers (C++ Encrypt a text file, allow use of decrypt via ifstream). The pubkey is in the form of "MIIBIDANBgkqhkiG.........== I'm using ubuntu 16.04 and poco library version 1.7.8p2 which has been built with OpenSSL 1.0.2g.

The code I use is the following:

#include <iostream>
#include <fstream> 
#include "Poco/Crypto/CipherFactory.h"
#include "Poco/Crypto/Cipher.h"
#include "Poco/Crypto/RSADigestEngine.h"

using namespace std;
using namespace Poco::Crypto;

int main(int argc, char** argv)   
{ 
    try {
  Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey("pubkey.txt"));       
    } 
    catch (const exception& exc)
        {
            cout << exc.what() << endl;
        }   
}

When I run the above code I get the exception "file access error" The txt file is given all the permissions to read write and execute. Afterwards I tried with the istream constructor that RSAKey class provides:

int main(int argc, char** argv)

{
    try {

        ifstream myfile("pubkey.txt");
        Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey(&myfile));

    catch (const exception& exc)

    {

      cout << exc.what() << endl;

    }

}

But I got the same error.

It worked when I replaced the above code with the following line:

Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(RSAKey(RSAKey::KL_1024, RSAKey::EXP_SMALL));

But this is not what I want. I also print the text file into a string by using

ifstream myfile("pubkey.txt");
string file;
myfile >> file;
file;

and the file is correctly written into the string.

What am I doing wrong in this case?

Cœur
  • 37,241
  • 25
  • 195
  • 267
dk13
  • 1,461
  • 4
  • 20
  • 47
  • Why do you want to encrypt a *public* key? Public keys are, err, **public**. – Martin Bonner supports Monica Aug 29 '18 at 09:14
  • @Martin Bonner I assume Public key would be used to encrypt the data, because the key pair is mathematically related, whatever is encrypted with a Public Key may only be decrypted by its corresponding Private Key and vice versa. But the point is that the code does not work properly – dk13 Aug 29 '18 at 09:27
  • Ah! Sorry, you said "I'm trying to encrypt an rsa public key" - that means that the value of the key is the plaintext from which you are trying to generate a ciphertext (presumably using another key). This is not completely crazy, because asymmetric keys are usually only used for encrypting other keys (usually symmetric keys which are then used to encrypt the actual message). I think that what you meant was "I'm trying to encrypt *with* an rsa public key" - which means that you have some plaintext and want to use the key to turn it into ciphertext. – Martin Bonner supports Monica Aug 29 '18 at 10:30

1 Answers1

0

I have faced the same problem and it turned out to be format error.

A friend of mine saw that the file format was wrong and fixed it and it worked.

Make sure that the file format is right, check every line ending.

Here is a screenshot from VIM for the file. Also, the file extension is .pub or .pem. I'm not sure though if the file extension affect it.

enter image description here

Yash Gupta
  • 1,807
  • 15
  • 21