Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过JNDI检索LDAP密码_Java_Passwords_Ldap_Jndi - Fatal编程技术网

Java 如何通过JNDI检索LDAP密码

Java 如何通过JNDI检索LDAP密码,java,passwords,ldap,jndi,Java,Passwords,Ldap,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

我能够通过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
publicstaticvoidmain(字符串[]args)
{
字符串INITCTX=“com.sun.jndi.ldap.LdapCtxFactory”;
字符串MY_HOST=”ldap://KhooGP-Comp1:1389";
String MGR\u DN=“cn=目录管理器”;
字符串管理器_PW=“密码”;
字符串MY_SEARCHBASE=“dc=QuizPortal”;
字符串MY_FILTER=“uid=yiwei”;
字符串MY_ATTRS[]={“cn”、“uid”、“sn”、“userpassword”};
//确定要使用的服务提供商
Hashtable env=新的Hashtable();
环境放置(Context.INITIAL\u Context\u工厂,INITCTX);
env.put(Context.PROVIDER\u URL,我的主机);
环境put(Context.SECURITY_认证,“simple”);
环境保护专员(环境安全负责人、经理);
环境保护专员(环境安全专员、经理);
尝试
{
//创建初始目录上下文
InitialDirContext initialContext=新的InitialDirContext(env);
DirContext ctx=(DirContext)initialContext;
System.out.println(“上下文成功初始化”);
SearchControls约束=新的SearchControls();
约束.setSearchScope(SearchControls.SUBTREE_范围);
NamingEnumeration results=ctx.search(我的搜索库、我的过滤器、约束);
while(results!=null&&results.hasMore()
{
SearchResult sr=(SearchResult)results.next();
字符串dn=sr.getName()+,“+MY_SEARCHBASE;
System.out.println(“可分辨名称为”+dn);
Attributes ar=ctx.getAttributes(dn,MY_ATTRS);
if(ar==null)
{
系统输出打印项次(“条目”+dn);
System.out.println(“没有指定的属性\n”);
}
其他的
{
对于(inti=0;i你所看到的([B@1cd8669)是Java表示“这是一个字节数组”的方式

存储的“密码”很可能是真实密码的散列或加密版本。根据定义,加密散列是不可逆的,因此如果LDAP存储散列,您将无法看到用户的密码

如果它是加密的,那么如果你知道算法和密钥,解密就相当简单。是一个很好的Java加密库,你可以用它来解密密码


基本上,您需要确切地了解您正在查看的内容,这取决于LDAP设置。

使用LDAP,我们将以字节数组形式获取数据。如果您需要获取原始密码文本,请使用
以下代码:

Attribute userPassword = attributes.get("userPassword");
String pwd = new String((byte[]) userPassword.get());

检索密码的目的到底是什么?这违反了基本的安全惯例。