用于解密的vba代码的C#代码

时间:2017-06-01 08:31:49

标签: c# access-vba

我需要帮助或有人能指出我正确的方向

我有一个自定义加密的Acces Vba代码,我需要为c#应用程序重写此代码

我已经完成了所有代码转换,但结果并不相同 我认为它可能是编码标准,但我不确定

编辑 我不能使用任何额外的库它必须都是本机C# (我知道vb库)

C#

internal class Class1
{
    public string fEncryptString(string TheString, int nbrPlaces)
    {
        //string sEnc = "91tephen@S", expected = "œ˜:²84²7 ©";
        int i, intlength;
        double mult, tmp;
        string tmpst = "";
        intlength = TheString.Length;
        nbrPlaces = (nbrPlaces % 8);
        mult = Math.Pow(2, nbrPlaces);
        for (i = 1; i < intlength + 1; i++)
        {
            tmp = Convert.ToChar(Mid(TheString, i, 1));
            tmp = tmp * mult;
            tmp = tmp % 256 + tmp / 256;
            tmp = Math.Floor(tmp);
            int x = Convert.ToInt16(tmp);
            tmpst = tmpst + Encoding.Default.GetString(new byte[] { Convert.ToByte(x) });
        }
        return tmpst;
    }

    public string Mid(string s, int a, int b)
    {
        string temp = s.Substring(a - 1, b);
        return temp;
    }

    public string Encrypt(string strString)
    {
        //Scramble the order of characters
        int intLen;
        string strRtt = "";

        //Determine length of string
        intLen = strString.Length;
        //Assign first two characters
        strRtt = strString.Substring(strString.Length - 1) + strString.Substring(strString.Length - 2, 1);

        //Assign all other characters except the last character
        for (int i = 2; i < intLen - 1; i++)
        {
            strRtt += Mid(strString, i, 1);
        }

        //Assign the last character

        strRtt = strRtt + strString.Substring(1, 1);
        //Encrypt the scrambled string
        return fEncryptString(strRtt, 7);
    }
}

VBA

    Public Function fEncryptString(TheString As String, ByVal nbrPlaces As Byte) As String


    Dim tmp As Integer, i As Integer, mult As Integer
    Dim intLength As Integer, tmpSt As String

    intLength = Len(TheString)
    tmpSt = ""
    nbrPlaces = nbrPlaces Mod 8     'no point doing more than 7, besides
    mult = 2 ^ nbrPlaces                  'mult (an Integer) would be too small

    For i = 1 To intLength
        tmp = Asc(Mid$(TheString, i, 1))          'get the ASCII value of each character
        tmp = tmp * mult                             'apply the multiplier
        tmp = tmp Mod 256 + tmp \ 256             'rotate any 'carry' bit
        tmpSt = tmpSt & Chr$(tmp)                 'add the character to the String
    Next i

    fEncryptString = tmpSt
End Function

Public Function fEncrypt(strString As String)
'Scramble the order of characters
    Dim intLen As Integer
    Dim strRtt As String

    'Determine length of string
    intLen = Len(strString)

    'Assign first two characters
    strRtt = Right(strString, 1) & Left(Right(strString, 2), 1)

    'Assign all other characters except the last character
    For i = 2 To intLen - 2
        strRtt = strRtt & Mid(strString, i, 1)
    Next i

    'Assign the last character
    strRtt = strRtt & Left(strString, 1)

    'Encrypt the scrambled string
    fEncrypt = fEncryptString(strRtt, 7)
End Function

0 个答案:

没有答案