javascript编码无法正常工作

时间:2013-12-12 16:46:00

标签: javascript ruby encoding decoding cryptojs

我在JavaScript中遇到编码问题,我没有得到预期的结果

这里有一些我想列出的东西 - 1)当我在ruby中进行编码然后获得预期结果时。 - 2)但是当我想在JavaScript中遵循相同的步骤然后没有得到预期的结果


1)请在下面找到正常工作的红宝石代码。

require 'openssl'
require "base64"
key =  Base64.decode64("yZQungXDXtksG4Ypcl0f6moRXWTCHh/CYDViXnZvbC0=")
data = "<content><app-id>9cd73144-46ca-4a02-852b-fa452b1d8bdc</app-id><hmac>HMACSHA256</hmac><signing-time>2013-11-19T12:06:11.409Z</signing-time></content>"
result  = OpenSSL::HMAC.digest('sha256', key , data)
final_result = Base64.encode64(result)

ruby​​输出:收到预期输出

Key =  "\xC9\x94.\x9E\x05\xC3^\xD9,\e\x86)r]\x1F\xEAj\x11]d\xC2\x1E\x1F\xC2`5b^vol-"
result = "\xC4\x9B\x94\x9C\anQT\xF9';\xE9$\x1C\x98k\xEE)\xD77\xFD\xCA\a\xD1L\xBB\x9B\xD2r\xE9\x1A\xA8"
final_result = "xJuUnAduUVT5JzvpJByYa+4p1zf9ygfRTLub0nLpGqg=\n"

2)请在下面找到未按预期运行的JavaScript代码

  • 使用crypo.js进行编码和解码

<html>
  <head>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script>

    <script>
      key = window.atob("yZQungXDXtksG4Ypcl0f6moRXWTCHh/CYDViXnZvbC0=");
      data = "<content><app-id>9cd73144-46ca-4a02-852b-fa452b1d8bdc</app-id><hmac>HMACSHA256</hmac><signing-time>2013-11-19T12:06:11.409Z</signing-time></content>";
      console.log("key");
      console.log(key);
      result = CryptoJS.HmacSHA256(data, key);
      console.log("result");
      console.log(result.toString());
      final_result = CryptoJS.enc.Base64.stringify(result);
      console.log("final_result");
      console.log(final_result.toString());
    </script>
  </head>
  <body>
  </body>
</html>

Javascript输出:收到预期输出

Key =  "É.Ã^Ù,)r]êj]dÂÂ`5b^vol-"
result = "035a028de6bea2c7843b4310b28b57f5193d7597840ea2f23c255cb889d77d60"
final_result = "A1oCjea+oseEO0MQsotX9Rk9dZeEDqLyPCVcuInXfWA="

所以在这里我不理解,为什么我在key

中得到resultfinal_resultruby and JavaScript不同

请分享您的经验,这将有助于我很多

谢谢

2 个答案:

答案 0 :(得分:0)

我已经尝试了很多并找到了以下解决方案。

<html>
  <head>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script>
    <script src="jquery.base64.js"></script>
    <script>
      var key = Base64.decode("yZQungXDXtksG4Ypcl0f6moRXWTCHh/CYDViXnZvbC0=");
      console.log(key)
      words = CryptoJS.enc.Latin1.parse(key);
      words = CryptoJS.enc.Hex.parse(words.toString());
     result = CryptoJS.HmacSHA256("<content><app-id>9cd73144-46ca-4a02-852b-fa452b1d8bdc</app-id><hmac>HMACSHA256</hmac><signing-time>2013-11-19T12:06:11.409Z</signing-time></content>", words);
     console.log(result);
     final_result = CryptoJS.enc.Base64.stringify(result);
     console.log(final_result);
   </script>
 </head>
</html>

现在我得到了预期的结果。

key = "É.Ã^Ù,)r]êj]dÂÂ`5b^vol-"
result = "c49b949c076e5154f9273be9241c986bee29d737fdca07d14cbb9bd272e91aa8"
final_result = "xJuUnAduUVT5JzvpJByYa+4p1zf9ygfRTLub0nLpGqg="

答案 1 :(得分:-1)

您可能需要使用Base64.strict_encode64(http://ruby-doc.org/stdlib-1.9.3/libdoc/base64/rdoc/Base64.html#method-i-strict_encode64) strict_encode完全符合RFC,而.encode则不然。大多数实现并不关心这个特定的问题(必须使用新的行字符),但有些实现。