Question:
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.
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.)
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)
I used the following the following line: success = pubKey2.LoadRsaDer(crypt.Decode(pub, "base64")) I have done the encryption part.