hibernate配置文件加密-base64

项目有个需求需要给hibernate配置文件的密码通过base64加密,贴下代码和配置文件

1.base64解密和加密:

public class Base64 {
 static String[] Base64Table = { "A", "B", "C", "D", "E", "F", "G", "H",
   "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
   "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h",
   "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
   "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
   "8", "9", "+", "/" };

 public static final String base64EncodeFile(String paramString) {
  try {
   File localFile = new File(paramString);
   FileInputStream localFileInputStream = new FileInputStream(
     paramString);

   byte[] arrayOfByte1 = new byte[(int) localFile.length()];
   int i = localFileInputStream.read(arrayOfByte1);
   localFileInputStream.close();
   if (i < 1) {
    return "";
   }
   byte[] arrayOfByte2 = base64Encode(arrayOfByte1);
   return new String(arrayOfByte2);
  } catch (Exception localException) {
  }
  return "";
 }

 public static final void base64DecodeFile(String paramString1,
   String paramString2) {
  try {
   byte[] arrayOfByte = base64Decode(paramString2.getBytes());
   FileOutputStream localFileOutputStream = new FileOutputStream(
     paramString1);
   localFileOutputStream.write(arrayOfByte);
   localFileOutputStream.close();
  } catch (IOException localIOException) {
  }
 }

 public static final byte[] base64Encode(byte[] paramArrayOfByte) {
  if (paramArrayOfByte == null) {
   return null;
  }

  byte[] arrayOfByte = new byte[(paramArrayOfByte.length + 2) / 3 * 4];

  int i = 0;
  int j = 0;
  for (; i < paramArrayOfByte.length - 2; i += 3) {
   arrayOfByte[(j++)] = (byte) ((paramArrayOfByte[i] & 0xFC) >>> 2);
   arrayOfByte[(j++)] = (byte) ((paramArrayOfByte[i] & 0x3) << 4 | (paramArrayOfByte[(i + 1)] & 0xF0) >>> 4);
   arrayOfByte[(j++)] = (byte) ((paramArrayOfByte[(i + 1)] & 0xF) << 2 | (paramArrayOfByte[(i + 2)] & 0xC0) >>> 6);
   arrayOfByte[(j++)] = (byte) (paramArrayOfByte[(i + 2)] & 0x3F);
  }

  if (i < paramArrayOfByte.length) {
   arrayOfByte[(j++)] = (byte) (paramArrayOfByte[i] >>> 2 & 0x3F);
   if (i < paramArrayOfByte.length - 1) {
    arrayOfByte[(j++)] = (byte) ((paramArrayOfByte[i] & 0x3) << 4 | (paramArrayOfByte[(i + 1)] & 0xF0) >> 4);
    arrayOfByte[(j++)] = (byte) (paramArrayOfByte[(i + 1)] << 2 & 0x3F);
   } else {
    arrayOfByte[(j++)] = (byte) (paramArrayOfByte[i] << 4 & 0x3F);
   }
  }
  for (int k = 0; k < j; ++k) {
   arrayOfByte[k] = Base64Table[arrayOfByte[k]].getBytes()[0];
  }

  for (; j < arrayOfByte.length; ++j) {
   arrayOfByte[j] = 61;
  }
  return arrayOfByte;
 }

 public static final byte[] base64Decode(byte[] paramArrayOfByte) {
  if (paramArrayOfByte == null) {
   return null;
  }
  int j = paramArrayOfByte.length * 3 / 4;
  byte[] arrayOfByte1 = new byte[paramArrayOfByte.length * 3 / 4];

  for (int k = 0; k < paramArrayOfByte.length; ++k) {
   if ((paramArrayOfByte[k] >= 65) && (paramArrayOfByte[k] <= 90)) {
    paramArrayOfByte[k] = (byte) (paramArrayOfByte[k] - 65);
   } else if ((paramArrayOfByte[k] >= 97)
     && (paramArrayOfByte[k] <= 122)) {
    paramArrayOfByte[k] = (byte) (paramArrayOfByte[k] - 71);
   } else if ((paramArrayOfByte[k] >= 48)
     && (paramArrayOfByte[k] <= 57)) {
    paramArrayOfByte[k] = (byte) (paramArrayOfByte[k] + 4);
   } else if (paramArrayOfByte[k] == 43) {
    paramArrayOfByte[k] = 62;
   } else {
    if (paramArrayOfByte[k] != 47)
     continue;
    paramArrayOfByte[k] = 63;
   }

  }

  int i = 0;
  for (j = 0; i < paramArrayOfByte.length - 4; i += 4) {
   arrayOfByte1[(j++)] = (byte) ((paramArrayOfByte[i] & 0xFF) << 2 | (paramArrayOfByte[(i + 1)] & 0xFF) >> 4);
   arrayOfByte1[(j++)] = (byte) ((paramArrayOfByte[(i + 1)] & 0xF) << 4 | (paramArrayOfByte[(i + 2)] & 0x3F) >> 2);
   arrayOfByte1[(j++)] = (byte) ((paramArrayOfByte[(i + 2)] & 0xFF) << 6 | paramArrayOfByte[(i + 3)]);
  }

  if (paramArrayOfByte[(i + 2)] == 61) {
   arrayOfByte1[(j++)] = (byte) ((paramArrayOfByte[i] & 0xFF) << 2 | (paramArrayOfByte[(i + 1)] & 0x30) >> 4);
  } else if (paramArrayOfByte[(i + 3)] == 61) {
   arrayOfByte1[(j++)] = (byte) ((paramArrayOfByte[i] & 0xFF) << 2 | (paramArrayOfByte[(i + 1)] & 0x30) >> 4);
   arrayOfByte1[(j++)] = (byte) ((paramArrayOfByte[(i + 1)] & 0xF) << 4 | (paramArrayOfByte[(i + 2)] & 0x3F) >> 2);
  } else {
   arrayOfByte1[(j++)] = (byte) ((paramArrayOfByte[i] & 0xFF) << 2 | (paramArrayOfByte[(i + 1)] & 0xFF) >> 4);
   arrayOfByte1[(j++)] = (byte) ((paramArrayOfByte[(i + 1)] & 0xF) << 4 | (paramArrayOfByte[(i + 2)] & 0x3F) >> 2);
   arrayOfByte1[(j++)] = (byte) ((paramArrayOfByte[(i + 2)] & 0xFF) << 6 | paramArrayOfByte[(i + 3)]);
  }

  byte[] arrayOfByte2 = new byte[j];
  for (int l = 0; l < j; ++l) {
   arrayOfByte2[l] = arrayOfByte1[l];
  }
  return arrayOfByte2;
 }
 
 /**
  * Base64加密String字符串

  * @param str
  * @return String
   */
 public static String encodeString(String str){
  if (str == null || str.equals("")) {
   return "";
   }
  BASE64Encoder encoder = new BASE64Encoder();
  return encoder.encode(str.getBytes());
 }
 
 /**
  * Base64解密String字符串

  * @param str
  * @return
  */
 public static String decodeString(String str){
  if (str == null || str.equals("")) {
   return "";
   }
  BASE64Decoder decoder = new BASE64Decoder();
  try {
   byte[] b = decoder.decodeBuffer(str);
   return new String(b);
  } catch (IOException e) {
   e.printStackTrace();
  }
  return "";
 }

 public static void main(String[] paramArrayOfString) {
//  String str = base64EncodeFile("1.frf");
//  System.out.println("str==");
//  base64DecodeFile("2.frf", str);
  String s = "12345678";
  String s1 = Base64.encodeString(s);
  System.out.println(s1);
  System.out.println(Base64.decodeString(s1));
 }
}

 

2.配置文件:applicationContext-db.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <!-- jdbcTemplate -->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <!-- Hibernate SessionFactory
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> -->
 <bean id="sessionFactory" class="com.ylink.util.MyLocalSessionFactoryBean">
 
  <property name="dataSource" ref="dataSource" />
  <property name="mappingLocations">
   <list>
    <value>classpath*:/com/ylink/hibernate/hbm/*.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
   <!--  <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>  -->
   </props>
  </property>
 </bean>
 <!-- Hibernate TransactionManager -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
  <property name="nestedTransactionAllowed" value="true" />
 </bean>
</beans>

3.继承LocalSessionFactoryBean类,重写setDataSource方法,解密password:

 

public class MyLocalSessionFactoryBean extends LocalSessionFactoryBean {

 @Override
 public void setDataSource(DataSource dataSource) {
  // TODO Auto-generated method stub
  BasicDataSource bds = (BasicDataSource)dataSource;
  String password = bds.getPassword();
  String psd = Base64.decodeString(password);
  bds.setPassword(psd);
  super.setDataSource(bds);
 }
 
}

你可能感兴趣的:(hibernate配置文件加密-base64)