Print Page | Close Window

Cryptography

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=5652
Printed Date: 29 March 2026 at 8:02am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Cryptography
Posted By: Diep-Vriezer
Subject: Cryptography
Date Posted: 10 September 2003 at 10:50am

Hi all, yes I've got yet another 'problem'.

This time it is about encrypting data, not really decripting. I think I need the System.Security.Cryptography namespace, since that's the one they recomment at msdn.microsoft.com.

Well, all I want is to encode an input string using MD5 or DES and select the security strenght, or bits (128-bit or 256-bit). Then I want to respond the output string.

Like: Input > Proces > Response.Write(Output)



-------------
Gone..



Replies:
Posted By: Diep-Vriezer
Date Posted: 10 September 2003 at 12:28pm

Not like ton's of responses, so lets keep it easy.

I just want an input string to be encrypted like this

Dim InputString As String

InputString = "Testing"

Response.Write(SHA512.Create.Create(InputString).GetType.ToS tring)

So, why doesn't this work?



-------------
Gone..


Posted By: otaku
Date Posted: 10 September 2003 at 2:56pm

i'm using this for my encrypt password:

Imports System.Web.Security

Dim xxx As String = "Testing"

Dim str As String = FormsAuthentication.HashPasswordForStoringInConfigFile(xxx, "MD5")

Response.Write(str)



-------------
Martial Boissonneault
http://getElementById.com/


Posted By: Diep-Vriezer
Date Posted: 10 September 2003 at 3:19pm
This works fine, but how can I do the same with SHA512 encryption (just to be sure the data is ok )? This is not possible with this function, since I can choose between MD5 and SHA1

-------------
Gone..


Posted By: otaku
Date Posted: 10 September 2003 at 3:43pm

I just find a tutorial (5 pages) with example and explanation, folllow the link below see if that's help you

http://www.devx.com/codemag/Article/16747/0/page/1 - http://www.devx.com/codemag/Article/16747/0/page/1



-------------
Martial Boissonneault
http://getElementById.com/


Posted By: Diep-Vriezer
Date Posted: 10 September 2003 at 11:40pm

Let's see...

Thx Alot, this may just the thing I need



-------------
Gone..


Posted By: otaku
Date Posted: 11 September 2003 at 6:31am

I found working code (Rijndael Algorithm => Advanced Encryption Standard) which could interest you.

Let's start with the class =>  Crypto.vb

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

Public Class Crypto

    Public Function EncryptString(ByVal InputString As String, ByVal PassPhrase As String, Optional ByVal IVString As String = vbNullString) As String

        Dim CryptProvider As New RijndaelManaged()
        Dim hashMD5 As New MD5CryptoServiceProvider()
        Dim InputbyteArray() As Byte = Encoding.UTF8.GetBytes(InputString)

        CryptProvider.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(PassPhrase) )
        CryptProvider.IV = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(IVString))
        CryptProvider.Mode = CipherMode.CBC

        Dim ms As MemoryStream = New MemoryStream()
        Dim cs As CryptoStream = New CryptoStream(ms, _
        CryptProvider.CreateEncryptor(), CryptoStreamMode.Write)
        cs.Write(InputbyteArray, 0, InputbyteArray.Length)
        cs.FlushFinalBlock()

        Dim ret As StringBuilder = New StringBuilder()
        Dim b() As Byte = ms.ToArray
        Dim I As Integer
        Dim Out As String
        For I = 0 To UBound(b)
             'Format as hex
             ret.AppendFormat("{0:X2}", b(I))
        Next
        Return ret.ToString()

    End Function

    Public Function DecryptString(ByVal InputString As String, ByVal PassPhrase As String, ByVal IVString As String) As String

        Dim DES As New RijndaelManaged()

        Dim hashMD5 As New MD5CryptoServiceProvider()

        DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(PassPhrase) )
        DES.IV = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(IVString))
        DES.Mode = CipherMode.CBC
        'Put the input string into the byte array
        Dim inputByteArray(InputString.Length / 2 - 1) As Byte
        Dim X As Integer
        For X = 0 To InputString.Length / 2 - 1
             Dim IJ As Integer = (Convert.ToInt32(InputString.Substring(X * 2, 2), 16))
             Dim BT As New ByteConverter()
             inputByteArray(X) = New Byte()
             inputByteArray(X) = BT.ConvertTo(IJ, GetType(Byte))
        Next

        'Create the crypto objects
        Dim MS As MemoryStream = New MemoryStream()
        Dim CS As CryptoStream = New CryptoStream(MS, DES.CreateDecryptor(), CryptoStreamMode.Write)

        'Flush the data through the crypto stream into the memory stream
        CS.Write(inputByteArray, 0, inputByteArray.Length)
        CS.FlushFinalBlock()

        'Get the decrypted data back from the memory stream
        Dim ret As StringBuilder = New StringBuilder()
        Dim B() As Byte = MS.ToArray
        Dim I As Integer
        For I = 0 To UBound(B)
             ret.Append(Chr(B(I)))
        Next
        Return ret.ToString()

    End Function

End Class


Then create an other page whatever.aspx, create an instance of our Crypto class
and use the 2 functions of that Crypto class for encrypt and decrypt your string:

Sub Page_Load(.................

Dim Crypt As New Crypto()
Dim TestString, PassPhrase, IVPhrase, CryptString, DecryptString As String
TestString = "Testing"
PassPhrase = "This is a passphrase"
IVPhrase = "This is an IV phrase"

Response.Write("Original String: " & TestString & "<br />")

CryptString = Crypt.EncryptString(TestString, PassPhrase, IVPhrase)
Response.Write("EnCrypt String: " & CryptString & "<br />")

DecryptString = Crypt.DecryptString(CryptString, PassPhrase, IVPhrase)
Response.Write("DeCrypt String: " & DecryptString & "<br />")

End Sub



-------------
Martial Boissonneault
http://getElementById.com/


Posted By: Diep-Vriezer
Date Posted: 11 September 2003 at 7:58am

 Fantastic

Dude, you rock, this is exactly what I wanted! Thx!



-------------
Gone..



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net