web项目加解密

环境:tomcat6.X+struts2.X+MyIbatis+Spring3.x

加密:使用AES加密,将文件的字节码读取,对字节码进行加密后替换源文件

Java代码 
  1. /** 
  2.      *  
  3.      * 字节加密 
  4.      */  
  5.     public static byte[] encrypt(byte[] data, String key) throws Exception {  
  6.         Key k = toKey(Base64.decode(key));  
  7.         byte[] raw = k.getEncoded();  
  8.         SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);  
  9.         Cipher cipher = Cipher.getInstance(ALGORITHM);  
  10.         cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);  
  11.         return cipher.doFinal(data);  
  12.     }  

 解密:

1、在tomcat的WebappClassLoader中修改源码(自动义类加载器);

2、修改spring源码Code包源码。

加密方法

Java代码 
  1. public static byte[] decrypt(byte[] data, String key) throws Exception {  
  2.         Key k = toKey(Base64.decode(key));  
  3.         byte[] raw = k.getEncoded();  
  4.         SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);  
  5.         Cipher cipher = Cipher.getInstance(ALGORITHM);  
  6.         cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);  
  7.         return cipher.doFinal(data);  
  8.     }  

在 WebappClassLoader中解密

Java代码 
  1. /** 
  2.              * 判断如需是需要解密的类进行数据处理 
  3.              * */  
  4.             //--------------------------------------start----------------------------------//  
  5.             byte []data=null;  
  6.             try {  
  7.                 if(isDecode(name)){  
  8.                     System.out.println("2818:--&&&-"+name);  
  9.                     data=AESUtils.decrypt(entry.binaryContent, key);  
  10.                 }else{  
  11.                     data=entry.binaryContent;  
  12.                 }  
  13.             } catch (Exception e) {  
  14.                 e.printStackTrace();  
  15.             }  
  16.             try {  
  17.                   clazz = defineClass(name, data, 0,  
  18.                           data.length,   
  19.                           new CodeSource(entry.codeBase, entry.certificates));  
  20.                   //--------------------------------------end----------------------------------//  

 在spring的code包的SimpleMetadataReader修改器构造函数

Java代码 
  1. // TODO 修改源码判断是否需要解密  
  2.     SimpleMetadataReader(Resource resource, ClassLoader classLoader)  
  3.             throws IOException {  
  4.         InputStream is = resource.getInputStream();  
  5.         ClassReader classReader = null;  
  6.         try {  
  7.             String name = "";  
  8.             if (resource.getURI().toString().indexOf("jar:file") == -1) {  
  9.                 name = resource.getFile().getAbsolutePath();  
  10.                 if (!"".equals(name) && isDecode(name, cams)) {  
  11.                     byte[] data = inputStreamToByte(is);  
  12.                     try {  
  13.                         is = new ByteArrayInputStream(AESUtils.decrypt(data,  
  14.                                 key));  
  15. //                       is = new ByteArrayInputStream(data);  
  16.                     } catch (Exception e) {  
  17.                         e.printStackTrace();  
  18.                     }  
  19.                 }  
  20.             }  
  21.             classReader = new ClassReader(is);  
  22.         } finally {  
  23.             is.close();  
  24.         }  

 

注:(此加密有弊端)

1、加密解密算法需保持一致。

2、加密加密密钥需是同一密钥。     

 

你可能感兴趣的:(web项目加解密)