Java代碼應該具有與C#代碼相同的功能。這段代碼是否一樣?將C#snippit轉換爲Java
這是在C#代碼代碼:
byte[] hashBytes;
UnicodeEncoding encoding = new UnicodeEncoding();
hashBytes = encoding.GetBytes(inputstr.Text.ToUpper().Trim());
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] cryptPassword = sha1.ComputeHash(hashBytes);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
SHA1.Text = enc.GetString(cryptPassword);
outputstr.Text = Convert.ToBase64String(cryptPassword);
這裏是Java的移植代碼,但我得到不同的輸出:
byte[] pwBytes = new String("password".toUpperCase().getBytes(), "UTF-16").getBytes();
MessageDigest md = null;
md = MessageDigest.getInstance("SHA-1");
byte[] sha1pw = md.digest(pwBytes);
final BASE64Encoder encoder = new BASE64Encoder();
String encodedPw = encoder.encode(sha1pw);
的Java代碼應該計算encodedPw以同樣的方式C#代碼計算outputstr.Text。
對不起,我無法運行C#代碼來提供示例。 Java代碼散列「密碼」爲oghZbO1T3U/eu3POLIIQweZ/gvQ=
。
你能告訴我們不同的輸出嗎? –
對不起,這個例子是密碼,所以我不能提供它。我編輯了這個問題。 – jcalfee314
那麼你是說上面的兩段代碼用密碼「password」* do *產生相同的正確輸出嗎? – berry120