为什么使用CryptoJS的base64与标准base64编码不同?

时间:2016-01-21 17:00:34

标签: javascript encoding base64 cryptojs

要与服务器通信,我需要发送密码SHA1& base64以与CryptoJS相同的方式编码。

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

我如何在.NET中使用与CryptoJS编码相同的方式对ASCII字符串进行base64编码?

您可以在此处查看两个结果: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

1 个答案:

答案 0 :(得分:2)

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