473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

encryption/decryption asp.net

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
Nov 20 '05 #1
5 7673
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

--
Joe Fallon
"Robert Bull" <ro*********@wbp.org> wrote in message
news:bf**************************@posting.google.c om...
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

Nov 20 '05 #2
Robert Bull wrote:
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/webservice...e/default.aspx

And specifically here:
http://msdn.microsoft.com/webservice...encryption.asp

--
Sven Groot
Nov 20 '05 #3
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4
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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #5
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

--
Joe Fallon

"Rob" <ro*@charter.net> wrote in message
news:uV**************@TK2MSFTNGP12.phx.gbl...
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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: D. Alvarado | last post by:
On my Fedora Core 2 Linux dev box, I have installed mcrypt and compiled PHP with the --with-mcrypt option. I am concerned that when I move to another hosting enviornment mcrypt will not be...
1
by: Jase H | last post by:
Hello, I have a ASP.NET web application problem involving the data encryption and decryption assembly(DLL) used on the connection string value that is set in the webconfig file. The problem...
2
by: Dave Bailey | last post by:
I have developed a web app using DPAPI to encrypt a connection string in the web.config file. The application works perfectly on the development machine but when deployed to the server when...
2
by: sushant.bhatia | last post by:
Hi All. I'm using the NCrypto dll for RSA Encryption/Decryption (http://sourceforge.net/projects/ncrypto/). My encryption code in .Net is pretty simple. The dataToEncrypt length is 1024. The...
2
by: almurph | last post by:
Hi everyone, Can you help me please? I am having a problem with the encryption/decryption of words with the Irish fada in them. The Irish fada is like this: áéíóú/ÁÉÍÓÚ. It's kind of like the...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
3
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
5
by: Netwatcher | last post by:
well, i started messing around with dictionaries, yet, most of the pages i found about them always talk about getting only one word out of it and turning it vice versa, i've been playing with that...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
9
by: Alan M Dunsmuir | last post by:
In my (PHP-5) application I have to write some records to a table in my database, which I don't want even my clients using the system to be able to read. This is not a problem in National...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.