encryption/decryption asp.net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Bull

    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
  • Joe Fallon

    #2
    Re: encryption/decryption asp.net

    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" <robert.bull@wb p.org> wrote in message
    news:bfa20f3a.0 404281151.5308a 42c@posting.goo gle.com...[color=blue]
    > 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[/color]


    Comment

    • Sven Groot

      #3
      Re: encryption/decryption asp.net

      Robert Bull wrote:[color=blue]
      > 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.[/color]

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

      Check here:


      And specifically here:


      --
      Sven Groot


      Comment

      • Rob

        #4
        Re: encryption/decryption asp.net

        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!

        Comment

        • Rob

          #5
          Re: encryption/decryption asp.net

          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!

          Comment

          • Joe Fallon

            #6
            Re: encryption/decryption asp.net

            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" <rob@charter.ne t> wrote in message
            news:uVflRxfLEH A.1348@TK2MSFTN GP12.phx.gbl...[color=blue]
            > 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![/color]


            Comment

            Working...