Home All Groups Group Topic Archive Search About

Hash seems incorrect in RC2

Author
13 Jun 2006 4:58 PM
tshad
I have a function I am using to encrypt Keys and it doesn't seem to use the
Hash I asked for.

************************************

    Public Shared Function TripleDESEncode(ByVal value As String, ByVal key
As String) As String

        Dim des As New Security.Cryptography.TripleDESCryptoServiceProvider

        des.IV = New Byte(7) {}

        Dim pdb As New Security.Cryptography.PasswordDeriveBytes(key, New
Byte(-1) {})

        des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})

        Dim ms As New IO.MemoryStream((value.Length * 2) - 1)

        Dim encStream As New Security.Cryptography.CryptoStream(ms,
des.CreateEncryptor(), Security.Cryptography.CryptoStreamMode.Write)

        Dim plainBytes As Byte() = Text.Encoding.UTF8.GetBytes(value)

        encStream.Write(plainBytes, 0, plainBytes.Length)

        encStream.FlushFinalBlock()

        Dim encryptedBytes(CInt(ms.Length - 1)) As Byte

        ms.Position = 0

        ms.Read(encryptedBytes, 0, CInt(ms.Length))

        encStream.Close()

        Return Convert.ToBase64String(encryptedBytes)

    End Function
***********************************

In the above code, I am using this:

        des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})

But if I look at pdp.hashname, I get "SHA1"?????

Why is that?

Doesn't it use what I pass?

Thanks,

Tom

Author
14 Jun 2006 10:32 AM
Larry Lard
tshad wrote:
Show quoteHide quote
> I have a function I am using to encrypt Keys and it doesn't seem to use the
> Hash I asked for.
>
> ************************************
>
>     Public Shared Function TripleDESEncode(ByVal value As String, ByVal key
> As String) As String
>
>         Dim des As New Security.Cryptography.TripleDESCryptoServiceProvider
>
>         des.IV = New Byte(7) {}
>
>         Dim pdb As New Security.Cryptography.PasswordDeriveBytes(key, New
> Byte(-1) {})
>
>         des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
>
>         Dim ms As New IO.MemoryStream((value.Length * 2) - 1)
>
>         Dim encStream As New Security.Cryptography.CryptoStream(ms,
> des.CreateEncryptor(), Security.Cryptography.CryptoStreamMode.Write)
>
>         Dim plainBytes As Byte() = Text.Encoding.UTF8.GetBytes(value)
>
>         encStream.Write(plainBytes, 0, plainBytes.Length)
>
>         encStream.FlushFinalBlock()
>
>         Dim encryptedBytes(CInt(ms.Length - 1)) As Byte
>
>         ms.Position = 0
>
>         ms.Read(encryptedBytes, 0, CInt(ms.Length))
>
>         encStream.Close()
>
>         Return Convert.ToBase64String(encryptedBytes)
>
>     End Function
> ***********************************
>
> In the above code, I am using this:
>
>         des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
>
> But if I look at pdp.hashname, I get "SHA1"?????
>
> Why is that?

Not sure; the documentation is sparse. But given that CryptDeriveKey is
stated to be a wrapper around the API function, I suspect that the
parameters IS used.

>
> Doesn't it use what I pass?

        Dim key As String = "apple"

        Dim pdb As New Security.Cryptography.PasswordDeriveBytes(key,
New _
Byte(-1) {})


Console.WriteLine(Convert.ToBase64String(pdb.CryptDeriveKey("RC2",
"MD5", 128, New Byte(7) {})))

Console.WriteLine(Convert.ToBase64String(pdb.CryptDeriveKey("RC2",
"SHA1", 128, New Byte(7) {})))

The two output lines are different, so I suspect that what you pass IS
being used.

--
Larry Lard
Replies to group please