开发者

Java to VB.Net Conversion of This Function

开发者 https://www.devze.com 2023-02-06 17:47 出处:网络
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:

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:

    private static byte[] SHA1(final String in)
            throws NoSu开发者_StackOverflowchAlgorithmException, 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.


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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消