Rijndael Encryption and Classic ASP with DocStoc

Hello-

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;

	  try
	  {
	  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);
	  }
	  cs.FlushFinalBlock();

	  return System.Convert.ToBase64String(msout.ToArray());
	  }
	  finally
	  {
	  //destroy 
	  if ((cs != null))
	  {
				  cs.Close();
				  cs = null;
	  }
	  if ((ct != null))
	  {
				  ct.Dispose();
				  ct = null;
	  }
	  if ((sa != null))
	  {
				  sa.Clear();
				  sa = null;
	  }
	  if ((msin != null))
	  {
				  msin.Close();
				  msin = null;
	  }
	  if ((msout != null))
	  {
				  msout.Close();
				  msout = null;
	  }
	  }
	  }

Open in new window

jfergyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd GerbertIT ConsultantCommented:
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)
		Console.ReadKey()
	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

		Try
			Dim encryptor As ICryptoTransform = rm.CreateEncryptor()
			msEncrypt = New MemoryStream()
			csEncrypt = New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
			swEncrypt = New StreamWriter(csEncrypt)

			swEncrypt.Write(PlainText)
		Finally
			If Not swEncrypt Is Nothing Then
				swEncrypt.Close()
			End If
			If Not csEncrypt Is Nothing Then
				csEncrypt.Close()
			End If
			If Not msEncrypt Is Nothing Then
				msEncrypt.Close()
			End If
			If Not rm Is Nothing Then
				rm.Clear()
			End If
		End Try

		returnValue = New StringBuilder()
		encrypted = msEncrypt.ToArray()
		For Each b As Byte In encrypted
			returnValue.Append(b.ToString("X2"))
		Next

		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 ""
			Else
				encryptedList.Add(piece)
			End If
		Next

		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

		Try
			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()
		Catch
			
			Return ""
		Finally
			Try
				If Not srDecrypt Is Nothing Then
					srDecrypt.Close()
				End If
				If Not csDecrypt Is Nothing Then
					csDecrypt.Close()
				End If
				If Not msDecrypt Is Nothing Then
					msDecrypt.Close()
				End If
				If Not rm Is Nothing Then
					rm.Clear()
				End If
			Catch ex As Exception

			End Try
		End Try

		Return returnValue
	End Function
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.