Home All Groups Group Topic Archive Search About

Upgrading Encryption to .Net not Working!!!!!!!

Author
23 Apr 2005 7:14 AM
TC
Hey Folks,

I am using the following the code below in VB6:

    Public Function EncryptPassword(ByVal Password As String) As String

        Dim objEncoder As BX_BCA.Encoder

        Dim objProviders As BX_BCA.clsProviders

        Dim objHash As BX_BCA.clsHash

        Dim lngProviderID As Long

        Dim abytPassword() As Byte

        Set objEncoder =  New BX_BCA.Encoder

        abytPassword() = objEncoder.stringToByteArray(strPassword)

        Set objProviders = New BX_BCA.clsProviders

        lngProviderID = objProviders.LocateProvider("Microsoft Enhanced
Cryptographic Provider v1.0",

ProviderTypes.PROV_RSA_FULL)

        Call
objProviders.Item(lngProviderID).Acquire(Flags:=CRYPT_VERIFYCONTEXT)

        Set objHash =
objProviders.Item(lngProviderID).CreateHash(HashTypes.CALG_MD5)

        Call objHash.AddData(abytPassword())

        EncryptPassword = objHash.ToHex

End Sub


I used an MSDN article to create an ecryption routine under C# but it isn't
being accepted by the app:

  public string EncryptPassword(string passWord)
  {
       MD5CryptoServiceProvider cryptoService;

       try
       {
            byte[] bytValue;
            byte[] bytHash;

            // Create New Crypto Service Provider Object
            cryptoService=new MD5CryptoServiceProvider();

            // Convert the original string to array of Bytes
            bytValue = Encoding.UTF8.GetBytes(passWord);

            // Compute the Hash, returns an array of Bytes
            bytHash = cryptoService.ComputeHash(bytValue);

            cryptoService.Clear();

            // Return a base 64 encoded string of the Hash value
            return Convert.ToBase64String(bytHash);

Any ideas?

Author
25 Apr 2005 1:58 PM
swat
Your VB code is returning the hash in hex, while your C# code is
returning it as a Base64 encoded string.

Try this:
-------------------------
StringBuilder hexVal = new StringBuilder();
foreach (byte b in bytHash)
{
    hexVal.Append(b.ToString("X"));
}
string bytHashInHex = hexVal.ToString();
-------------------------

return bytHashInHex instead of  Convert.ToBase64String(bytHash­)

HTH
Author
25 Apr 2005 2:11 PM
swat
Your VB code is returning the hash in hex, while your C# code is
returning it as a Base64 encoded string.

Try this:
-------------------------
StringBuilder hexVal = new StringBuilder();
foreach (byte b in bytHash)
{
    hexVal.Append(b.ToString("X"));
}
-------------------------

return hexVal.ToString() instead of  Convert.ToBase64String(bytHash)


HTH
Author
25 Apr 2005 5:36 PM
TC
Hey Swat,

That was one thought I had.  As it turns out, below is what I needed:

       string encrypted=
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(passWord,"md5");

Regards,

TC


Show quoteHide quote
"swat" <loka_1***@yahoo.com> wrote in message
news:1114438291.499174.280750@o13g2000cwo.googlegroups.com...
> Your VB code is returning the hash in hex, while your C# code is
> returning it as a Base64 encoded string.
>
> Try this:
> -------------------------
> StringBuilder hexVal = new StringBuilder();
> foreach (byte b in bytHash)
> {
>    hexVal.Append(b.ToString("X"));
> }
> -------------------------
>
> return hexVal.ToString() instead of  Convert.ToBase64String(bytHash)
>
>
> HTH
>