Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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(mac)从密钥链查看internet密码_Java_Terminal_Passwords_Keychain_Inputstreamreader - Fatal编程技术网

如何使用Java(mac)从密钥链查看internet密码

如何使用Java(mac)从密钥链查看internet密码,java,terminal,passwords,keychain,inputstreamreader,Java,Terminal,Passwords,Keychain,Inputstreamreader,我想创建一个简单的java应用程序(用于mac),它接收一个网站,并在Keychain中输出与该网站相关联的密码 我的问题是我的应用程序无法从输出中读取密码。如果我手动写入: security find-internet-password -gs www.google.com 进入终端,我得到几行信息,然后->密码:“我的密码”。但在我的应用程序中,我只看到了信息行,但最后一行应该是密码,它不存在或为空 我的代码: public static void command(){ try{

我想创建一个简单的java应用程序(用于mac),它接收一个网站,并在Keychain中输出与该网站相关联的密码

我的问题是我的应用程序无法从输出中读取密码。如果我手动写入:

security find-internet-password -gs www.google.com
进入终端,我得到几行信息,然后->密码:“我的密码”。但在我的应用程序中,我只看到了信息行,但最后一行应该是密码,它不存在或为空

我的代码:

public static void command(){
    try{

        String command = "security find-internet-password -gs www.google.com";
        Process child = Runtime.getRuntime().exec(command);

        BufferedReader r = new BufferedReader(new InputStreamReader(child.getInputStream()));
        String s;

        while ((s = r.readLine()) != null) {
            System.out.println(s);
        }
        System.out.println(r.readLine());
        r.close();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}
我没有收到任何错误,只是上面的代码没有显示密码。如果有必要的话,我正在使用Eclipse。谢谢

应用程序打印:

keychain: "/Users/*username*/Library/Keychains/login.keychain"
class: "inet"
attributes:
    0x00000007 <blob>="www.google.com"
    0x00000008 <blob>=<NULL>
    "acct"<blob>="*username*"
    "atyp"<blob>="http"
    "cdat"<timedate>=0x323031*numbers*3333325A00  "2011*numbers*132Z\000"
    "crtr"<uint32>="rimZ"
    "cusi"<sint32>=<NULL>
    "desc"<blob>=<NULL>
    "icmt"<blob>=<NULL>
    "invi"<sint32>=<NULL>
    "mdat"<timedate>=0x3230331*numbers*33834375A00  "20115*numbers*3847Z\000"
    "nega"<sint32>=<NULL>
    "path"<blob>="/"
    "port"<uint32>=0x00000000 
    "prot"<blob>=<NULL>
    "ptcl"<uint32>="htps"
    "scrp"<sint32>=<NULL>
    "sdmn"<blob>="www.google.com"
    "srvr"<blob>="www.google.com"
    "type"<uint32>=<NULL>
已编辑


是InputStreamReader没有读取密码吗?是否有其他方法获取密码?

在输出中没有获取密码的原因是它不是打印到标准输出,而是打印到标准输出

如果将代码中的
child.getInputStream()
替换为
child.getErrorStream()
,您将获得正确的
密码:secret

例如,您可以使用以下代码的修改版本:

public static boolean command(String host) {
    try {

        String command = "security find-internet-password -gs " + host;
        Process child = Runtime.getRuntime().exec(command);

        try (BufferedReader out = new BufferedReader(new InputStreamReader(
                child.getInputStream()));
                BufferedReader err = new BufferedReader(
                        new InputStreamReader(child.getErrorStream()))) {
            String user = null;
            String password = null;
            String s;

            while ((s = out.readLine()) != null) {
                if (s.matches(" *\"acct\".*")) {
                    user = s.replaceAll("^.*=\"", "").replace("\"", "");
                }
            }
            s = err.readLine();
            password = s.replaceAll("^.*: *\"", "").replace("\"", "");
            System.out.println("user: " + user);
            System.out.println("pwd: " + password);
            return true;
        }
    } catch (IOException e) {
        return false;
    }
}

你能告诉我们一般印刷的是什么吗?以及应该打印的内容。程序完成了吗?@LoganMurphy我添加了它打印的内容,我试着从命令行运行java?可能得像你一样跑admin@LoganMurphy我不知道你的意思,但我是管理员,当我运行应用程序(或在终端中键入代码)时,我会收到一条消息,询问是否允许安全部门访问您钥匙链中“www.google.com”中存储的机密信息,我回答允许。
public static boolean command(String host) {
    try {

        String command = "security find-internet-password -gs " + host;
        Process child = Runtime.getRuntime().exec(command);

        try (BufferedReader out = new BufferedReader(new InputStreamReader(
                child.getInputStream()));
                BufferedReader err = new BufferedReader(
                        new InputStreamReader(child.getErrorStream()))) {
            String user = null;
            String password = null;
            String s;

            while ((s = out.readLine()) != null) {
                if (s.matches(" *\"acct\".*")) {
                    user = s.replaceAll("^.*=\"", "").replace("\"", "");
                }
            }
            s = err.readLine();
            password = s.replaceAll("^.*: *\"", "").replace("\"", "");
            System.out.println("user: " + user);
            System.out.println("pwd: " + password);
            return true;
        }
    } catch (IOException e) {
        return false;
    }
}