Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Create and Verify a Digital Signature for a File This VB.NET sample code loads a file and creates a digitial signature. It then verifies the digital signature. ' This example uses the Chilkat .NET encryption library, which can be downloaded
' at http://www.chilkatsoft.com/downloads.asp
' Create an instance of the Chilkat encryption class.
Dim success As Boolean
Dim crypt As New Chilkat.Crypt2()
' Any code begins the 30-day trial.
crypt.UnlockComponent("30-day-trial")
' Load the file to be signed into a byte array
Dim oFile As System.IO.FileInfo
oFile = New System.IO.FileInfo("fileToSign.dat")
Dim oFileStream As System.IO.FileStream = oFile.OpenRead()
Dim lBytes As Long = oFileStream.Length
Dim fileData(lBytes) As Byte
oFileStream.Read(fileData, 0, lBytes)
oFileStream.Close()
' Load a certificate from a .cer file
' There are many other ways of loading a certificate...
Dim cert As New Chilkat.Cert()
success = cert.LoadFromFile("myCert.cer")
If (Not success) Then
MsgBox(cert.LastErrorText)
Exit Sub
End If
' Tell the crypt object to use the certificate.
crypt.SetSigningCert(cert)
' Create a digital signature based on the file content.
Dim signatureBytes() As Byte
signatureBytes = crypt.SignBytes(fileData)
If (signatureBytes.Length = 0) Then
MsgBox(crypt.LastErrorText)
Exit Sub
End If
' Save the detached signature to a file.
oFileStream = New System.IO.FileStream("signature.dat", System.IO.FileMode.Create)
oFileStream.Write(signatureBytes, 0, signatureBytes.Length - 1)
oFileStream.Close()
MsgBox("Digital signature created!")
' Now verify the signature.
Dim verified As Boolean
verified = crypt.VerifyBytes(fileData, signatureBytes)
If Not verified Then
MsgBox(crypt.LastErrorText)
Else
MsgBox("Signature verified!")
End If
Important: The download for this
example does not contain the ChilkatDotNet.dll which |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2007 Chilkat Software, Inc. All Rights Reserved.