bassprodukt
bassprodukt

Reputation: 121

Java - Encrypting with RSA key

I've been fighting with this issue for a while now. The matter is that I have to send a PEM string to a server, which expects the final step of the following:

Here's what I got so far:

Use all that to create the PEM string:

            StringBuilder sb = new StringBuilder();
            sb.append(StringUtils.repeat("-", 5));
            sb.append("BEGIN PEM file");
            sb.append(StringUtils.repeat("-", 5));
            sb.append("\n");

            sb.append("Proc-Type: 4,ENCRYPTED\n");
            sb.append("DEK-Info: " + "AES-256-CBC" + "," + new String(aes.get("IV")) + "\n");
            sb.append("");
            sb.append(Base64.encode(rsa.doFinal(aes.get("key"))));

            sb.append("\n");
            sb.append(StringUtils.repeat("-", 5));
            sb.append("END PEM file");
            sb.append(StringUtils.repeat("-", 5));

And then send that out to the server, which throws the following error:

3936:error:0906D06C:PEM routines:PEM_read_bio:no start line:.\crypto\pem\pem_lib.c:698:

I don't have much more visibility of the error, but I wanted to check if there's anything wrong I might be doing in the process, as it seems that error is associated to the unrecognition of the PEM.

Let me know if you have any questions.

Thanks!

Upvotes: 2

Views: 1813

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 93948

The specific error is reported because there should be no spaces between the dashes and the BEGIN statement. I don't know about the other issues, but it seems you have some work ahead of you to match the exact input requirements. Make sure you understand precisely what is expected, or you may have to try different formats "ad nauseam".

Upvotes: 2

Related Questions