[英]How to retrieve LDAP password via JNDI
我能夠通過JNDI讀取存儲在LDAP中的密碼。 但結果是一些胡言亂語的人物。 那么我該怎么解密呢?
以下是我的代碼:
public static void main(String[] args)
{
String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
String MY_HOST = "ldap://KhooGP-Comp1:1389";
String MGR_DN = "cn=Directory Manager";
String MGR_PW = "password";
String MY_SEARCHBASE = "dc=QuizPortal";
String MY_FILTER = "uid=yiwei";
String MY_ATTRS[] = {"cn", "uid", "sn", "userpassword"};
//Identify service provider to use
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
env.put(Context.PROVIDER_URL, MY_HOST);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
env.put(Context.SECURITY_CREDENTIALS, MGR_PW);
try
{
// Create the initial directory context
InitialDirContext initialContext = new InitialDirContext(env);
DirContext ctx = (DirContext)initialContext;
System.out.println("Context Sucessfully Initialized");
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints);
while(results != null && results.hasMore())
{
SearchResult sr = (SearchResult) results.next();
String dn = sr.getName() + "," + MY_SEARCHBASE;
System.out.println("Distinguished Name is " + dn);
Attributes ar = ctx.getAttributes(dn, MY_ATTRS);
if(ar == null)
{
System.out.println("Entry " + dn);
System.out.println(" has none of the specified attributes\n");
}
else
{
for(int i=0; i<MY_ATTRS.length; i++)
{
Attribute attr = ar.get(MY_ATTRS[i]);
System.out.println(MY_ATTRS[i] + ":");
for(Enumeration vals=attr.getAll(); vals.hasMoreElements();)
{
System.out.println("\t" + vals.nextElement());
}
}
}
}
}
catch(Exception e)
{
System.err.println(e);
}
}
Below is the result:
Distinguished Name is uid=yiwei,ou=Administrator,o=SID,dc=QuizPortal
cn:
yiwei huang
uid:
yiwei
sn:
huang
userpassword:
[B@1cd8669
任何建議? 提前謝謝了
凱文
你所看到的([B @ 1cd8669]是Java的說法“這是一個字節數組”。
存儲的“密碼”很可能是真實密碼的散列或加密版本。 根據定義,加密哈希值是不可逆的,因此如果LDAP存儲哈希值,您將無法看到用戶的密碼是什么。
如果它是加密的,那么如果您知道算法和密鑰,則解密起來相當簡單。 BouncyCastle是一個很棒的Java加密庫,可用於解密密碼。
基本上,您需要確切地知道您正在查看的內容,這取決於LDAP設置。
使用ldap,我們將獲得字節數組中的數據。如果需要獲取原始密碼文本,請使用
以下代碼:
Attribute userPassword = attributes.get("userPassword");
String pwd = new String((byte[]) userPassword.get());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.