Java到VB.Net转换此函数

时间:2021-06-26 22:34:48

I have a Java snippet here, I was wondering if it is possible to translate to VB.Net, as I have no found a snippet for VB.Net - only this:

我在这里有一个Java片段,我想知道是否有可能转换为VB.Net,因为我没有找到VB.Net的片段 - 只有这个:

    private static byte[] SHA1(final String in)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(in.getBytes("iso-8859-1"), 0, in.length());
        return md.digest();
    }

    public static String decryptSHA1(String key, final String start) {
        final String delim = "a";
        if (start == null)
            return null;
        byte[] hashedkey;
        byte[] password;
        int i;
        try {
            hashedkey = SHA1(key);
        } catch (final NoSuchAlgorithmException e) {
            e.printStackTrace();
            return start;
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
            return start;
        }
        final String[] temp = start.split(delim);
        password = new byte[temp.length];
        for (i = 0; i < hashedkey.length; i++) {
            final int temp2 = Integer.parseInt(temp[i]);
            if (hashedkey[i] == temp2) {
                break;
            } else {
                password[i] = (byte) (temp2 - hashedkey[i]);
            }
        }
        return new String(password, 0, i);
    }

Thanks for any advice.

谢谢你的建议。

1 个解决方案

#1


0  

The hardest part here seems to be redoing the SHA1 method. You just need to find the equivalent .NET library classes/methods. Judging from the names, you probably want the System.Text.Encoding class and the System.Security.Cryptography.SHA1 class. Off hand, the algorithm probably ends up something like this

这里最难的部分似乎是重做SHA1方法。您只需要找到等效的.NET库类/方法。从名称来看,您可能需要System.Text.Encoding类和System.Security.Cryptography.SHA1类。在手边,算法可能会结束这样的事情

Private Shared Function SHA1(input As String) As Byte()
    Dim iso8859 = System.Text.Encoding.GetEncoding("iso-8859-1")
    Dim inBytes = ios8859.GetBytes(input)
    ' This is one implementation of the abstract class SHA1.'
    Dim sha As New SHA1CryptoServiceProvider()
    Return sha.ComputeHash(data)
End Function

From there you should be able to convert the rest of the decryptSHA1 function yourself as it is just basic byte manipulation. I will note that the GetEncoding function says it throws ArgumentException if you pass an invalid code page name and there does not seem to be any equivalent exception for NoSuchAlgorithmException to worry about catching.

从那里你应该能够自己转换其余的decryptSHA1函数,因为它只是基本的字节操作。我将注意到GetEncoding函数表示如果传递无效的代码页名称它会抛出ArgumentException,并且NoSuchAlgorithmException似乎没有任何等效的异常来担心捕获。

#1


0  

The hardest part here seems to be redoing the SHA1 method. You just need to find the equivalent .NET library classes/methods. Judging from the names, you probably want the System.Text.Encoding class and the System.Security.Cryptography.SHA1 class. Off hand, the algorithm probably ends up something like this

这里最难的部分似乎是重做SHA1方法。您只需要找到等效的.NET库类/方法。从名称来看,您可能需要System.Text.Encoding类和System.Security.Cryptography.SHA1类。在手边,算法可能会结束这样的事情

Private Shared Function SHA1(input As String) As Byte()
    Dim iso8859 = System.Text.Encoding.GetEncoding("iso-8859-1")
    Dim inBytes = ios8859.GetBytes(input)
    ' This is one implementation of the abstract class SHA1.'
    Dim sha As New SHA1CryptoServiceProvider()
    Return sha.ComputeHash(data)
End Function

From there you should be able to convert the rest of the decryptSHA1 function yourself as it is just basic byte manipulation. I will note that the GetEncoding function says it throws ArgumentException if you pass an invalid code page name and there does not seem to be any equivalent exception for NoSuchAlgorithmException to worry about catching.

从那里你应该能够自己转换其余的decryptSHA1函数,因为它只是基本的字节操作。我将注意到GetEncoding函数表示如果传递无效的代码页名称它会抛出ArgumentException,并且NoSuchAlgorithmException似乎没有任何等效的异常来担心捕获。