Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 15th, 2004, 08:54 AM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default SHA1 Encryption/Decryption help

Hi:

Can someone help me in decrypting a string that has been encrypted in the manner below?:

=== Start Encryption ===
public static byte[] EncryptPassword(string password)
{
    UnicodeEncoding encoding = new UnicodeEncoding();
    byte[] hashBytes = encoding.GetBytes( password );

    // compute SHA-1 hash.
    SHA1 sha1 = new SHA1CryptoServiceProvider();
    byte[] cryptPassword = sha1.ComputeHash ( hashBytes );

    return cryptPassword;
}
=== End Encryption ===


Reply With Quote
  #2 (permalink)  
Old April 15th, 2004, 11:34 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Nope! SHA1 hashing is a one way street. That's why it's so great for passwords. If you are looking to do some kind of check of a password, you'll need to hash the password entered and compare the result to the saved hashed password. This is the usual technique. You can't however, un-hash.

Peter
-------------------------
Work smarter, not harder
Reply With Quote
  #3 (permalink)  
Old April 15th, 2004, 12:16 PM
Authorized User
 
Join Date: Jun 2003
Location: Norfolk, , United Kingdom.
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default


I had the same issue a while back, and found a routine that will do encryption and decryption.

If you want this routine, I can email it to you, it does three types of encryption DES, RC2 and Rijndael.


Duncan
Reply With Quote
  #4 (permalink)  
Old October 30th, 2004, 06:41 AM
Registered User
 
Join Date: Oct 2004
Location: New Delhi, Delhi, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to tabishahmed Send a message via Yahoo to tabishahmed
Default

Hi Duncan,

Can u plz email me the Encryption and decryption routine for Rijdael and RC2.
Thanks & Regards,

Tabish

tabish.ahmed@gmail.com
Reply With Quote
  #5 (permalink)  
Old October 30th, 2004, 06:51 AM
Authorized User
 
Join Date: Jun 2003
Location: Norfolk, , United Kingdom.
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
put this in its own class

'Set the Default options.
Option Explicit On
Option Strict On

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

' SymmCrypto is a wrapper of System.Security.Cryptography.SymmetricAlgorithm classes
' and simplifies the interface. It supports customized SymmetricAlgorithm as well.
Public Class SymmCrypto

    ' Supported .Net intrinsic SymmetricAlgorithm classes.
    Public Enum CryptographyType As Integer
        Des
        Rc2
        Rijndael
    End Enum

    Private Shared mobjCryptoService As SymmetricAlgorithm

    'Constructor for using a customized SymmetricAlgorithm class.
    Public Sub New(ByVal encryptionType As CryptographyType)
        MyBase.New()
        Select Case (encryptionType)
            Case CryptographyType.Des
                mobjCryptoService = New DESCryptoServiceProvider
            Case CryptographyType.Rc2
                mobjCryptoService = New RC2CryptoServiceProvider
            Case CryptographyType.Rijndael
                mobjCryptoService = New RijndaelManaged
        End Select
    End Sub

    ' Depending on the legal key size limitations of a specific CryptoService provider
    ' and length of the private key provided, padding the secret key with space character
    ' to meet the legal size of the algorithm.
    Private Function GetLegalKey(ByVal key As String) As Byte()
        Dim sTemp As String
        If (mobjCryptoService.LegalKeySizes.Length > 0) Then
            Dim LessSize As Int32 = 0
            Dim MoreSize As Int32 = mobjCryptoService.LegalKeySizes(0).MinSize
            Dim SkipSize As Int32 = mobjCryptoService.LegalKeySizes(0).SkipSize
            ' key sizes are in bits
            Do While (key.Length * 8 > MoreSize)
                LessSize = MoreSize
                MoreSize += SkipSize
            Loop
            sTemp = key.PadRight(CType(MoreSize / 8, Int32), CType(" ", Char))
        Else
            sTemp = key
        End If

        ' convert the secret key to byte array
        Return ASCIIEncoding.ASCII.GetBytes(sTemp)
    End Function

#Region "ENCRYPTING"
    Public Function Encrypting(ByVal source As String, ByVal key As String) As String
        Dim bytIn As Byte() = ASCIIEncoding.ASCII.GetBytes(source)
        ' create a MemoryStream so that the process can be done without I'O files
        Dim ms As MemoryStream = New MemoryStream

        Dim bytKey As Byte() = GetLegalKey(key)

        ' set the private key
        mobjCryptoService.Key = bytKey
        mobjCryptoService.IV = bytKey

        ' create an Encryptor from the Provider Service instance
        Dim encrypto As ICryptoTransform = mobjCryptoService.CreateEncryptor

        ' create Crypto Stream that transforms a stream using the encryption
        Dim cs As CryptoStream = New CryptoStream(ms, encrypto, CryptoStreamMode.Write)

        ' write out encrypted content into MemoryStream
        cs.Write(bytIn, 0, bytIn.Length)
        cs.FlushFinalBlock()

        ' get the output and trim the '\0' bytes
        Dim bytOut As Byte() = ms.GetBuffer
        Dim i As Int32 = 0
        For i = 0 To bytOut.Length - 1
            If (bytOut(i) = 0) Then
                Exit For
            End If
        Next

        ' convert into Base64 so that the result can be used in xml
        Return Convert.ToBase64String(bytOut, 0, i)

    End Function
#End Region

#Region "DECRYPTING"
    Public Function Decrypting(ByVal source As String, ByVal key As String) As String
        ' convert from Base64 to binary
        Dim bytIn As Byte() = Convert.FromBase64String(source)
        ' create a MemoryStream with the input
        Dim ms As MemoryStream = New MemoryStream(bytIn, 0, bytIn.Length)

        Dim bytKey As Byte() = GetLegalKey(key)

        ' set the private key
        mobjCryptoService.Key = bytKey
        mobjCryptoService.IV = bytKey


        ' create a Decryptor from the Provider Service instance
        Dim encrypto As ICryptoTransform = mobjCryptoService.CreateDecryptor

        'create Crypto Stream that transforms a stream using the decryption
        Dim cs As CryptoStream = New CryptoStream(ms, encrypto, CryptoStreamMode.Read)

        '' read out the result from the Crypto Stream
        Dim sr As StreamReader = New StreamReader(cs)

        Return sr.ReadToEnd
    End Function
#End Region


To call this routine use the following syntax at the top of each class
    Private Encryption As SymmCrypto = New SymmCrypto(SymmCrypto.CryptographyType.Des)



Duncan
Reply With Quote
  #6 (permalink)  
Old October 30th, 2004, 06:59 AM
Authorized User
 
Join Date: Jun 2003
Location: Norfolk, , United Kingdom.
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I forgot to add that when you need to encrypt or decrypt something the code is as follows:

encryption.Encrypting(value to encrypt, key)
encryption.Decrypting(value to decrypt, key)

the key can be any value you like for example "12345 " (shown with quotes to show that the last charactor is a space), I have added the key to the app.exe.config file, and read it in using configurationsettings.appsettings

Hope this helps


Duncan
Reply With Quote
  #7 (permalink)  
Old November 2nd, 2004, 01:00 AM
Registered User
 
Join Date: Oct 2004
Location: New Delhi, Delhi, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to tabishahmed Send a message via Yahoo to tabishahmed
Default

Thanks Duncan.
Reply With Quote
  #8 (permalink)  
Old November 15th, 2004, 07:20 PM
Registered User
 
Join Date: Nov 2004
Location: , , USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Have any of you guys tried a key greater than eight characters? "12345 " works for me too, but "12345679 " does not because the loop in GetLegalKey() never quits. Also, I tried using my name "bill " and the encryption worked, but the decryption failed. Does anyone know what is going on?

Bill

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Encryption/Decryption with rijndael ?!? vbmo Classic ASP Basics 3 November 17th, 2009 04:14 AM
Encryption/Decryption Problem param99 SQL Server 2000 4 December 14th, 2006 05:20 AM
Encryption and Decryption kvanchi General .NET 1 February 2nd, 2005 02:04 PM
Encryption and Decryption kvanchi General .NET 2 January 24th, 2005 02:52 PM
SHA1 Encryption/Decryption help kakko72 General .NET 2 April 16th, 2004 03:12 PM



All times are GMT -4. The time now is 03:14 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.