ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

将密码学vb.net转换为C#

2019-11-18 21:05:45  阅读:180  来源: 互联网

标签:type-conversion cryptography vb-net c net


我在Vb.net中有一个密码学类,可以正常工作,但是我需要在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”会被截断.替换以下两行代码:

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

标签:type-conversion,cryptography,vb-net,c,net
来源: https://codeday.me/bug/20191118/2030962.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有