简体   繁体   English

为什么在Windows和Linux上加密和以base64编码的文本看起来有所不同

[英]Why encrypted and base64 encoded text appears different on Windows and Linux

I have a legacy system that uses hibernate interceptor to encrypt (and encode) and decrypt (and decode) some fields on some database tables. 我有一个旧系统,该系统使用休眠拦截器对某些数据库表上的某些字段进行加密(编码)和解密(解码)。 It makes use of the OnSave, OnLoad and OnFlushDirty methods. 它利用OnSave,OnLoad和OnFlushDirty方法。 This code turns out to be buggy as data read from this system, when transferred to another application still has some of the records encrypted and encoded (some encrypted multiple times). 由于从该系统读取的数据转移到另一个应用程序时,仍然会对某些记录进行加密和编码(有些加密多次),因此该代码是有缺陷的。 The challenge for me here is that I could perform the decryption and decoding (as many times as necessary) when the receiving application is on a Windows machine. 对我来说,这里的挑战是,当接收应用程序在Windows计算机上时,我可以执行解密和解码(必要时进行多次)。 I get a BadPaddingException when I try to repeat the same thing when the receiving application is a linux VM. 当接收应用程序是linux VM时,我尝试重复同样的事情时,出现BadPaddingException。

Any help/suggestions will be greatly appreciated 任何帮助/建议将不胜感激

here is a snippet of the hibernate interceptor 这是休眠拦截器的一小段

public boolean onLoad(Object entity, Serializable arg1, Object[] state, String[]  propertyNames, Type[] arg4) throws CallbackException {
if (key != null){
 try {
  if (entity instanceof BasicData) {
   for (int i = 0; i < state.length; i++) {

     if (state[i] instanceof String){
       String cipherText = (String)state[i];
       byte[] cipherTextBytes = Base64Coder.decode(cipherText);
       byte[] plainTextBytes = dCipher.doFinal(cipherTextBytes);
       state[i] = new String(plainTextBytes, "UTF8");
    }
 }
 return true;
}
} catch (Exception e) {
  e.printStackTrace();
}}return false;}

I'd have to guess here but if you mean this Base64Coder the problem might be the following: 我不得不在这里猜测,但是如果您是说这个Base64Coder,那么问题可能是以下原因:

It is unclear how the base64 string has been created, ie which encoding had been used. 目前尚不清楚如何创建base64字符串,即使用了哪种编码。 If you use UTF-8 to get the bytes of a string and create a base64 from those bytes you'll get a different result than if you'd use ISO Latin-1, for example. 例如,如果您使用UTF-8获取字符串的字节并从这些字节创建base64,则将获得与使用ISO Latin-1的结果不同的结果。

Afterwards you create a string from those bytes using UTF-8, but if the base64 string had not been created using UTF-8, you'll get wrong results. 然后,您使用UTF-8从这些字节创建一个字符串,但是如果未使用UTF-8创建base64字符串,则会得到错误的结果。

Just a quote from the linked source (if this is the correct one): 只是来自链接源的报价(如果这是正确的报价):

 public static String encodeString (String s) {
   return new String(encode(s.getBytes())); }

Here, s.getBytes() will use the system's/jvm's default encoding, so you should really ensure it is UTF-8! 在这里, s.getBytes()将使用系统的/ JVM的默认编码,所以你应该确保它是UTF-8!

If you control both sides, encode and decode, better way to use DatatypeConverter: 如果控制双方,则进行编码和解码,这是使用DatatypeConverter的更好方法:

String          buffer          = DatatypeConverter.printBase64Binary( symKey );
byte[]          supposedSymKey  = DatatypeConverter.parseBase64Binary( buffer );

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

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