login about faq
0
1

I have the latest one month trial chilkat dll version file for encryption. I want to encrypt the message in RSA 2048 where public/private(generated in some other tool) keys are given to me and kept in a file like 'a.txt' or 'a' or 'a.key' (This file is not snk file), this file consist the key in the following way:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCIkvYpB+1YiF7pkxzPfT0DTW77R0fE5sa/d2aon47B/A/ng5xrf81OFx+XrnwQhUcxuozcRXd0cCdYSBn0gEiUl2LtDmrAzejnvs/R3kIF/pKnGRpWjG7lkZi+Vbdf+JpDfBCY6or/4Dr/DLnmFih6zEGmJgypErajsLQiQGoY8wIDAQAB

I took this key and made it 'a.pem' file manually and set the public key as per pem file format. I used this file pem file and successfully encrypted the message using RSA. I do not want to this manual conversion. I just want to take the public key from a file or a field from database and use it straight a way without any conversion.

Please let me know how can I achieve this.

I commented my older code and implement the code as per your suggestions. Following is my code:

Dim rsa2 As New ChilkatRsa
Dim pubKey2 As New PublicKey

'  For brevity, don't check the success..
Dim pub As String
'pub = "C:\Documents and Settings\TGL\Desktop\MyTest\PublicKey.pem"
pub = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAm9D3cMDWiyklw7XaiF3iLZmeKHj/+TIjXiBV2VcmyY80a7QhX6yczFrCbr7tU7mypDsm+TWb7yCNjLLDpoT++abQFElzLXVjm3L767cc8FLwfxt4q4uiQGUKTcbRLC4EwLDMSOGB23nz73NXcZC2lbWTq+OYcBEj2kxP32rT6QIDAQAB"
'success = pubKey2.LoadOpenSslPemFile(pub)
success = pubKey2.LoadOpenSslPem(pub)

'success = pubKey2.LoadPkcs1Pem("C:\Documents and Settings\TGL\Desktop\MyTest\PublicKey.pem")
'pubKeyXml = pubKey2.GetXml()
'success = rsa2.ImportPublicKey(pubKeyXml)
success = rsa2.ImportPublicKeyObj(pubKey2)

' RSA OaepPadding
rsa2.OaepPadding = 0
'  RSA encrypt the AES key and return it as a base64 encoded string.
rsa2.EncodingMode = "base64"
Dim bUsePrivateKey As Long
bUsePrivateKey = 0
Dim encryptedAesKey As String
Dim hexSig As String

encryptedAesKey = rsa2.EncryptStringENC(randomKey, bUsePrivateKey)
txtEncAESKey.Text = txtEncAESKey.Text & encryptedAesKey

But the LoadOpenSslPem function failed, please correct me if I did any mistake.

asked Nov 12 '14 at 02:36

Amit%20Raj's gravatar image

Amit Raj
1144

edited Dec 11 '14 at 01:33

I have edited my question.

(Nov 19 '14 at 08:29) Amit Raj

Examine the contents of the LastErrorText property for the object whose method call failed. (And make sure to check the return value of each method call for success/failure. There is no point in continuing onward if a method call failed. For example, there's no point in trying to encrypt if the call to pubKey2.LoadOpenSslPem failed.)

link

answered Dec 11 '14 at 07:47

chilkat's gravatar image

chilkat ♦♦
11.8k316358420

ChilkatLog: EncryptStringENC: DllDate: Aug 1 2014 ChilkatVersion: 9.5.0.43 UnlockPrefix: Anything for 30-day trial Username: TGL-DA5A20427C0:TGL Architecture: Little Endian; 32-bit Language: ActiveX VerboseLogging: 0 Warning: IV length is less than the algorithm's block size blockSize: 16 IV_len: 0 Success. --EncryptStringENC --ChilkatLog

Same code is working fine with when I took the public key from manually created pem file. I got the above error in the LoadOpenSslPem function.

(Dec 11 '14 at 08:06) Amit Raj

There is no IV in RSA encryption. I suspect your LastErrorText is from the Crypt2 object's call to its EncryptStringENC, and not the rsa2 object's call to EncryptStringENC?

(Dec 11 '14 at 08:09) chilkat ♦♦

Yes, you are right. ChilkatLog: LoadOpenSslPem: DllDate: Aug 1 2014 ChilkatVersion: 9.5.0.43 UnlockPrefix: Anything for 30-day trial Username: TGL-DA5A20427C0:TGL Architecture: Little Endian; 32-bit Language: ActiveX VerboseLogging: 0 pem: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAm9D3cMDWiyklw7XaiF3iLZmeKHj/+T Unrecognized PEM format Failed. --LoadOpenSslPem --ChilkatLog

Please let me know how can I convert the key(string) in PEM format.

(Dec 11 '14 at 08:18) Amit Raj

I understand now... What you have is not PEM. You have the Base64 encoded representation of the RSA key in DER format. There are methods for loading from DER (LoadRsaDer and LoadOpenSslDer) but these accept the binary DER. You can use Crypt2 to decode from Base64 (by calling the Crypt2.Decode method, where the 1st argument is the encoded string (i.e. your Base64 RSA public key) and the 2nd argument indicates the encoding (i.e. "base64"). Pass the return value of Crypt2.Decode directly to LoadRsaDer:

success = rsa2.LoadRsaDer(crypt.Decode(pub,"base64"))

(Dec 11 '14 at 08:41) chilkat ♦♦

I used .LoadRsaDer function but the code showed me invalid function error.

(Dec 11 '14 at 08:45) Amit Raj

It was a typo on my part -- the LoadRsaDer method is a PublicKey method, not an Rsa method...

(Dec 11 '14 at 08:47) chilkat ♦♦

I used the following the following line: success = pubKey2.LoadRsaDer(crypt.Decode(pub, "base64")) I have done the encryption part.

(Dec 11 '14 at 08:54) Amit Raj
showing 5 of 7 show all

Load the PEM string into a Chilkat.PublicKey object (or CkPublicKey depending on the programming language), by calling LoadOpenSslPem (see http://www.chilkatsoft.com/refdoc/csPublicKeyRef.html )

Then make the PublicKey object available to the Rsa object via the ImportPublicKeyObj (see http://www.chilkatsoft.com/refdoc/csRsaRef.html)

link

answered Nov 14 '14 at 11:57

chilkat's gravatar image

chilkat ♦♦
11.8k316358420

edited Dec 10 '14 at 13:02

I have edited my question.

(Dec 11 '14 at 01:40) Amit Raj

I used the following the following line: success = pubKey2.LoadRsaDer(crypt.Decode(pub, "base64")) I have done the encryption part.

link

answered Dec 12 '14 at 00:37

Amit%20Raj's gravatar image

Amit Raj
1144

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×64
×47

Asked: Nov 12 '14 at 02:36

Seen: 1,250 times

Last updated: Dec 12 '14 at 00:37

powered by OSQA