encryption/decryption asp.net

R

Robert Bull

I am looking for an example of encryption/decryption in asp.net. I
want to encrypt a string before I send it to a Web Service, decrypt it
on the Web Service then encrypt the results on the Web Service and
decrypt the results on the web app. Any example using
System.Security.Cryptography namespace (VB not C#, thanks)would be
greatly appreciated. Thanks in advance.

-Rob
 
J

Joe Fallon

Option Explicit On
Option Strict On

Imports System
Imports System.Text
Imports System.IO
Imports System.Security.Cryptography

Public Class myCrypto

Shared Sub New()
End Sub

Public Shared Function EncryptString(ByVal AString As String) As String
If AString = String.Empty Then
Return AString
Else
Dim encryptedData() As Byte
Dim dataStream As MemoryStream

Dim encryptor As ICryptoTransform
encryptor = mProvider.CreateEncryptor()

Try
dataStream = New MemoryStream

Dim encryptedStream As CryptoStream
Try
'Create the encrypted stream
encryptedStream = New CryptoStream(dataStream, encryptor,
CryptoStreamMode.Write)

Dim theWriter As StreamWriter
Try
'Write the string to memory via the encryption algorithm
theWriter = New StreamWriter(encryptedStream)
'Write the string to the memory stream
theWriter.Write(AString)

'End the writing
theWriter.Flush()
encryptedStream.FlushFinalBlock()

'Position back at start
dataStream.Position = 0

'Create area for data
ReDim encryptedData(CInt(dataStream.Length))

'Read data from memory
dataStream.Read(encryptedData, 0, CInt(dataStream.Length))

'Convert to String
Return Convert.ToBase64String(encryptedData, 0,
encryptedData.Length)
Finally
theWriter.Close()
End Try
Finally
encryptedStream.Close()
End Try
Finally
dataStream.Close()
End Try
End If
End Function

Public Shared Function DecryptString(ByVal AString As String) As String
If AString = String.Empty Then
Return AString
Else
Dim encryptedData() As Byte
Dim dataStream As MemoryStream
Dim encryptedStream As CryptoStream
Dim strLen As Integer

'Get the byte data
encryptedData = Convert.FromBase64String(AString)

Try
dataStream = New MemoryStream
Try
'Create decryptor and stream
Dim decryptor As ICryptoTransform
decryptor = mProvider.CreateDecryptor()
encryptedStream = New CryptoStream(dataStream, decryptor,
CryptoStreamMode.Write)

'Write the decrypted data to the memory stream
encryptedStream.Write(encryptedData, 0, encryptedData.Length -
1)
encryptedStream.FlushFinalBlock()

'Position back at start
dataStream.Position = 0

'Determine length of decrypted string
strLen = CInt(dataStream.Length)

'Create area for data
ReDim encryptedData(strLen - 1)

'Read decrypted data to byte()
dataStream.Read(encryptedData, 0, strLen)

'Construct string from byte()
Dim retStr As String

Dim i As Integer
For i = 0 To strLen - 1
retStr += Chr(encryptedData(i))
Next

'Return result
Return retStr
Finally
encryptedStream.Close()
End Try
Finally
dataStream.Close()
End Try
End If
End Function

End Class
 
S

Sven Groot

Robert said:
I am looking for an example of encryption/decryption in asp.net. I
want to encrypt a string before I send it to a Web Service, decrypt it
on the Web Service then encrypt the results on the Web Service and
decrypt the results on the web app. Any example using
System.Security.Cryptography namespace (VB not C#, thanks)would be
greatly appreciated. Thanks in advance.

You might also want to look into the Web Services Enhancements, it supports
WS-Security which offers encryption.

Check here:
http://msdn.microsoft.com/webservices/building/wse/default.aspx

And specifically here:
http://msdn.microsoft.com/webservic...l=/library/en-us/dnwse/html/wseencryption.asp
 
R

Rob

Your functions worked great, Joe. Is this a secure way of passing data
from a web app to a web service and vice versa? It almost looks too easy
to be true. Thanks a million.

-Rob
 
R

Rob

Encrypting and decrypting within the web service works, but when I
encrypt a message on the web service and try to decrypt it on the web
app, I get a 'Bad Data' error at this line of code:

encryptedStream.FlushFinalBlock()

Can your functions not be used in this manner...? Thanks
 
J

Joe Fallon

1. Don't really know. Keep testing.

2. How did they work without a correct Sub New?
It looks like mProvider is in the methods but is not in the previuosly
posted Sub New. (Sorry about that!)

Shared Sub New()
'Ensure that you create your own key and IV.
mProvider = New TripleDESCryptoServiceProvider
mProvider.Key = New Byte() {111, 222, 333, 85, 171, 41, 165, 135, 218,
183, 42, 192, 113, 111, 138, 14}
mProvider.IV = New Byte() {162, 213, 12, 41, 232, 162, 71, 212}
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top