 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 15th, 2004, 08:54 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 ===
|
|

April 15th, 2004, 11:34 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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
|
|

April 15th, 2004, 12:16 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 30th, 2004, 06:41 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Duncan,
Can u plz email me the Encryption and decryption routine for Rijdael and RC2.
Thanks & Regards,
Tabish
[email protected]
|
|

October 30th, 2004, 06:51 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 30th, 2004, 06:59 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 2nd, 2004, 01:00 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Duncan.
|
|

November 15th, 2004, 07:20 PM
|
|
Registered User
|
|
Join Date: Nov 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |