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
(Tcl) RSA Signature/Verify with .key and .cerSee more RSA ExamplesDemonstrates how to use a .key file (private key) and digital certificate (.cer, public key) to create and verify an RSA signature.
load ./chilkat.dll # This example assumes the Chilkat API to have been previously unlocked. # See Global Unlock Sample for sample code. set privKey [new_CkPrivateKey] # Load the private key from an RSA .key file: set success [CkPrivateKey_LoadPemFile $privKey "privateKey.key"] if {$success != 1} then { puts [CkPrivateKey_lastErrorText $privKey] delete_CkPrivateKey $privKey exit } # Get the private key in XML format: set privKeyXml [CkPrivateKey_getXml $privKey] set rsa [new_CkRsa] # Import the private key into the RSA component: set success [CkRsa_ImportPrivateKey $rsa $privKeyXml] if {$success != 1} then { puts [CkRsa_lastErrorText $rsa] delete_CkPrivateKey $privKey delete_CkRsa $rsa exit } # Create the signature as a hex string: CkRsa_put_EncodingMode $rsa "hex" set strData "This is the string to be signed." # Sign the string using the sha-1 hash algorithm. # Other valid choices are "md2", "sha256", "sha384", # "sha512", and "md5". set hexSig [CkRsa_signStringENC $rsa $strData "sha-1"] puts "$hexSig" # Load a digital certificate from a .cer file: set cert [new_CkCert] set success [CkCert_LoadFromFile $cert "myCert.cer"] if {$success != 1} then { puts [CkCert_lastErrorText $cert] delete_CkPrivateKey $privKey delete_CkRsa $rsa delete_CkCert $cert exit } # pubKey is a CkPublicKey set pubKey [CkCert_ExportPublicKey $cert] # Now verify using a separate instance of the RSA object: set rsa2 [new_CkRsa] # Import the public key into the RSA object: set success [CkRsa_ImportPublicKey $rsa2 [CkPublicKey_getXml $pubKey]] if {$success != 1} then { puts [CkRsa_lastErrorText $rsa2] delete_CkPrivateKey $privKey delete_CkRsa $rsa delete_CkCert $cert delete_CkRsa $rsa2 exit } delete_CkPublicKey $pubKey # The signature is a hex string, so make sure the EncodingMode is correct: CkRsa_put_EncodingMode $rsa2 "hex" # Verify the signature: set success [CkRsa_VerifyStringENC $rsa2 $strData "sha-1" $hexSig] if {$success != 1} then { puts [CkRsa_lastErrorText $rsa2] delete_CkPrivateKey $privKey delete_CkRsa $rsa delete_CkCert $cert delete_CkRsa $rsa2 exit } puts "Success." delete_CkPrivateKey $privKey delete_CkRsa $rsa delete_CkCert $cert delete_CkRsa $rsa2 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.