9.2.1 Encryption
The CryptoMessage object enables you to encrypt text information directly with a
certificate's public key in one easy step. The resultant PKCS#7 encrypted message
(also known as "envelope") can only be decrypted with a private key context
associated with this certificate. Decryption can take place on the client's
machine using the XEncrypt ActiveX control, or on the server.
PKCS#7 envelopes also allow you to encrypt data with multiple certificates at the same time.
Any one of the corresponding private keys is sufficient to decrypt the message.
The following code sample demonstrates how to use the CryptoMessage object to
encrypt a text string with a certificate. For this code sample to work,
you must export your personal certificate to a .cer file and place it on the server.
VBScript |
Set CM = Server.CreateObject("Persits.CryptoManager")
Set Context = CM.OpenContext( "", True )
Set Msg = Context.CreateMessage( True )
' Obtain encryption certificate
Set Cert = CM.ImportCertFromFile("c:\path\mycert.cer")
Msg.AddRecipientCert Cert
Encrypted = Msg.EncryptText("my secret phrase") |
C# |
ICryptoManager objCM = new CryptoManager();
ICryptoContext objContext = objCM.OpenContext( "", true, Missing.Value );
ICryptoMessage objMsg = objContext.CreateMessage( true );
// Obtain encryption certificate
ICryptoCert objCert = objCM.ImportCertFromFile( @"c:\path\mycert.cer" );
objMsg.AddRecipientCert( objCert );
txtResult.Text = objMsg.EncryptText("my secret phrase"); |
The CryptoMessage method EncryptText performs encryption on the specified text string and
returns a PKCS#7 envelope which may look similar to the following:
MIICNwYJKoZIhvcNAQcDoIICKDCCAiQCAQAxggHgMIIB3AIBADCBwzCBrjELMAkGA1UEBhMCVVMx
CzAJBgNVBAgTAlVUMRcwFQYDVQQHEw5TYWx0IExha2UgQ2l0eTEeMBwGA1UEChMVVGhlIFVTRVJU
UlVTVCBOZXR3b3JrMSEwHwYDVQQLExhodHRwOi8vd3d3LnVzZXJ0cnVzdC5jb20xNjA0BgNVBAMT
LVVUTi1VU0VSRmlyc3QtQ2xpZW50IEF1dGhlbnRpY2F0aW9uIGFuZCBFbWFpbAIQdIendmufMn2Q
RDFiCVP1BTANBgkqhkiG9w0BAQEFAASCAQAb+i9wpStZgR3TfWb+NHg+FduZFQVpCrFaityTREMQ
KB9A+OLBURVStO7RIPODdwvZ7uN3/69tLBrMwckvzXizwiBgLNGE9qtToulaDBIwdxqesN/IU9bU
HcukqUUFtRwS3P6ECsZ+7xuf64QwpouaxiMa4xoaOdEi/CRAxF/sfz2X6D2pCNN7qEy9EVdmjeue
9RgPvmQxQbI7CCP8aixnbdOMw+q9pMAlbqenusxJx6Hot4dTM3wlyG6mXA5mhH1a5PM66vhnuSCe
zy59XXv+KRbZ9cB6v91pKcp7Gj8UfJoYPEDDLJphMl0+UaH9U/0SLINKExt3CcB43buZCH5jMDsG
CSqGSIb3DQEHATAUBggqhkiG9w0DBwQIT1IO/rf63iWAGPAcGb+oHf3fnQD5butjI49SZZjVOcUm
wc==
Even a short string turns into a relatively large encrypted blob because a PKCS#7
envelope also contains the certificate used for encryption.
Before running this code sample, you need to export your personal certificate to a file
and change the path argument to ImportCertFromFile, if necessary. Click on the links below to run this code sample.
http://localhost/aspencrypt/manual_09/09_encrypt.asp
http://localhost/aspencrypt/manual_09/09_encrypt.aspx
9.2.1 Decryption
To decrypt a PKCS#7 envelope, you need to call the CryptoMessage method DecryptText
and pass it, besides the blob, the name of the private key container for
the encryption certificate. This name can be obtained via the expression
Cert.PrivateKeyContext.ContainerName. If no container name is specified,
AspEncrypt will try all certificates in the MY store until a match is found.
The following code snippet is to be executed on the user's machine using the XEncrypt ActiveX control:
Client-Side VBScript |
Sub Decrypt
Set Context = XEncrypt.OpenContext( "mycontainer", False)
Set Msg = Context.CreateMessage(True)
On Error Resume Next
document.myForm.txtDecr.Value = _
Msg.DecryptText(document.myForm.txtEncr.Value, "")
If Err <> 0 Then
MsgBox Err.Description
End If
End Sub |
Client-Side JavaScript |
function Decrypt()
{
var Context = XEncrypt.OpenContext( "mycontainer", false);
var Msg = Context.CreateMessage(true);
try
{
document.forms[0].txtDecr.value =
Msg.DecryptText(document.forms[0].txtEncr.value, "")
}
catch( e )
{
alert( e.description );
}
} |
Click on the links below to run this code sample.
http://localhost/aspencrypt/manual_09/09_decrypt_vb.htm
http://localhost/aspencrypt/manual_09/09_decrypt_js.htm