[英]Java to VB.Net Conversion of This Function
我在這里有一個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);
}
感謝您的任何建議。
這里最難的部分似乎是重做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
從那里,您應該能夠自己轉換其余的decryptSHA1
函數,因為它只是基本的字節操作。 我將注意到, GetEncoding
函數表示,如果您傳遞了無效的代碼頁名稱,它將引發ArgumentException
,並且NoSuchAlgorithmException
似乎沒有任何等效的異常要擔心被捕獲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.