Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(Excel) RSASSA-PSS Sign Binary DataSigns binary data to create a PKCS7/CMS signature. The signature algorithm is RSASSA-PSS with SHA256. Note: This example requires Chilkat v9.5.0.67 or greater.
' This example requires the Chilkat Crypt API to have been previously unlocked. ' See Unlock Chilkat Crypt for sample code. Dim crypt As Chilkat.Crypt2 Set crypt = Chilkat.NewCrypt2 ' Get a digital certificate with private key from a .pfx ' (Chilkat has many different ways to provide a cert + private key for siging. ' Using a PFX is just one possible option.) Dim pfx As Chilkat.Pfx Set pfx = Chilkat.NewPfx success = pfx.LoadPfxFile("qa_data/rsassa-pss/privatekey.pfx","PFX_PASSWORD") If (success <> True) Then Debug.Print pfx.LastErrorText Exit Sub End If ' Get the certificate to be used for signing. ' (The typical case for a PFX is that it contains a cert with an associated private key, ' as well as other certificates in the chain of authentication. The cert with the private ' key should be in the first position at index 0.) Set cert = pfx.GetCert(0) If (pfx.LastMethodSuccess <> True) Then Debug.Print pfx.LastErrorText Exit Sub End If Dim success As Boolean success = crypt.SetSigningCert(cert) ' Indicate that RSASSA-PSS with SHA256 should be used. crypt.SigningAlg = "pss" crypt.HashAlgorithm = "sha256" crypt.EncodingMode = "base64_mime" ' Load a binary file to be signed: Dim binaryData As Chilkat.BinData Set binaryData = Chilkat.NewBinData success = binaryData.LoadFile("qa_data/jpg/starfish20.jpg") If (success <> True) Then Debug.Print "Failed to load file." Exit Sub End If ' Sign the binary bytes to get a PKCS7 detached signature in base64 format: pkcs7sig = crypt.SignBdENC(binaryData) Debug.Print "Detached PCKS7 Signature:" Debug.Print pkcs7s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Rlcm5ldHdpZGdldHMuY29tAgkAw+wkJPXVyeIwPQYJKoZIhvcNAQEKMDCgDTAL ' BglghkgBZQMEAgGhGjAYBgkqhkiG9w0BAQgwCwYJYIZIAWUDBAIBogMCASAEgYCWV0g82volvnwf ' YpwIpqpQzMmTPBKNQmFGjbyH2opdcbJwgu2qEFvaXkyjYDtgQ7XsCqc15dm6Ee1Ujkosbp57kLTt ' /WbwxY1CC/uxs3oV+5ESUyB+2iocTYABYn4ye0FhBPut86n/gzZTL+RLG6Z1fxwwzkoxWUp7GjKK ' 58mveQ== ' The ASN.1 of the signature can be examined by browsing to https://lapo.it/asn1js/ , ' then copy-and-paste the Base64 signature into the form and decode.. ' The signature can be verified against the original data like this: success = crypt.VerifyBdENC(binaryData,pkcs7sig) Debug.Print "Signature verified: "; success ' Now we'll create an opaque signature (the opposite of a detached signature). ' An opaque signature is a PKCS7/CMS message that contains both the original data and ' the signature. The verification process extracts the original data. ' Then OpaqueSignBd method in-place signs the binaryData. ' The contents of binaryData are replaced with the CMS/PKCS7 message. success = crypt.OpaqueSignBd(binaryData) ' Show the contents of the opaque signature in base64 format: Debug.Print "Opaque Signature:" Debug.Print binaryData.GetEncoded("base64_mime") ' MIIKCgYJKoZIhvcNAQcCoIIJ+zCCCfcCAQExDzANBglghkgBZQMEAgEFADCCAywGCSqGSIb3DQEH ' AaCCAx0EggMZ/9j/4AAQSkZJRgABAQEASABIAAD//gAmRmlsZSB3cml0dGVuIGJ5IEFkb2JlIFBo ' b3Rvc2hvcD8gNC4w/9sAQwAQCwwODAoQDg0OEhEQExgoGhgWFhgxIyUdKDozPTw5Mzg3QEhcTkBE ' V0U3OFBtUVdfYmdoZz5NcXlwZHhcZWdj/9sAQwEREhIYFRgvGhovY0I4QmNjY2NjY2NjY2NjY2Nj ' Y2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2Nj/8IAEQgAFAAUAwERAAIRAQMRAf/E ' ABcAAAMBAAAAAAAAAAAAAAA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EwMjZaMC8GCSqGSIb3 ' DQEJBDEiBCCuNRCSgx4FhSGYMaM+DXjtbJsXTDYsBjoX4iHqodQwHzBfBgkqhkiG9w0BCQ8xUjBQ ' MAsGCWCGSAFlAwQBAjAKBggqhkiG9w0DBzAOBggqhkiG9w0DAgICAIAwDQYIKoZIhvcNAwICAUAw ' BwYFKw4DAgcwDQYIKoZIhvcNAwICASgwgbEGCSsGAQQBgjcQBDGBozCBoDCBkjELMAkGA1UEBhMC ' QVUxETAPBgNVBAgMCFZpY3RvcmlhMRIwEAYDVQQHDAlNZWxib3VybmUxITAfBgNVBAoMGEludGVy ' bmV0IFdpZGdpdHMgUHR5IEx0ZDEPMA0GA1UEAwwGV2lkZ2V0MSgwJgYJKoZIhvcNAQkBFhlhZG1p ' bkBpbnRlcm5ldHdpZGdldHMuY29tAgkAw+wkJPXVyeIwgbMGCyqGSIb3DQEJEAILMYGjoIGgMIGS ' MQswCQYDVQQGEwJBVTERMA8GA1UECAwIVmljdG9yaWExEjAQBgNVBAcMCU1lbGJvdXJuZTEhMB8G ' A1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMQ8wDQYDVQQDDAZXaWRnZXQxKDAmBgkqhkiG ' 9w0BCQEWGWFkbWluQGludGVybmV0d2lkZ2V0cy5jb20CCQDD7CQk9dXJ4jA9BgkqhkiG9w0BAQow ' MKANMAsGCWCGSAFlAwQCAaEaMBgGCSqGSIb3DQEBCDALBglghkgBZQMEAgGiAwIBIASBgAGVtpI5 ' slxfw+1EyJK4jqxokLvUrqksBLotv1vaP4QaSeF2A1lNrsPfJoEjZJpD1F6vXrFPsR4sPD+6n7P/ ' lz3sGoFykTjE2rPwKEFIbzfxD3gSZKJPWFgDa19DojarmwJMkSPXt9TQEzdjDPrbsCGLYOy29Puq ' ZDI1rUcyxg7Y ' The ASN.1 of the signature can be examined by browsing to https://lapo.it/asn1js/ , ' then copy-and-paste the Base64 signature into the form and decode.. ' The signature is verified, and the original data restored like this: success = crypt.OpaqueVerifyBd(binaryData) If (success <> True) Then Debug.Print "Signature verification failed." Debug.Print crypt.LastErrorText Exit Sub End If ' Save the extracted data to a file: success = binaryData.WriteFile("qa_output/extractedStarfish20.jpg") Debug.Print "Signature verified." |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.