将解密函数从VB6翻译成C#

问题描述 投票:0回答:1

我正在将代码从 VB6 更新为 C#。我有向后兼容性的要求。因此,在 VB6 版本中加密的文件需要能够在 C# 版本中解密。到目前为止我无法解密VB6版本的文件。

VB6解密代码是

Public Function DecryptFile(File() As Byte) As Byte()
    Dim op1 As Integer
    Dim i As Double
    Dim j As Integer
    Dim k As Integer
    Dim l As Integer
    Dim M As Double
    Dim r As Integer
    Dim The_Password As String
    pwork = Swap(Expand_Process("Password"))
    l = Len(Trim(pwork))
    M = UBound(File)
    k = 1
    For i = 0 To M
        j = File(i) - Asc(Mid(pwork, k, 1))
        If j < 0 Then
            j = j + 256
        End If
        File(i) = j
        k = k + 1
        If k > l Then
           k = 1
        End If
    Next i
    DecryptFile = File
End Function

Public Function Swap(The_Data As String)
    Dim Y, x As Integer
    Dim rest As String
    Dim r As Integer
    Dim wrk As String
    rest = ""
    x = Len(The_Data) / 2
    For Y = 1 To x
      wrk = Mid(The_Data, Y * 2, 1) & Mid(The_Data, Y * 2 - 1, 1)
      rest = rest & wrk
    Next Y
    Y = Len(The_Data) - (x * 2)
    If Y > 0 Then
       rest = rest & Right(The_Data, Y)
    End If
    Swap = rest
End Function

Public Function Expand_Process(The_Data As String) As String
    Dim p1 As Double
    Dim p2 As Integer
    Dim rd() As Variant
    Dim m1 As Integer
    Dim m2 As Integer
    Dim dwork As String
    rd = Array(7, 3, 5, 1, 4, 2)
    p1 = Round(Len("Password") / 5, 0)
    dwork = ""
    For p2 = 1 To 5
        m1 = rd(p2) * p1
        m2 = m1 - (p1 - 1)
        dwork = dwork & Mid(The_Data, m1, p1)
    Next p2
    Expand_Process = dwork
End Function

我将其翻译成的 C# 代码是

public byte[] Decrypt(byte[] file)
{
    int op1;
    int i;
    int j;
    int k;
    int l;
    double m;
    int r;
    string pword = Swap(Expand_Process("Password"));
    l = pword.Trim().Length;
    m = file.GetUpperBound(0);
    k = 1;
    for (i = 0; i <= m; i++)
    {
        string mid = Microsoft.VisualBasic.Strings.Mid(pword, k, 1);
        int asc = Microsoft.VisualBasic.Strings.Asc(mid);
        j = file[i] - asc;
        if (j < 0)
        {
            j += 256;
        }
        file[i] = Convert.ToByte(j);
        k++;
        if (k > l)
        {
            k = 1;
        }
    }
    return file;
}

private string Expand_Process(string data)
{
    double p1;
    int[] rd;
    double m1;
    double m2;
    rd = [7, 3, 5, 1, 4, 2];
    p1 = Math.Round((double)("Password".Length / 5), 0);
    string dwork = "";
    for (int p2 = 1; p2 <= 5; p2++)
    {
        m1 = rd[p2] * p1;
        m2 = m1 - (p1 - 1);
        dwork += dwork + Microsoft.VisualBasic.Strings.Mid(data, (int)m1, (int)p1);
    }
    return dwork;
}

private string Swap(string data)
{
    int x;
    int y;
    string rest;
    int r;
    string wrk;
    rest = "";
    x = data.Length / 2;
    for (y = 1; y <= x; y++)
    {
        wrk = Microsoft.VisualBasic.Strings.Mid(data, y * 2, 1) + Microsoft.VisualBasic.Strings.Mid(data, y * 2 - 1, 1);
        rest += wrk;
    }
    y = data.Length - (x * 2);
    if (y > 0)
    {
        rest += Microsoft.VisualBasic.Strings.Right(data, y);
    }
    return rest;
}

我假设问题在于两种语言之间的打字差异。例如,VB6 中的行

File(i) = j
不能直接写入 C# 中作为
file[i] = j
,因为文件数组是字节。我尝试使用
Convert.ToByte(j)
,但我不确定这是否是准确的翻译。

如果您可以帮助我找出翻译代码的问题,以便我可以解密使用旧 VB6 代码加密的文件,我将不胜感激。谢谢!

c# vb6 code-conversion
1个回答
0
投票

这是我的翻译

       static byte[] DecryptFile(byte[] File)
        {
            byte j;
            int k;
            int l;
            double M;
            int r;

            string pwork = Swap("Password");

            l = pwork.Trim().Length;
            k = 1;
            for(int i = 0; i < File.Length; i++)
            {
                byte chr = (byte)(pwork.Substring(k, 1).First());
                j = (byte)(File[i] - chr);
                File[i] = j;
                k = k + 1;
                if (k > l) k = 1;
            }
            return File;
        }
        static string Swap(string The_Data)
        {
            int x;
            string rest = "";
            int r;
            char wrk;

            x = The_Data.Length / 2;
            for (int Y = 1; Y <= x; Y++)
            {
                wrk = (char)(The_Data[Y * 2] & The_Data[Y * 2 - 1]);
                rest += wrk;

                Y = The_Data.Length - (x * 2);
                if (Y > 0) rest = rest + The_Data.Substring(The_Data.Length - Y);
            }
            return rest;
         }
© www.soinside.com 2019 - 2024. All rights reserved.