473,566 Members | 3,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7688
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(B yVal AString As String) As String
If AString = String.Empty Then
Return AString
Else
Dim encryptedData() As Byte
Dim dataStream As MemoryStream

Dim encryptor As ICryptoTransfor m
encryptor = mProvider.Creat eEncryptor()

Try
dataStream = New MemoryStream

Dim encryptedStream As CryptoStream
Try
'Create the encrypted stream
encryptedStream = New CryptoStream(da taStream, encryptor,
CryptoStreamMod e.Write)

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

'End the writing
theWriter.Flush ()
encryptedStream .FlushFinalBloc k()

'Position back at start
dataStream.Posi tion = 0

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

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

'Convert to String
Return Convert.ToBase6 4String(encrypt edData, 0,
encryptedData.L ength)
Finally
theWriter.Close ()
End Try
Finally
encryptedStream .Close()
End Try
Finally
dataStream.Clos e()
End Try
End If
End Function

Public Shared Function DecryptString(B yVal 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.FromBas e64String(AStri ng)

Try
dataStream = New MemoryStream
Try
'Create decryptor and stream
Dim decryptor As ICryptoTransfor m
decryptor = mProvider.Creat eDecryptor()
encryptedStream = New CryptoStream(da taStream, decryptor,
CryptoStreamMod e.Write)

'Write the decrypted data to the memory stream
encryptedStream .Write(encrypte dData, 0, encryptedData.L ength -
1)
encryptedStream .FlushFinalBloc k()

'Position back at start
dataStream.Posi tion = 0

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

'Create area for data
ReDim encryptedData(s trLen - 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(encryptedDa ta(i))
Next

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

End Class

--
Joe Fallon
"Robert Bull" <ro*********@wb p.org> wrote in message
news:bf******** *************** ***@posting.goo gle.com...
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 .FlushFinalBloc k()

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 TripleDESCrypto ServiceProvider
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.ne t> wrote in message
news:uV******** ******@TK2MSFTN GP12.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 .FlushFinalBloc k()

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
2417
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 supported. In people's experiences, have hosting companies supporting PHP/MySQL generally provided support for mcrypt? Does anyone recommend another...
1
2458
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 occurs in the application when you instantiate a new instance of the class as shown below: ---Dim dp As DPAPIComp.DataProtectorComp = New...
2
4186
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 opening the app the following wrror is generated: Exception decrypting. Decryption failed. Key not valid for use in specified state. Description: An...
2
2573
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 returned data is 1161 in length. public static byte EncryptRSA(string certFilePublic, byte dataToEncrypt) { RSAParameters rsaParams =...
2
7133
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 French grave... Anyway when I run encryption on a plaintext word like:
4
5676
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 = new RijndaelManaged(); cipher.KeySize = 256; cipher.BlockSize = 256;
3
3053
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 functions. While my functions read and write files the encryption/decryption is not working properly. My test file has an original length of...
5
9491
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 code for a few hours: #dics Encryption={',':'hy{;',' ':'h4x0r2','':'zomg','?':'bko','a':'ika','b':'d0v','c':'ino', 'd':'maw', 'e':'aon', 'f':'que',...
9
4810
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 . I didn't test script on windows. Here is the code, please send me your views. <?php /* Mother Eye Chipper with PHP :), Licence:GPL,
9
2429
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 Security; I simply want the contents of records in this file to remain unreadable, even by the client's IT supervisor who can look at the contents of the...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.