简体   繁体   English

得到Java和Z686155AF75A60A0D36E9D80C1F7中加密字符串的Base64编码output相同

[英]Obtaining same Base64 encoded output of encrypted string in Java and JavaScript

After getting byte array encryptedMessageInBytes from AES encryption function call cipher.doFinal in Java, I convert the byte array to base64 like this:从 AES 加密 function 中获取字节数组encryptedMessageInBytes后,在 Java 中调用cipher.doFinal ,我将字节数组转换为 Z95A1446A7120E4AF5C2Z,如下所示:

String encryptedMessageInBase64 = new String(Base64.getEncoder().encode(encryptedMessageInBytes));

In JavaScript, I simply do .toString() to the output I get from CryptoJS.AES.encrypt method and I get exact same base64 string.在 JavaScript 中,我只需对从CryptoJS.AES.encrypt方法得到的 output 执行.toString()即可,得到完全相同的 base64 字符串。 ie IE

var encryptedMessageInBase64 = CryptoJS.AES.encrypt("Message", "Secret Passphrase").toString();

It gives same base64 string as in Java code.它给出了与 Java 代码相同的 base64 字符串。

However, in one of the Java source code, they have done like this:但是,在 Java 源代码之一中,他们这样做了:

String encryptedMessageInBase64 = Base64.getUrlEncoder().encodeToString(encryptedMessageInBytes);

What shall I do in JavaScript to obtain same base64 string?我应该在 JavaScript 中做什么以获得相同的 base64 字符串?

Here is answer:这是答案:

However, in one of the Java source code, they have done like this:但是,在 Java 源代码之一中,他们这样做了:

String encryptedMessageInBase64 = Base64.getUrlEncoder().encodeToString(encryptedMessageInBytes);*字符串 encryptedMessageInBase64 = Base64.getUrlEncoder().encodeToString(encryptedMessageInBytes);*

Here, basically they have done UrlEncoding instead of Base64 encoding.在这里,基本上他们已经做了UrlEncoding而不是 Base64 编码。 It is nothing but replacing + and / characters with - and _ characters.它只不过是用-_字符替换+/字符。 Url encoding is when we want to send encoded string over HTTP where it treats these two as special characters this is why we need to replace them with some other characters which are HTTP friendly. Url 编码是当我们想通过 HTTP 发送编码字符串时,它将这两个视为特殊字符,这就是为什么我们需要用 HTTP 友好的其他字符替换它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM