为什么加密和base64编码的文本在Windows和Linux上显示不同 [英] Why encrypted and base64 encoded text appears different on Windows and Linux

查看:137
本文介绍了为什么加密和base64编码的文本在Windows和Linux上显示不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个遗留系统,使用hibernate拦截器对一些数据库表上的某些字段进行加密(和编码)和解密(和解码)。它使用OnSave,OnLoad和OnFlushDirty方法。当从该系统读取的数据被传输到另一个应用程序时,这个代码证明是错误的,但是还有一些记录被加密和编码(一些加密了多次)。我这里的挑战是,当接收应用程序在Windows机器上时,我可以执行解密和解码(根据需要多次)。我收到一个BadPaddingException当我尝试重复同样的事情,当接收应用程序是一个linux VM。



任何帮助/建议将不胜感激



这里是hibernate拦截器的一个代码片段

  public boolean onLoad(Object entity,Serializable arg1 ,Object [] state,String [] propertyNames,Type [] arg4)throws CallbackException {
if(key!= null){
try {
if(entity instanceof BasicData){$ b (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);
}
}
返回true;
}
} catch(Exception e){
e.printStackTrace();
}} return false;}


解决方案

您控制双方,编码和解码,更好的方式使用DatatypeConverter:

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


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. 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. I get a BadPaddingException when I try to repeat the same thing when the receiving application is a linux VM.

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;}

解决方案

If you control both sides, encode and decode, better way to use DatatypeConverter:

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

这篇关于为什么加密和base64编码的文本在Windows和Linux上显示不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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