Using CAPICOM.EncryptedData from ASP

IMPORTANT: CAPICOM does not support the PKCS #7 EncryptedData content type but uses a nonstandard ASN structure for EncryptedData. Therefore, only CAPICOM can decrypt a CAPICOM EncryptedData object.

More info about EncryptedData object at MSDN Library.

Output

3DES - Encrypt
--------------
MGoGCSsGAQQBgjdYA6BdMFsGCisGAQQBgjdYAwGgTTBLAgMCAAECAmYDAgIAwAQI
XcVF2ZyiGRMEEAPQjqjc1ler+ufMSHPlVCIEIKnr9oZGrCbT3l3wpcpmoqmULB1Q
4KKXuBykhrQ+Xg5x

3DES - Decrypt
--------------
Hello World αβω

AES (Rijndael) - Encrypt
------------------------
MHIGCSsGAQQBgjdYA6BlMGMGCisGAQQBgjdYAwGgVTBTAgMCAAECAmYQAgIBAAQQ
AAAAAAAAAAAAAAAAAAAAAAQQOGv2sUJ6Jld2vGbme9QxdwQgQoCq/aIcoxYpeLTy
CNLaNuay+1FIkYsjLQO5HY/zcq0=

AES (Rijndael) - Decrypt
------------------------
Hello World αβω

Sources

/CAPICOM/EncryptedData.asp

<%@ language="VBScript" %>
<!--#include virtual="/lib/unit-tests.asp"-->
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link href="/lib/unit-tests.css" rel="stylesheet" type="text/css" />
<h1>Using CAPICOM.EncryptedData from ASP</h1>
<blockquote>
	<strong>IMPORTANT:</strong> CAPICOM does not support the PKCS #7 EncryptedData content type but uses a nonstandard ASN structure for EncryptedData. Therefore, only CAPICOM can decrypt a CAPICOM EncryptedData object.
</blockquote>
<p>
	More info about EncryptedData object at <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa382010(v=vs.85).aspx">MSDN Library</a>.
</p>
<h2>Output</h2>
<div class="code"><pre><%
 
'CAPICOM_ENCRYPTION_ALGORITHM enumeration <https://msdn.microsoft.com/en-us/library/windows/desktop/aa375675(v=vs.85).aspx>
const CAPICOM_ENCRYPTION_ALGORITHM_RC2  = 0
const CAPICOM_ENCRYPTION_ALGORITHM_RC4  = 1
const CAPICOM_ENCRYPTION_ALGORITHM_DES  = 2
const CAPICOM_ENCRYPTION_ALGORITHM_3DES = 3
const CAPICOM_ENCRYPTION_ALGORITHM_AES  = 4
 
'CAPICOM_ENCRYPTION_KEY_LENGTH enumeration <https://msdn.microsoft.com/en-us/library/windows/desktop/aa375683(v=vs.85).aspx>
const CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM  = 0
const CAPICOM_ENCRYPTION_KEY_LENGTH_40_BITS  = 1
const CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS  = 2
const CAPICOM_ENCRYPTION_KEY_LENGTH_128_BITS = 3
const CAPICOM_ENCRYPTION_KEY_LENGTH_192_BITS = 4
const CAPICOM_ENCRYPTION_KEY_LENGTH_256_BITS = 5
 
'CAPICOM_ENCODING_TYPE enumeration <https://msdn.microsoft.com/en-us/library/windows/desktop/aa375673(v=vs.85).aspx>
const CAPICOM_ENCODE_BASE64 = 0
const CAPICOM_ENCODE_BINARY = 1
 
const MSG = "Hello World αβω"
const PWD = "password"
 
dim base64
 
with Server.createObject("CAPICOM.EncryptedData")
	.Algorithm = CAPICOM_ENCRYPTION_ALGORITHM_3DES
	.Algorithm.KeyLength = CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM
	.setSecret PWD
	.content = MSG
 
	base64 = .Encrypt(CAPICOM_ENCODE_BASE64)
 
	Response.write "3DES - Encrypt" & vbNewline
	Response.write "--------------" & vbNewline
	Response.write base64
	Response.write vbNewline
end with
 
with Server.createObject("CAPICOM.EncryptedData")
	.Algorithm = CAPICOM_ENCRYPTION_ALGORITHM_3DES
	.Algorithm.KeyLength = CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM
	.setSecret PWD
	.Decrypt(base64)
 
	Response.write "3DES - Decrypt" & vbNewline
	Response.write "--------------" & vbNewline
	Response.write .content
	Response.write vbNewline
end with
 
Response.write vbNewline
 
with Server.createObject("CAPICOM.EncryptedData")
	.Algorithm = CAPICOM_ENCRYPTION_ALGORITHM_AES
	.Algorithm.KeyLength = CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM
	.setSecret PWD
	.content = MSG
 
	base64 = .Encrypt(CAPICOM_ENCODE_BASE64)
 
	Response.write "AES (Rijndael) - Encrypt" & vbNewline
	Response.write "------------------------" & vbNewline
	Response.write base64
	Response.write vbNewline
end with
 
with Server.createObject("CAPICOM.EncryptedData")
	.Algorithm = CAPICOM_ENCRYPTION_ALGORITHM_AES
	.Algorithm.KeyLength = CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM
	.setSecret PWD
	.Decrypt(base64)
 
	Response.write "AES (Rijndael) - Decrypt" & vbNewline
	Response.write "------------------------" & vbNewline
	Response.write .content
	Response.write vbNewline
end with
 
%></pre></div>
<hr />
<h2>Sources</h2>
<h3><%= Request.ServerVariables("SCRIPT_NAME") %></h3>
<div class="code"><%= geshify( loadTextFile( Server.mapPath( Request.ServerVariables("SCRIPT_NAME") ) ), "asp" ) %></div>