?
Solved

Rijndael Encryption and Classic ASP with DocStoc

Posted on 2010-01-07
1
Medium Priority
?
1,554 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:jfergy
1 Comment
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 26203377
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This paper addresses the security of Sennheiser DECT Contact Center and Office (CC&O) headsets. It describes the DECT security chain comprised of “Pairing”, “Per Call Authentication” and “Encryption”, which are all part of the standard DECT protocol.
Ransomware is a growing menace to anyone using a computer or mobile device. Here are answers to some common questions about this vicious new form of malware.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

621 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