AutoIt
AutoIt
RSA Sign with PKCS8 Encrypted Key
See more RSA Examples
Demonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it).Chilkat AutoIt Downloads
Local $bSuccess = False
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
$oPrivKey = ObjCreate("Chilkat.PrivateKey")
; Load the private key from an RSA PEM file:
$bSuccess = $oPrivKey.LoadAnyFormatFile("raul_privateKey.key","a0123456789")
If ($bSuccess = False) Then
ConsoleWrite($oPrivKey.LastErrorText & @CRLF)
Exit
EndIf
$oRsa = ObjCreate("Chilkat.Rsa")
; Import the private key into the RSA component:
$bSuccess = $oRsa.UsePrivateKey($oPrivKey)
If ($bSuccess = False) Then
ConsoleWrite($oRsa.LastErrorText & @CRLF)
Exit
EndIf
; This example will sign a string, and receive the signature
; in a hex-encoded string. Therefore, set the encoding mode
; to "hex":
$oRsa.EncodingMode = "hex"
Local $strData = "This is the string to be signed."
; Sign the string using the sha256 hash algorithm.
; Other valid choices are sha1, sha384, sha512 and others.
Local $sHexSig = $oRsa.SignStringENC($strData,"sha256")
If ($oRsa.LastMethodSuccess = False) Then
ConsoleWrite($oRsa.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite($sHexSig & @CRLF)
; Now verify with the public key.
; This example shows how to use the public key from
; a digital certificate (.cer file)
$oCert = ObjCreate("Chilkat.Cert")
$bSuccess = $oCert.LoadFromFile("raul_publicKey.cer")
If ($bSuccess = False) Then
ConsoleWrite($oCert.LastErrorText & @CRLF)
Exit
EndIf
$oPubKey = ObjCreate("Chilkat.PublicKey")
$oCert.GetPublicKey($oPubKey)
$oRsa2 = ObjCreate("Chilkat.Rsa")
$bSuccess = $oRsa2.UsePublicKey($oPubKey)
If ($bSuccess = False) Then
ConsoleWrite($oRsa2.LastErrorText & @CRLF)
Exit
EndIf
; Verify the signature against the original data:
$oRsa2.EncodingMode = "hex"
$bSuccess = $oRsa2.VerifyStringENC($strData,"sha256",$sHexSig)
If ($bSuccess = False) Then
ConsoleWrite($oRsa2.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Signature verified!" & @CRLF)
; Verify with incorrect data:
$bSuccess = $oRsa2.VerifyStringENC("something else","sha256",$sHexSig)
If ($bSuccess <> True) Then
ConsoleWrite("Signature not verified! (which was expected in this case)" & @CRLF)
Else
ConsoleWrite("Hmmm... that's not right..." & @CRLF)
EndIf