openfire登录验证AuthProvider

来源:互联网 发布:淘宝代付超限 编辑:程序博客网 时间:2024/06/10 22:45

openfire登录验证AuthProvider

http://blog.csdn.net/linyu19872008/article/details/19109771

以前一直发现openfire数据库里面的密码是什么加密格式的,但是总弄不清楚,即便是同样的明文密码,在数据库中保存的结果却是不一样的。

今天又从登录入口查找,查到DefaultAuthProvider中的

public String getPassword(String username) throws UserNotFoundException 

方法。在其中设置如下代码


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. String plainText = rs.getString(1);  
  2.             String encrypted = rs.getString(2);  
  3.             if (encrypted != null) {  
  4.                 try {  
  5.                     String plainPass = AuthFactory.decryptPassword(encrypted);  
  6.                     System.out.println(plainPass);  
  7.                     System.out.println(AuthFactory.encryptPassword(plainPass));  
  8.                     System.out.println(AuthFactory.encryptPassword(plainPass));  
  9.                     System.out.println(AuthFactory.encryptPassword(plainPass));  
  10.                     return AuthFactory.decryptPassword(encrypted);  
  11.                 } catch (UnsupportedOperationException uoe) {  
  12.                     // Ignore and return plain password instead.  
  13.                 }  
  14.             }  

打印的结果如下:

123456

cb669f302c1ae6c20d703cf985b4cfb416bf14c308cae75c

42695c45901a59f58f6594a25ee186aaef22af3039b1fdea

170658949dbcb31a4b6656fdd748f1bc6414f5f1df226211

 

发现即便是同样的明文密码,经过相同的加密方法,得到的结果却不一样。

于是,我查找到了AuthFactory中的


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static String decryptPassword(String encryptedPassword) {  
  2.         if (encryptedPassword == null) {  
  3.             return null;  
  4.         }  
  5.         Blowfish cipher = getCipher();  
  6.         if (cipher == null) {  
  7.             throw new UnsupportedOperationException();  
  8.         }  
  9.         return cipher.decryptString(encryptedPassword);  
  10.     }  
  11.   
  12.  private static synchronized Blowfish getCipher() {  
  13.         if (cipher != null) {  
  14.             return cipher;  
  15.         }  
  16.         // Get the password key, stored as a database property. Obviously,  
  17.         // protecting your database is critical for making the  
  18.         // encryption fully secure.  
  19.         String keyString;  
  20.         try {  
  21.             keyString = JiveGlobals.getProperty("passwordKey");  
  22.             if (keyString == null) {  
  23.                 keyString = StringUtils.randomString(15);  
  24.                 JiveGlobals.setProperty("passwordKey", keyString);  
  25.                 // Check to make sure that setting the property worked. It won't work,  
  26.                 // for example, when in setup mode.  
  27.                 if (!keyString.equals(JiveGlobals.getProperty("passwordKey"))) {  
  28.                     return null;  
  29.                 }  
  30.             }  
  31.             cipher = new Blowfish(keyString);  
  32.         }  
  33.         catch (Exception e) {  
  34.             Log.error(e.getMessage(), e);  
  35.         }  
  36.         return cipher;  
  37.     }  

 由以上代码可以看出,加密以及解密过程都是由 Blowfish 方法提供。

 

我又将上面加密的密码做了一个解密的例子


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class Test1 {  
  2.   
  3.     public static void main(String[] args) {  
  4.         Blowfish blow = new Blowfish("4dbKrqSsqQdUk3c");  
  5.         System.out  
  6.                 .println(blow  
  7.                         .decryptString("c96d8464e6d01a70f39a2e44c4a5cc634b367e021b3a5f20"));  
  8.         System.out  
  9.                 .println(blow  
  10.                         .decryptString("42d812e85a602f479871acebe0a3987658abb6184fbb132f"));  
  11.         System.out  
  12.                 .println(blow  
  13.                         .decryptString("1c037aafe1fc7d653cd9bfab7f126aeba9a4abadfe479c8f"));  
  14.     }  
  15.   
  16. }  

输出如下:

123456

123456

123456

 

 由此可以看出,只要系统属性的passwordKey值不变化,无论加过密的密码是啥样子的,只要明文相同,就可以恢复成原来相同的明文

此特性是blowfish提供

 

Blowfish是1993年布鲁斯·施奈尔开发的对称密钥区块加密算法,区块长为64位,密钥为1至448位的可变长度。[1]DES等算法相比,其处理速度较快。因为其无须授权即可使用,作为一种自由授权的加密方式在SSH、文件加密软件等被广泛地使用。



0 0
原创粉丝点击