Rijndael Encryption and Classic ASP with DocStoc

Posted on 2010-01-07
Last Modified: 2012-05-08

I am looking to create a function that  can encrypt a string to send to DocStoc API. They have examples in many languages, but not in classic ASP. As this is still a classic app I am builing in i would prefer to keep it kosher. I have included the C# example below. If you can help do this in Classic ASP that would be appreciated!
public string Encrypt(string value, string AuthKey, string IV)


	  System.IO.MemoryStream msin = null;

	  System.IO.MemoryStream msout = null;

	  SymmetricAlgorithm sa = SymmetricAlgorithm.Create("Rijndael");

	  ICryptoTransform ct = null;

	  CryptoStream cs = null;

	  byte[] buf = new byte[2049];

	  int nLen;



	  msin = new System.IO.MemoryStream();

	  msout = new System.IO.MemoryStream();

	  sa = SymmetricAlgorithm.Create("Rijndael");

	  sa.BlockSize = sa.LegalBlockSizes[0].MinSize;

	  sa.KeySize = sa.LegalKeySizes[0].MinSize;

	  msin.Write(System.Text.Encoding.Default.GetBytes(value), 0, System.Text.Encoding.Default.GetBytes(value).Length);

	  msin.Position = 0;

    			  sa.IV = System.Text.Encoding.Default.GetBytes(IV);

    			  sa.Key = System.Text.Encoding.Default.GetBytes(AuthKey);

	  ct = sa.CreateEncryptor();

	  cs = new CryptoStream(msout, ct, CryptoStreamMode.Write);

	  nLen = msin.Read(buf, 0, buf.Length);

	  while ((nLen > 0))


				  cs.Write(buf, 0, nLen);

				  nLen = msin.Read(buf, 0, buf.Length);



	  return System.Convert.ToBase64String(msout.ToArray());





	  if ((cs != null))



				  cs = null;


	  if ((ct != null))



				  ct = null;


	  if ((sa != null))



				  sa = null;


	  if ((msin != null))



				  msin = null;


	  if ((msout != null))



				  msout = null;




Open in new window

Question by:jfergy
    1 Comment
    LVL 33

    Accepted Solution

    That example is built specifically for the .Net platform, it cannot simply be re-written for classic ASP (apples and oranges).  However, it would be a fairly straight-forward task to create a .Net library that is exposed via COM and can be consumed by ASP.  Here's a slightly simpler example in VB.Net - it's a console application, but you could just put the MyCrypto class in a DLL project and remove the Main sub.
    Imports System.Security.Cryptography
    Imports System.Text
    Imports System.IO
    Public Class MyCrypto
    	Private Shared ReadOnly key() As Byte = System.Text.Encoding.ASCII.GetBytes("some --- 32 ---- char --- string")
    	Private Shared ReadOnly iv() As Byte = System.Text.Encoding.ASCII.GetBytes("Some Random Text")
    	Public Shared Sub Main(ByVal args() As String)
    		MsgBox(Convert.ToBase64String(Encoding.ASCII.GetBytes("Hello World")))
    		Console.WriteLine("The encrypted value of MySetting is: {0}", My.Settings.MySetting)
    		Console.WriteLine("The DEcrypted valye of MySetting is: {0}", MyCrypto.Decrypt(My.Settings.MySetting))
    		Console.WriteLine(vbCrLf + "Press any key to exit." + vbCrLf)
    	End Sub
    	Public Shared Function Encrypt(ByVal PlainText As String) As String
    		Dim returnValue As StringBuilder = Nothing
    		Dim msEncrypt As MemoryStream = Nothing
    		Dim csEncrypt As CryptoStream = Nothing
    		Dim swEncrypt As StreamWriter = Nothing
    		Dim encrypted As Byte()
    		Dim rm As New RijndaelManaged()
    		rm.Key = key
    		rm.IV = iv
    			Dim encryptor As ICryptoTransform = rm.CreateEncryptor()
    			msEncrypt = New MemoryStream()
    			csEncrypt = New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
    			swEncrypt = New StreamWriter(csEncrypt)
    			If Not swEncrypt Is Nothing Then
    			End If
    			If Not csEncrypt Is Nothing Then
    			End If
    			If Not msEncrypt Is Nothing Then
    			End If
    			If Not rm Is Nothing Then
    			End If
    		End Try
    		returnValue = New StringBuilder()
    		encrypted = msEncrypt.ToArray()
    		For Each b As Byte In encrypted
    		Return returnValue.ToString()
    	End Function
    	Public Shared Function Decrypt(ByVal EncryptedText As String) As String
    		If EncryptedText.Length Mod 2 <> 0 Then
    			Return ""
    		End If
    		Dim encryptedList As New List(Of Byte)
    		For i As Integer = 0 To EncryptedText.Length - 1 Step 2
    			Dim piece As Byte
    			If (Not Byte.TryParse(EncryptedText.Substring(i, 2), System.Globalization.NumberStyles.HexNumber, Nothing, piece)) Then
    				Return ""
    			End If
    		Dim returnValue As String = ""
    		Dim msDecrypt As MemoryStream = Nothing
    		Dim csDecrypt As CryptoStream = Nothing
    		Dim srDecrypt As StreamReader = Nothing
    		Dim rm As New RijndaelManaged
    		rm.Key = key
    		rm.IV = iv
    			Dim decryptor As ICryptoTransform = rm.CreateDecryptor()
    			msDecrypt = New MemoryStream(encryptedList.ToArray())
    			csDecrypt = New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
    			srDecrypt = New StreamReader(csDecrypt)
    			returnValue = srDecrypt.ReadToEnd()
    			Return ""
    				If Not srDecrypt Is Nothing Then
    				End If
    				If Not csDecrypt Is Nothing Then
    				End If
    				If Not msDecrypt Is Nothing Then
    				End If
    				If Not rm Is Nothing Then
    				End If
    			Catch ex As Exception
    			End Try
    		End Try
    		Return returnValue
    	End Function
    End Class

    Open in new window


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    #SSL #TLS #Citrix #HTTPS #PKI #Compliance #Certificate #Encryption #StoreFront #Web Interface #Citrix XenApp
    When the confidentiality and security of your data is a must, trust the highly encrypted cloud fax portfolio used by 12 million businesses worldwide, including nearly half of the Fortune 500.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now