PureBasic
PureBasic
Load PEM Public/Private Key into RSA Object
See more RSA Examples
Demonstrates how to load a PEM key into the Chilkat RSA object.Chilkat PureBasic Downloads
IncludeFile "CkPublicKey.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkRsa.pb"
Procedure ChilkatExample()
success.i = 0
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
rsa.i = CkRsa::ckCreate()
If rsa.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; First demonstrate importing a PEM public key:
publicKeyPem.s = "PEM public-key data goes here"
pubkey.i = CkPublicKey::ckCreate()
If pubkey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPublicKey::ckLoadFromString(pubkey,publicKeyPem)
If success = 0
Debug CkPublicKey::ckLastErrorText(pubkey)
CkRsa::ckDispose(rsa)
CkPublicKey::ckDispose(pubkey)
ProcedureReturn
EndIf
success = CkRsa::ckUsePublicKey(rsa,pubkey)
If success = 0
Debug CkRsa::ckLastErrorText(rsa)
CkRsa::ckDispose(rsa)
CkPublicKey::ckDispose(pubkey)
ProcedureReturn
EndIf
; Demonstrate importing a PEM private key:
privateKeyPem.s = "PEM private-key data goes here"
privkey.i = CkPrivateKey::ckCreate()
If privkey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; If the private key PEM is protected with a password, then
; call LoadEncryptedPem. Otherwise call LoadPem.
success = CkPrivateKey::ckLoadPem(privkey,privateKeyPem)
If success = 0
Debug CkPrivateKey::ckLastErrorText(privkey)
CkRsa::ckDispose(rsa)
CkPublicKey::ckDispose(pubkey)
CkPrivateKey::ckDispose(privkey)
ProcedureReturn
EndIf
success = CkRsa::ckUsePrivateKey(rsa,privkey)
If success = 0
Debug CkRsa::ckLastErrorText(rsa)
CkRsa::ckDispose(rsa)
CkPublicKey::ckDispose(pubkey)
CkPrivateKey::ckDispose(privkey)
ProcedureReturn
EndIf
Debug "OK!"
CkRsa::ckDispose(rsa)
CkPublicKey::ckDispose(pubkey)
CkPrivateKey::ckDispose(privkey)
ProcedureReturn
EndProcedure