简体   繁体   English

Glassfish和Tomcat中的decodeBase64的不同结果

[英]Different results for decodeBase64 in Glassfish and Tomcat

I have a value that i encode with special characters, then encode it again using base64, my development environment is the standard uses Netbeans glassfish server, but site is hosted on Tomcat server, I seem to get different results for exactly the same decryption code/function, it works perfect in netbeans, but on tomcat fails, making my special character decode code fail as characters are not the same 我有一个值,我用特殊字符编码,然后使用base64再次编码,我的开发环境是标准使用Netbeans glassfish服务器,但是站点托管在Tomcat服务器上,对于完全相同的解密代码,我似乎会得到不同的结果/功能,它在netbeans中完美运行,但是在tomcat上失败,由于字符不同,我的特殊字符解码代码失败

String key = enc_key;

// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");

// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
strDecrypted = new String(cipher.doFinal(Base64.decodeBase64(encrypted.getBytes())));

Any suggestions :-( ? 有什么建议么 :-( ?

You're using the String(byte[]) constructor without specifying an encoding. 您正在使用String(byte[])构造函数,但未指定编码。 Don't do that. 不要那样做

Likewise, don't call String.getBytes() without specifying the encoding, either. 同样,也不要在未指定编码的情况下调用String.getBytes() You're doing that twice in the code you've shown us - and my guess is that you did it when encrypting the data. 您在显示给我们的代码中做了两次-我的猜测是您在加密数据时做到了。 Always specify the encoding you want to use. 始终指定要使用的编码。

Now we don't know which Base64 class you're using, but I would personally try to find a method which accepts a String to decode to start with - Base64 is logically a byte[] <==> String conversion scheme, so in a decent API you'd have String encode(byte[]) and byte[] decode(String) . 现在我们不知道您使用的是哪个Base64类,但是我个人将尝试找到一个接受String进行解码的方法来开始-Base64在逻辑上是一个byte[] <==> String转换方案,因此在一个不错的API,您需要使用String encode(byte[])byte[] decode(String) String encode(byte[]) byte[] decode(String)

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

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