2016-01-21 194 views
0

要与服务器通信,我需要发送密码SHA1 & base64编码方式与CryptoJS相同。为什么base64使用CryptoJS与标准的base64编码不同?

我的问题是,我正在使用VB.NET。典型的base64编码(UTF-8)结果与CryptoJS的结果不同。

我该如何使用CryptoJS对它进行编码,以base64对.NET中的SHA1字符串进行编码?

你可以看到两个结果在这里:https://jsfiddle.net/hn5qqLo7/

var helloworld = "Hello World"; 
var helloword_sha1 = CryptoJS.SHA1(helloworld); 
document.write("SHA1: " + helloword_sha1); 

var helloword_base64 = helloword_sha1.toString(CryptoJS.enc.Base64); 
document.write("1) Base64: " + helloword_base64); 
document.write("2) Base64: " + base64_encode(helloword_sha1.toString())); 

其中base64_encode一个给定的字符串到Base 64编码字符串转换。

我看到了类似的问题,但我不明白。 Decode a Base64 String using CryptoJS

回答

2

在您的小提琴的(1)中,CryptoJS计算字符串的SHA1值,然后将原始字节转换为Base64。然而,(2)首先计算'Hello World'的SHA1值,然后将其以十六进制格式(仅包含0-9和a-f)放入,然后将此十六进制格式的SHA1转换为base64。所以这就是为什么你最终得到两个不同的结果。