Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
Public-Key Encryption / Decryption using Certificate This ASP script shows how to use a digital certificate for encryption and decryption. <html>
<head>
<title>ASP to Public-Key Encrypt / Decrypt a String</title>
</head>
<body>
<%
' First, locate a certificate to use for public-key encryption.
' (There are many ways to instantiate a digital certificate.
' Please review the digital certificate examples for more information.)
' Get an object to create a certificate store object
set ccs = Server.CreateObject("ChilkatCertificate.ChilkatCreateCS")
' Open the local machine certificate store.
set certStore = ccs.OpenLocalSystemStore()
' Find the certificate matching the email address "matt@chilkatsoft.com"
set cert = certStore.FindCertBySubjectE("matt@chilkatsoft.com")
if (cert is Nothing) then
Response.write "Certificate not found!"
else
Response.write "Using certificate: " & cert.SubjectDN & "<br>"
' Create a Chilkat encryption instance to do the public-key encryption.
set crypt = Server.CreateObject("ChilkatCrypt2.ChilkatCrypt2")
' Any value passed to UnlockComponent begins the 30-day trial.
unlocked = crypt.UnlockComponent("30-day trial")
if unlocked then
' Use the digital certificate...
crypt.SetEncryptCert cert
crypt.CryptAlgorithm = "pki"
stringToEncrypt = "This is a test"
' Public-key encrypt the string and
' convert the encrypted byte data to a hexidecimalized string.
crypt.EncodingMode = "hex"
encryptedString = crypt.EncryptStringENC(stringToEncrypt)
Response.write "(1) Encrypted Data:<br>" & encryptedString & "<br><br>"
' Display the details of the encryption
' Notice that we probably used 40-bit RC2 encryption.
' Next we will use the Chilkat CSP object to select stronger encryption.
Response.write crypt.LastErrorHtml & "<br><br>"
' Select the enhanced Microsoft provider with 3DES
set csp = Server.CreateObject("ChilkatUtil.ChilkatCSP")
csp.SetProviderMicrosoftEnhanced
' Select 168-bit 3DES encryption
csp.SetEncryptAlgorithm "3DES"
crypt.SetCSP(csp)
' Now re-do the public-key encryption.
encryptedString = crypt.EncryptStringENC(stringToEncrypt)
Response.write "(2) Encrypted Data:<br>" & encryptedString & "<br><br>"
' Display the details of the encryption
Response.write crypt.LastErrorHtml & "<br><br>"
' Now decrypt it...
' Decrypting requires the private key to be installed on the computer
' and accessible from ASP.
decryptedString = crypt.DecryptStringENC(encryptedString)
Response.write "Decrypted: [" & decryptedString & "]<br><br>"
' Show the details of the decryption.
Response.write crypt.LastErrorHtml & "<br><br>"
end if
end if
%>
</body>
</html>
|
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2008 Chilkat Software, Inc. All Rights Reserved.