Classic ASP
Classic ASP
RSA Sign with PKCS8 Encrypted Key
See more RSA Examples
Demonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it).Chilkat Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
' This example assumes the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.
set privKey = Server.CreateObject("Chilkat.PrivateKey")
' Load the private key from an RSA PEM file:
success = privKey.LoadAnyFormatFile("raul_privateKey.key","a0123456789")
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( privKey.LastErrorText) & "</pre>"
Response.End
End If
set rsa = Server.CreateObject("Chilkat.Rsa")
' Import the private key into the RSA component:
success = rsa.UsePrivateKey(privKey)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( rsa.LastErrorText) & "</pre>"
Response.End
End If
' This example will sign a string, and receive the signature
' in a hex-encoded string. Therefore, set the encoding mode
' to "hex":
rsa.EncodingMode = "hex"
strData = "This is the string to be signed."
' Sign the string using the sha256 hash algorithm.
' Other valid choices are sha1, sha384, sha512 and others.
hexSig = rsa.SignStringENC(strData,"sha256")
If (rsa.LastMethodSuccess = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( rsa.LastErrorText) & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( hexSig) & "</pre>"
' Now verify with the public key.
' This example shows how to use the public key from
' a digital certificate (.cer file)
set cert = Server.CreateObject("Chilkat.Cert")
success = cert.LoadFromFile("raul_publicKey.cer")
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( cert.LastErrorText) & "</pre>"
Response.End
End If
set pubKey = Server.CreateObject("Chilkat.PublicKey")
success = cert.GetPublicKey(pubKey)
set rsa2 = Server.CreateObject("Chilkat.Rsa")
success = rsa2.UsePublicKey(pubKey)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( rsa2.LastErrorText) & "</pre>"
Response.End
End If
' Verify the signature against the original data:
rsa2.EncodingMode = "hex"
success = rsa2.VerifyStringENC(strData,"sha256",hexSig)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( rsa2.LastErrorText) & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Signature verified!") & "</pre>"
' Verify with incorrect data:
success = rsa2.VerifyStringENC("something else","sha256",hexSig)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Signature not verified! (which was expected in this case)") & "</pre>"
Else
Response.Write "<pre>" & Server.HTMLEncode( "Hmmm... that's not right...") & "</pre>"
End If
%>
</body>
</html>