频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

给hibernate配置文件加密、解密的方案

来源: 作者:(来自javaeye论坛) 出处:巧巧读书 2008-02-01 进入讨论组
下一页 1 2 

    之前有人问过hibernate密码问题,大家都没有给出一个具体的解决方案,所以我就看了一下代码,把我的想法和实现拿出来和大家讨论一下。我现在的环境是spring+hibernate,但是这并不影响这个加密解密的问题,其他环境应该是略有不同,但是思路肯定是一样的。

总体思路:在工程的配置文件中填写数据库密码的密文,在应用程序使用datasource的时候解密成明文以创建连接。

步骤1
使用java的中cipher类并使用DES(对称加密算法)算法对明文进行加密
````````````````这里如何使用cipher类和DES算法的原理可以上网查找,我懒得写了,如果大家真的也怕麻烦自己去找的话我再写一个贴出来吧

1public class DESUtil ...{ 2 3 public static void main(String[] args)...{ 4 try ...{ 5 if(args[0].equals("-genKey"))...{ 6 generateKey(args[1]); 7 }else...{ 8 9 //if (args[0].equals("-encrypt"))encrypt(); 10 11 //else decrypt(); 12 } 13 }catch (Exception e) ...{ 14 // TODO: handle exception 15 } 16 } 17 18 public static String encrypt(String plainText, String encryptString, File keyFile)throws IOException, ClassNotFoundException,GeneralSecurityException...{ 19 ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(keyFile)); 20 int mode = Cipher.ENCRYPT_MODE; 21 Key key = (Key)keyIn.readObject(); 22 keyIn.close(); 23 24 InputStream in = new FileInputStream(plainText); 25 OutputStream out = new FileOutputStream(encryptString); 26 27 Cipher cipher = Cipher.getInstance("DES"); 28 cipher.init(mode, key); 29 30 doEncryptAndDecrypt(in, out, cipher); 31 32 String result = out.toString(); 33 System.out.print(result); 34 in.close(); 35 out.close(); 36 return result; 37 } 38 39 public static String decrypt(String encryptString, String plainText, File keyFile)throws IOException, ClassNotFoundException,GeneralSecurityException...{ 40 ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(keyFile)); 41 int mode = Cipher.DECRYPT_MODE; 42 Key key = (Key)keyIn.readObject(); 43 keyIn.close(); 44 45 InputStream in = new FileInputStream(encryptString); 46 OutputStream out = new FileOutputStream(plainText); 47 48 Cipher cipher = Cipher.getInstance("DES"); 49 cipher.init(mode, key); 50 51 doEncryptAndDecrypt(in, out, cipher); 52 53 String result = out.toString(); 54 System.out.print(result); 55 in.close(); 56 out.close(); 57 return result; 58 } 59 60 public static void doEncryptAndDecrypt(InputStream in, OutputStream out, Cipher cipher)throws IOException, GeneralSecurityException...{ 61 int blockSize = cipher.getBlockSize(); 62 int outputSize = cipher.getOutputSize(blockSize); 63 64 byte[] inBytes = new byte[blockSize]; 65 byte[] outBytes = new byte[outputSize]; 66 67 int inLength = 0; 68 boolean more = true; 69 70 while(more)...{ 71 inLength = in.read(inBytes); 72 if(inLength == blockSize)...{ 73 int outLength = cipher.update(inBytes, 0, blockSize, outBytes); 74 out.write(outBytes,0,outLength); 75 } 76 else more = false; 77 } 78 79 if(inLength>0) outBytes = cipher.doFinal(inBytes, 0, inLength); 80 else outBytes = cipher.doFinal(); 81 82 out.write(outBytes); 83 84 } 85 86 public static void generateKey(String path) throws Exception...{ 87 KeyGenerator keygen = KeyGenerator.getInstance("DES"); 88 SecureRandom random = new SecureRandom(); 89 90 keygen.init(random); 91 SecretKey key = keygen.generateKey(); 92 93 ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(path)); 94 out.writeObject(key); 95 out.close(); 96 } 97 98} 99
通过以上的encrypt方法得到一个密码的密文(一般的密码是明文,作为参数传进去可以得到对应的密文),比如说21sadf25收藏 http://www.qqread.com/java/2008/02/c395158.html 更多文章 更多内容请看电脑配置手册服务器配置专栏加密与解密技术专题,或进入讨论组讨论。
下一页 1 2 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章