簡體   English   中英

將加密vb.net轉換為c#

[英]convert cryptography vb.net to c#

我在Vb.net中有一個Cryptography類工作正常,但我需要轉換為c#代碼。 我的Vb.net方法是這樣的:

   Public Shared Function CryptSenha(ByVal strCdSenha As String) As String


        Dim Chave As String

        Const MIN_ASC = 48
        Const MAX_ASC = 126
        Const NUM_ASC = MAX_ASC - MIN_ASC + 1

        Chave = 2001 

        Dim offset As Long
        Dim str_len As Integer
        Dim i As Integer
        Dim ch As Integer
        Dim to_text As String

        Try
            to_text = ""
            offset = NumericPassword(Chave)
            Rnd(-1)
            Randomize(offset)
            str_len = Len(strCdSenha)

            For i = 1 To str_len 'Faça 1 até str_len
                ch = Asc(Mid$(strCdSenha, i, 1))

                'Início do If
                If ch >= MIN_ASC And ch <= MAX_ASC Then
                    ch = ch - MIN_ASC
                    offset = Int((NUM_ASC + 1) * Rnd())
                    ch = ((ch + offset) Mod NUM_ASC)
                    ch = ch + MIN_ASC
                    to_text = to_text & Chr(ch)
                End If

            Next i
            Return to_text
        Catch ex As Exception
            Throw ex
        End Try
    End Function

我的C#代碼是這樣的:

 public static string CryptSenha(string strCdSenha)
    {
    string Chave = null;

    const int MIN_ASC = 48;
    const int MAX_ASC = 126;
    const int NUM_ASC = MAX_ASC - MIN_ASC + 1;

    Chave = "2001";
    long offset = 0;
    int str_len = 0;
    int i = 0;
    int ch = 0;
    string to_text = null;

    try
    {
        to_text = "";
        offset = NumericPassword(Chave);
        VBMath.Rnd(-1);
        VBMath.Randomize(offset);
        str_len = Strings.Len(strCdSenha);

        for (i = 1; i <= str_len; i++)
        {
            ch = Strings.Asc(Strings.Mid(strCdSenha, i, 1));

            if (ch >= MIN_ASC & ch <= MAX_ASC)
            {
                ch = ch - MIN_ASC;
                offset = Convert.ToInt64((NUM_ASC + 1) * VBMath.Rnd());
                ch = Convert.ToInt16((ch + offset) % NUM_ASC);
                ch = ch + MIN_ASC;
                to_text = to_text + Strings.Chr(ch);
            }
        }

        return to_text;
}
catch (Exception ex)
{
    throw ex;
}

}

所以當我在vb.net中使用我的函數時,像'Igor'這樣的簡單單詞等於“iLCA”

在函數c#中,'Igor'這個詞等於“iLDB”,我想這是一個轉換

在c#代碼的這一行:

offset = Convert.ToInt64((NUM_ASC + 1) * VBMath.Rnd());

有人可以幫幫我嗎?

您對'Int'的轉換不正確 - 'Int'截斷。 替換以下2行代碼:

offset = Convert.ToInt32(Math.Floor(Convert.ToDouble((NUM_ASC + 1) * VBMath.Rnd())));
ch = (int)((ch + offset) % NUM_ASC);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM