Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(AutoIt) RSA Sign with PKCS8 Encrypted KeyDemonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it).
; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. $oPkey = ObjCreate("Chilkat.PrivateKey") ; Load the private key from an RSA PEM file: Local $bSuccess = $oPkey.LoadPkcs8EncryptedFile("raul_privateKey.key","a0123456789") Local $sPkeyXml ; Get the private key in XML format: $sPkeyXml = $oPkey.GetXml() $oRsa = ObjCreate("Chilkat.Rsa") ; Import the private key into the RSA component: $bSuccess = $oRsa.ImportPrivateKey($sPkeyXml) If ($bSuccess <> True) 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 sha-1 hash algorithm. ; Other valid choices are "md2" and "md5". Local $sHexSig = $oRsa.SignStringENC($strData,"sha-1") 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 <> True) Then ConsoleWrite($oCert.LastErrorText & @CRLF) Exit EndIf Local $oPubKey $oPubKey = $oCert.ExportPublicKey() Local $sPubKeyXml ; Get the private key in XML format: $sPubKeyXml = $oPubKey.GetXml() $oRsa2 = ObjCreate("Chilkat.Rsa") $bSuccess = $oRsa2.ImportPublicKey($sPubKeyXml) If ($bSuccess <> True) Then ConsoleWrite($oRsa2.LastErrorText & @CRLF) Exit EndIf ; Verify the signature against the original data: $oRsa2.EncodingMode = "hex" $bSuccess = $oRsa2.VerifyStringENC($strData,"sha-1",$sHexSig) If ($bSuccess <> True) Then ConsoleWrite($oRsa2.LastErrorText & @CRLF) Exit EndIf ConsoleWrite("Signature verified!" & @CRLF) ; Verify with incorrect data: $bSuccess = $oRsa2.VerifyStringENC("something else","sha-1",$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 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.