Hash seems incorrect in RC2

T

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
 
L

Larry Lard

tshad said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Shared functions not accessible 10
3DES confusion 1
8 byte key for 3DES 3
Crypto Question 2

Top