C#中的解密函数等价

时间:2015-06-05 14:37:25

标签: c# vb.net encryption

我有这个函数来解密VB .NET中的连接字符串,我试着把它带到C#:

Function DecryptText(strText As String, ByVal strPwd As String)
    Dim i As Integer, c As Integer
    Dim strBuff As String

#If Not CASE_SENSITIVE_PASSWORD Then

    'Convert password to upper case
    'if not case-sensitive
    strPwd = UCase$(strPwd)

#End If

    'Decrypt string
    If Len(strPwd) Then
        For i = 1 To Len(strText)
            c = Asc(Mid$(strText, i, 1))
            c = c - Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
            strBuff = strBuff & Chr$(c & &HFF)
        Next i
    Else
        strBuff = strText
    End If
    DecryptText = strBuff
End Function

我试过这两种方式:

public static string DesencriptarCadena(this string cadena, string llave = "asdf")
{
    try
    {
        cadena = cadena.Replace("\r", "").Replace("\n", "");
        byte b = 0;
        string strBuff = "";
        System.Text.Encoding encoding = System.Text.Encoding.Default;

        for (int i = 0; i < cadena.Length; i++)
        {
            b = encoding.GetBytes(cadena.ToCharArray(), i, 1)[0];
            int ind = i % llave.Length;
            b -= encoding.GetBytes(llave.ToCharArray(), ind, 1)[0];
            strBuff += encoding.GetChars(new byte[] { Convert.ToByte((int)b & +0xff) })[0];
        }

        return strBuff;
    }
    catch (Exception ex)
    {
        return cadena;
    }

}

或其他方式:

public static string DesencriptarCadena(this string cadena, string llave = "asdf")
{
    try
    {
        cadena = cadena.Replace("\r", "").Replace("\n", "");
        int c;
        string strBuff = "";
        System.Text.Encoding encoding = System.Text.Encoding.Default;

        for (int i = 0; i < cadena.Length; i++)
        {
            int ind = i % llave.Length;
            c = cadena[i];
            c -= llave[ind];
            strBuff += Convert.ToChar(c & +0xff);
        }

        return strBuff;
    }
    catch (Exception ex)
    {
        return cadena;
    }

}

但是,它不起作用。有人可以向我解释我应该做些什么吗?

第一种方式返回:

ÞÓÚÏË×ÞÓÚÏË×ÞÓÚÏ    ÞÓÚÏË×ÞÓÚÏú×ÞÓÚÏË×ÞÓÚÏË×ÞÓÚÏË×ÞÚÏË×ÞÓÚÏË×Þ

在第二个:NúÎCÄÁÎî

它没有返回解密的连接字符串。

2 个答案:

答案 0 :(得分:0)

对于这些类型的转化,

http://converter.telerik.com一直都很棒。让我知道这是如何工作的。

public object DecryptText(string strText, string strPwd)
{
int i = 0;
int c = 0;
string strBuff = null;

#if Not CASE_SENSITIVE_PASSWORD

//Convert password to upper case
//if not case-sensitive
strPwd = Strings.UCase(strPwd);

#endif

//Decrypt string
if (Strings.Len(strPwd)) {
    for (i = 1; i <= Strings.Len(strText); i++) {
        c = Strings.Asc(Strings.Mid(strText, i, 1));
        c = c - Strings.Asc(Strings.Mid(strPwd, (i % Strings.Len(strPwd)) + 1, 1));
        strBuff = strBuff + Strings.Chr(c + 0xff);
    }
} else {
    strBuff = strText;
}
return strBuff;
}

答案 1 :(得分:0)

在包含Microsoft.VisualBasic程序集之后,我添加了两个using语句:

using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;

我编译了它并用dotPeek反编译它。结果就是:

    public static object DecryptText(string strText, string strPwd)
    {
        string str = string.Empty;
        strPwd = Strings.UCase(strPwd);
        if ((uint)Strings.Len(strPwd) > 0U)
        {
            int num1 = 1;
            int num2 = Strings.Len(strText);
            int Start = num1;
            while (Start <= num2)
            {
                int num3 = checked(Strings.Asc(Strings.Mid(strText, Start, 1)) - Strings.Asc(Strings.Mid(strPwd, unchecked(Start % Strings.Len(strPwd)) + 1, 1)));
                str = str + Conversions.ToString(Convert.ToChar(Conversions.ToString(num3) + Conversions.ToString((int)byte.MaxValue)));
                checked { ++Start; }
            }
        }
        else
            str = strText;
        return (object)str;
    }