1. Phil Roth
  2. PowerBuilder
  3. Wednesday, 4 September 2019 14:24 PM UTC

Hi,

We are trying to encrypt log on information using Asymmetric Encryption RSA 2048 using PB17 R3

The Public Key is being provided by another system using OpenSSH which we are receiving as a .PEM file.

When I try and use this Public Key to encrypt, I'm getting  following error :-

"Powerbuilder Application execution Error R0208" and "Invalid PubKey in asymmetric Encrypt".

Please can you advise how I need to format the contents of the .PEM file. I've tried using the  Base64Decode

sPublicKey = '-----BEGIN RSA PUBLIC KEY-----' + '~r~n' + &
'MIIBCgKCAQEA2E7AXucgl0h6kEo7QWgZRuCsTHuroFrIH6oh0emJTBeLsUqqLFu5+LiRvV2QKGIqF9KpiCAatiBEpTSPtXbFlFdCrSsjGg7q/eqkA/sN+FwwtLpRFwMazNwAtJ5YQk1hdFosWkG0beVYpfqfeWHxNAdwS01SujYuZRUzlXnq/OARfLzNketOYRc9TpQvZaeCjbavqmm7VSLn3we3Mezcpj75KP5ozwRCydiBJS1Yy5I3pK+fgWtePZeqenq78Vn+C0n8ZgkQFdqmDUKSGV9BnbKy3+qfKZifF9f1ppcM4r0XMzZeXXOnjAWoK8LGYefz3s9l2ltMKeMd30464/ywqwIDAQAB' + '~r~n' + &
'-----END RSA PUBLIC KEY-----'

ls_Base64Str =sPublicKey
lblb_pubkey = lnv_CoderObject.Base64Decode(ls_Base64Str)
messagebox("Base64Decode", string(lblb_pubkey, EncodingANSI!))

lblb_encrypt = lnv_CrypterObject.AsymmetricEncrypt(RSA!, lblb_data, lblb_pubKey)

=> Public Key Error

 

Regards

Phil

 

 

 

Miguel Leeuwe Accepted Answer Pending Moderation
  1. Wednesday, 4 September 2019 14:59 PM UTC
  2. PowerBuilder
  3. # 1

Not sure if it works but at least not the same error:

Blob lblb_data
Blob lblb_privKey
Blob lblb_pubKey
Blob lblb_encrypt

string sPublicKey = '-----BEGIN RSA PUBLIC KEY-----' + '~r~n' + &
'MIIBCgKCAQEA2E7AXucgl0h6kEo7QWgZRuCsTHuroFrIH6oh0emJTBeLsUqqLFu5+LiRvV2QKGIqF9KpiCAatiBEpTSPtXbFlFdCrSsjGg7q/eqkA/sN+FwwtLpRFwMazNwAtJ5YQk1hdFosWkG0beVYpfqfeWHxNAdwS01SujYuZRUzlXnq/OARfLzNketOYRc9TpQvZaeCjbavqmm7VSLn3we3Mezcpj75KP5ozwRCydiBJS1Yy5I3pK+fgWtePZeqenq78Vn+C0n8ZgkQFdqmDUKSGV9BnbKy3+qfKZifF9f1ppcM4r0XMzZeXXOnjAWoK8LGYefz3s9l2ltMKeMd30464/ywqwIDAQAB' + '~r~n' + &
'-----END RSA PUBLIC KEY-----'

lblb_pubkey = Blob(sPublicKey, EncodingAnsi!)
lblb_data = Blob("Test Rsa", EncodingAnsi!)

CrypterObject lnv_CrypterObject
lnv_CrypterObject = Create CrypterObject

// Generate the private key
int li_return
li_return = lnv_CrypterObject.AsymmetricGenerateKey(RSA!, 1024, lblb_privKey, lblb_pubKey)
if li_return = 1 then
messagebox("Success", "Key is generated successfully!")
else
messagebox("Error", "Failed to generate the key!")
end if

messagebox('Private Key', string(lblb_privKey, EncodingAnsi!))

// Encrypt data using RSA
lblb_encrypt = lnv_CrypterObject.AsymmetricEncrypt(RSA!, lblb_data, lblb_pubKey)
messagebox('Encrypted data', string(lblb_encrypt, EncodingAnsi!))

destroy lnv_CrypterObject

 

Comment
  1. Miguel Leeuwe
  2. Wednesday, 4 September 2019 15:00 PM UTC
I think it doesn't work with base64 encoding.
  1. Helpful
There are no comments made yet.
Phil Roth Accepted Answer Pending Moderation
  1. Wednesday, 4 September 2019 15:49 PM UTC
  2. PowerBuilder
  3. # 2

Thanks Miguel, but in your code you are generating the Private and Public keys, whereas I am receiving public key from

another system in order to encrypt my data.

So, I think I'm hitting some formatting error on the public key that I am receiving as it's from a .pem file, and I 

think it needs to be converted so PB17 can process it.

Comment
  1. Miguel Leeuwe
  2. Wednesday, 4 September 2019 15:53 PM UTC
Miguel Leeuwe

yes, because that's what I found in an example, just to illustrate.

But ... if you leave that private key part out, this part still seems to work:



// Encrypt data using RSA

lblb_encrypt = lnv_CrypterObject.AsymmetricEncrypt(RSA!, lblb_data, lblb_pubKey)

messagebox('Encrypted data', string(lblb_encrypt, EncodingAnsi!))

  1. Helpful
  1. Miguel Leeuwe
  2. Wednesday, 4 September 2019 15:55 PM UTC
The difference is that I created the blob variables using EncodingAnsi! and no base64 encoding.



blb_pubkey = Blob(sPublicKey, EncodingAnsi!)

lblb_data = Blob("Test Rsa", EncodingAnsi!)



HIH
  1. Helpful
There are no comments made yet.
Ken Guo @Appeon Accepted Answer Pending Moderation
  1. Thursday, 5 September 2019 06:27 AM UTC
  2. PowerBuilder
  3. # 3

Hi Phil,

There are some difference between the PublicKey you provided and that the PB used. The PB PublicKey is generated via the following function:
lnv_CrypterObject.AsymmetricGenerateKey(RSA!, 1024, lblb_privKey, lblb_pubKey)

Below is the PublicKey example it generated:
MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAlZjl7EXsklLY4rKDb7IaovS7ZKC/qSmCNNHgdsFXxmg2Lur3FwH7BF0W9K1pb8mM4GxWS20x2yV25Dl5xMoX8+HFg35qnifjzbJlFq4MuH1RNrxNlg75PKFQ6djXdv/eVbUbq1ATulOyJZkKf+xU916Dztc6Q6V7gWhwF0LJq/gJ6AbPEVRt3+SYBZhu6kjHZUyQc70tNso7lmpuYPhPpjofulVqiXuamiyx+rSM5G1DFLCeAXWZ3qP+oEZwf9kpPa5FlxxdS9da3j6l/BIWX6we7ponuaoVM+ZMn4XYQOasPYXueXZGrHy16h+5PZBobhQnvqFo0dlNkELDqyGz9QIBEQ==

Do you know how the third party program generate the pubkey via OpenSSH?
Could you please let us know the exact function names and the parameter values?

Regards,
Ken

Comment
  1. Miguel Alzate
  2. Friday, 28 October 2022 10:30 AM UTC
Hi Phil.

How did you manage to get the public key from the PEM file sent to you by the other system? What PowerBuilder function did you use? What if the public key comes in a CER file?

  1. Helpful
There are no comments made yet.
Phil Roth Accepted Answer Pending Moderation
  1. Thursday, 5 September 2019 13:42 PM UTC
  2. PowerBuilder
  3. # 4

Hi Ken,

The public key is generated on Tandem platform using SSH calls from a C program

 

#include "appsh"
#include "rsah"
#include "pemh"
#include "err1h"
#include
#include
#define KEY_LENGTH 2048
#define PUB_EXP 3
#define PRINT_KEYS
#define WRITE_TO_FILE
int main(void) {
size_t pri_len; /* Length of private key */
size_t pub_len; /* Length of public key */
char *pri_key; /* Private key */
char *pub_key; /* Public key */
char msg[KEY_LENGTH/8]; /* Message to encrypt */
char *encrypt = NULL; /* Encrypted message */
char *decrypt = NULL; /* Decrypted message */
char *err; /* Buffer for any error messages */
RSA *keypair;
BIO *pri;
BIO *pub;
BIO *bp_public, *bp_private;
FILE *out;
FILE *pu;
int ret;
int encrypt_len, elen;


/* Generate key pair */
printf("Generating RSA (%d bits) keypair...", KEY_LENGTH);
fflush(stdout);
keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);
/* To get the C-string PEM form: */
pri = BIO_new(BIO_s_mem());
pub = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
PEM_write_bio_RSAPublicKey(pub, keypair);
pri_len = BIO_pending(pri);
pub_len = BIO_pending(pub);
pri_key = malloc(pri_len + 1);
pub_key = malloc(pub_len + 1);
BIO_read(pri, pri_key, pri_len);
BIO_read(pub, pub_key, pub_len);
pri_key[pri_len] = '\0';
pub_key[pub_len] = '\0';
#ifdef PRINT_KEYS
printf("\n%s\n%s\n", pri_key, pub_key);
#endif
printf("done.\n");

/* 2. save public key using 2 different methods*/

bp_public = BIO_new(BIO_s_mem());
bp_public = BIO_new_file("$db17.srsa.pupem1", "w+");

ret = PEM_write_bio_RSAPublicKey(bp_public, keypair);
if(ret != 1){
goto free_stuff;
}

pu = fopen("$db17.srsa.pupem2", "w+");
PEM_write_RSAPub

 

Regards

Phil

 

 

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.