Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert Routine from VB to C#

Posted on 2009-04-02
6
Medium Priority
?
674 Views
Last Modified: 2013-12-17
I need to convert the two method below from VB to C#. The method generates a simple Hash.
I am not a VB Programmer and do not understand exactely what the programmer did...

The goal shoud be that the VB and C# Code generates the same Values.

Hope anyone can help me here...
Public Function get_output_simplehash() As String
 
Dim fdata As String
 
Dim HashVal As String, hash As String
Dim hbytes(16) As Byte
Dim dbyte As Byte
Dim l As Long
Dim bb As Long
Dim p As Integer, i As Integer
 
Dim fs As Object, ts As Object
 
get_output_simplehash = ""
 
For i = 1 To 16
    hbytes(i) = 0
Next i
 
l = 0
For bb = 1 To bufferanz
    fdata = buffer(bb)
    l = l + Len(fdata)
    For i = 1 To Len(fdata)
        dbyte = CByte(Asc(Mid(fdata, i, 1)))
        p = i Mod 16 + 1
        hbytes(p) = hbytes(p) Xor dbyte
    Next i
Next bb
 
HashVal = ""
 
For i = 1 To 16
    HashVal = HashVal & " " & Right("0" & Hex(CInt(hbytes(i))), 2)
Next i
 
get_output_simplehash = Trim(HashVal)
 
End Function
 
 
Public Function get_output_md5hash() As String
 
Dim fs As Object, f As Object, ts As Object
Dim i As Integer
Dim fdata As String
 
Dim sContainer As String, sDescription As String, sProvider As String, lHCryptprov As Long
Dim lHHash As Long, lResult As Long, lSignatureLen As Long
 
Dim hdata(24) As Byte
Dim lDataLen As Long
Dim p As Integer
Dim HashVal As String
 
get_output_md5hash = ""
 
'Get handle to the default provider.
sContainer = vbNullChar
sProvider = MS_DEF_PROV & vbNullChar
If Not CBool(CryptAcquireContext(lHCryptprov, ByVal sContainer, ByVal sProvider, PROV_RSA_FULL, 0)) Then
    GoTo ReleaseHandles:
End If
 
'Create a hash object.
If Not CBool(CryptCreateHash(lHCryptprov, CALG_MD5, 0, 0, lHHash)) Then
    GoTo ReleaseHandles:
End If
 
For i = 1 To bufferanz
    fdata = buffer(i)
    If Not CBool(CryptHashData(lHHash, fdata, Len(fdata), 0)) Then
        'MsgBox ("Error " & CStr(GetLastError) & " during CryptHashData!")
        GoTo ReleaseHandles:
    End If
Next i
 
lDataLen = Len(hdata(0)) * (UBound(hdata) + 1)
If Not CBool(CryptGetHashParam(lHHash, HP_HASHVAL, hdata(0), lDataLen, 0)) Then
    GoTo ReleaseHandles:
End If
p = LBound(hdata)
While p <= UBound(hdata)
    If hdata(p) <> 0 Then
        HashVal = HashVal & " " & Right("0" & Hex$(hdata(p)), 2)
    Else
        p = UBound(hdata)
    End If
    p = p + 1
Wend
 
get_output_md5hash = Trim(HashVal)
 
ReleaseHandles:
'Destroy hash object.
If lHHash Then lResult = CryptDestroyHash(lHHash)
'Release provider handle.
If lHCryptprov Then lResult = CryptReleaseContext(lHCryptprov, 0)
 
 
End Function

Open in new window

0
Comment
Question by:schubduese
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 24058544
Here it is:

 public string get_output_simplehash() {
        string fdata;
        string HashVal;
        string hash;
        byte[,] hbytes;
        byte dbyte;
        long l;
        long bb;
        int p;
        int i;
        object fs;
        object ts;
        get_output_simplehash = "";
        for (i = 1; (i <= 16); i++) {
            hbytes[i] = 0;
        }
        l = 0;
        for (bb = 1; (bb <= bufferanz); bb++) {
            fdata = buffer(bb);
            l = (l + fdata.Length);
            for (i = 1; (i <= fdata.Length); i++) {
                dbyte = byte.Parse(Asc(fdata.Substring((i - 1), 1)));
                p = ((i % 16) 
                            + 1);
                hbytes[p] = hbytes[p];
                dbyte;
            }
        }
        HashVal = "";
        for (i = 1; (i <= 16); i++) {
            HashVal = (HashVal + (" " + ("0" + Hex(int.Parse(hbytes[i]))).Substring((("0" + Hex(int.Parse(hbytes[i]))).Length - 2))));
        }
        return HashVal.Trim();
    }
    
    public string get_output_md5hash() {
        object fs;
        object f;
        object ts;
        int i;
        string fdata;
        string sContainer;
        string sDescription;
        string sProvider;
        long lHCryptprov;
        long lHHash;
        long lResult;
        long lSignatureLen;
        byte[,] hdata;
        long lDataLen;
        int p;
        string HashVal;
        get_output_md5hash = "";
        sContainer = vbNullChar;
        sProvider = (MS_DEF_PROV + vbNullChar);
        if (!bool.Parse(CryptAcquireContext(lHCryptprov, sContainer, sProvider, PROV_RSA_FULL, 0))) {
            goto ReleaseHandles;
        }
        // Create a hash object.
        if (!bool.Parse(CryptCreateHash(lHCryptprov, CALG_MD5, 0, 0, lHHash))) {
            goto ReleaseHandles;
        }
        for (i = 1; (i <= bufferanz); i++) {
            fdata = buffer(i);
            if (!bool.Parse(CryptHashData(lHHash, fdata, fdata.Length, 0))) {
                // MsgBox ("Error " & CStr(GetLastError) & " during CryptHashData!")
                goto ReleaseHandles;
            }
        }
        lDataLen = (hdata[0].Length 
                    * (UBound(hdata) + 1));
        if (!bool.Parse(CryptGetHashParam(lHHash, HP_HASHVAL, hdata[0], lDataLen, 0))) {
            goto ReleaseHandles;
        }
        p = LBound(hdata);
        while ((p <= UBound(hdata))) {
            if ((hdata[p] != 0)) {
                HashVal = (HashVal + (" " + ("0" + Hex(hdata[p])).Substring((("0" + Hex(hdata[p])).Length - 2))));
            }
            else {
                p = UBound(hdata);
            }
            p = (p + 1);
            get_output_md5hash = HashVal.Trim();
        ReleaseHandles:
            if (lHHash) {
                lResult = CryptDestroyHash(lHHash);
            }
            // Release provider handle.
            if (lHCryptprov) {
                lResult = CryptReleaseContext(lHCryptprov, 0);
            }
        }
    }

Open in new window

0
 
LVL 21

Expert Comment

by:MogalManic
ID: 24058547
I used http://www.carlosag.net/Tools/CodeTranslator/ to translate this.

just google "vb to c#"
0
 

Author Comment

by:schubduese
ID: 24059198
MogalManic, thanks for your reply

I will test it asap
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:schubduese
ID: 24059228
My Code is from a VB6 Application, I have seen, that the Translator works between VB.NET and C#.
I think this does not work with VB6!?
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 600 total points
ID: 24061678
You need to translate from VB6 to VB.NET, and then to C#.  You might also want to look at the process, and see if it can be reworked in C#, with the classes that are in the .NET framework...
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 900 total points
ID: 24062139
TheLearnedOne is right.  The code I converted depends on many functions that were part of the VB6 library but were converted to Object methods by the .Net class libary.  For example, if you want to get the character at string position i, instead of
  Mid(fdata, i, 1)
all you need is:
  char ch=fdata[i];

Also the routine uses the dreaded "GOTO" which, while works, is not recommended for writing maintainable code. (see http://en.wikipedia.org/wiki/Considered_harmful)

If all that you want is the MD5 hash value, then you can use built in .net classes to do this:
public string GetMD5Hash(string input)
{
    System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
    byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
    bs = x.ComputeHash(bs);
    System.Text.StringBuilder s = new System.Text.StringBuilder();
    foreach (byte b in bs)
    {
        s.Append(b.ToString("x2").ToLower());
    }
    string password = s.ToString();
    return password;
}

Open in new window

0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question