CryptoJS.enc.Base64.stringify()和正常的Base64加密之间的区别 [英] Difference between CryptoJS.enc.Base64.stringify() and normal Base64 encryption

查看:10697
本文介绍了CryptoJS.enc.Base64.stringify()和正常的Base64加密之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  6bb984727b8c8c8017207e54b63976dc42ea9d24ad33bd5feeaa66869b650096 

需要访问网站的API。该网站使用 CryptoJS.enc.Base64.stringify()方法在JavaScript中显示一个示例脚本来加密哈希。
此方法的结果是

  a7mEcnuMjIAXIH5Utjl23ELqnSStM71f7qpmhptlAJY = 

然而,我尝试的每个在线base64加密工具给了我以下结果:

  NmJiOTg0NzI3YjhjOGM4MDE3MjA3ZTU0YjYzOTc2ZGM0MmVhOWQyNGFkMzNiZDVmZWVhYTY2ODY5YjY1MDA5Ng == 

我需要用C ++创建编码的字符串。我也已经尝试过4种不同的base64encode实现(OpenSSL和自定义代码),但是我也得到了上述结果,API总是回答我的字符串编码不正确。



所以区别在哪里,并且有人知道 C ++实现C CryptoJS.enc.Base64.stringify()

解决方案

我们来调用

  a =6bb984727b8c8c8017207e54b63976dc42ea9d24ad33bd5feeaa66869b650096; 
b =a7mEcnuMjIAXIH5Utjl23ELqnSStM71f7qpmhptlAJY =;
c =NmJiOTg0NzI3YjhjOGM4MDE3MjA3ZTU0YjYzOTc2ZGM0MmVhOWQyNGFkMzNiZDVmZWVhYTY2ODY5YjY1MDA5Ng ==;

这两个转换都是正确的,但取决于您实际想要的。



例如,以下两个方程式包含

  toBase64FromBytes(toBytesFromUtf8(a))== c 
toBase64FromBytes(toBytesFromHex(a))== b

这是一个坏主意,在线计算器,因为他们通常不会透露他们如何编码的东西,所以你会得到任意的结果。如果你自己编程,你会得到预期的结果,如果你遵循的文件。



我怀疑你有 a 通过将哈希或加密结果打印到控制台,如下所示:

  console.log(result.toString()); // a 

CryptoJS中的大多数结果 WordArray 类型。当您在这样的对象上调用 toString()函数时,您将获得该二进制对象的十六进制编码字符串。



如果您打印 result.toString(CryptoJS.enc.Base64),则可以获得二进制结果的Base64编码字符串。



如果您将 a 直接编码到Base64,那么可能假设 a 已经是一个字符串(例如UTF-8编码)。在线计算器不知道它是十六进制编码的。


I'm trying to encrypt the following hash to base64:

6bb984727b8c8c8017207e54b63976dc42ea9d24ad33bd5feeaa66869b650096

It's needed to access the API of a website. The website shows an example script in JavaScript using the CryptoJS.enc.Base64.stringify() method to encrypt the hash. The result with this method is

a7mEcnuMjIAXIH5Utjl23ELqnSStM71f7qpmhptlAJY=

However, every online base64 encryption tool I tried gives me the following result:

NmJiOTg0NzI3YjhjOGM4MDE3MjA3ZTU0YjYzOTc2ZGM0MmVhOWQyNGFkMzNiZDVmZWVhYTY2ODY5YjY1MDA5Ng==

I need to create the encoded string in C++. I've also already tried 4 different base64encode implementations (OpenSSL and custom codes), but also there I get the above result and the API always answers my string is not correctly encoded.

So where is the difference, and does somebody know a C++ implementation for CryptoJS.enc.Base64.stringify()?

解决方案

Let's call

a = "6bb984727b8c8c8017207e54b63976dc42ea9d24ad33bd5feeaa66869b650096";
b = "a7mEcnuMjIAXIH5Utjl23ELqnSStM71f7qpmhptlAJY=";
c = "NmJiOTg0NzI3YjhjOGM4MDE3MjA3ZTU0YjYzOTc2ZGM0MmVhOWQyNGFkMzNiZDVmZWVhYTY2ODY5YjY1MDA5Ng==";

Both conversions are correct, but depend on what you actually want.

For example the following two equations hold

toBase64FromBytes(toBytesFromUtf8(a)) == c
toBase64FromBytes(toBytesFromHex(a)) == b

It's a bad idea to trust some kind of online calculator, because they usually don't disclose how they encode stuff, so you will get arbitrary results. If you program it yourself, you get the expected results if you follow the documentation.

I suspect you got a by printing a hash or encryption result to the console like this:

console.log(result.toString()); // a

Most result objects in CryptoJS are WordArray types. When you call the toString() function on such an object, you get a Hex-encoded string of that binary object.

If you print result.toString(CryptoJS.enc.Base64) then you get the Base64-encoded string of the binary result.

If you take a and directly encode it to Base64, then it is probably assumed that a is already a string (e.g. UTF-8 encoded). An online calculator doesn't know that it is Hex-encoded.

这篇关于CryptoJS.enc.Base64.stringify()和正常的Base64加密之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆