有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从注册表编辑器中插入和检索密钥

我对密码学一无所知。我必须开发基于密码学的项目。。在我的项目中,我必须向注册表中插入一个密钥,然后我必须检索相同的密钥进行解密。。在找到注册表路径之前我一直在做

我在这里展示我的代码:

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

public final class Project {

    public static final String readRegistry(String location, String key) {
        try {
            // Run reg query, then read output with StreamReader (internal class)
            Process process = Runtime.getRuntime().exec("reg query " +
                    '"' + location + "\" /v " + key);

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();
            String output = reader.getResult();

            // Output has the following format:
            // \n<Version information>\n\n<key>\t<registry type>\t<value>
            if (!output.contains("\t")) {
                return null;
            }

            // Parse out the value
            String[] parsed = output.split("\t");
            return parsed[parsed.length - 1];
        } catch (Exception e) {
            return null;
        }

    }

    static class StreamReader extends Thread {

        private InputStream is;
        private StringWriter sw = new StringWriter();

        public StreamReader(InputStream is) {
            this.is = is;
        }

        public void run() {
            try {
                int c;
                while ((c = is.read()) != -1) {
                    System.out.println("Reading" + c);
                    sw.write(c);
                }
            } catch (IOException e) {
                System.out.println("Exception in run() " + e);
            }
        }

        public String getResult() {
            System.out.println("Content " + sw.toString());
            return sw.toString();
        }
    }

    public static boolean addValue(String key, String valName, String val) {
        try {
            // Run reg query, then read output with StreamReader (internal class)



            Process process = Runtime.getRuntime().exec("reg add \"" + key + "\" /v \"" + valName + "\" /d \"\\\"" + val + "\\\"\" /f");

            StreamReader reader = new StreamReader(process.getInputStream());
            reader.start();
            process.waitFor();
            reader.join();
            String output = reader.getResult();
            System.out.println("Processing........ggggggggggggggggggggg." + output);
            // Output has the following format:
            // \n&lt;Version information&gt;\n\n&lt;key&gt;\t&lt;registry type&gt;\t&lt;value&gt;
            return output.contains("The operation completed successfully");
        } catch (Exception e) {
            System.out.println("Exception in addValue() " + e);
        }
        return false;
    }

    public static void main(String[] args) {

        // Sample usage
        JAXRDeleteConcept hc = new JAXRDeleteConcept();
        System.out.println("Before Insertion");
        if (JAXRDeleteConcept.addValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU", "REG_SZ", "Muthus")) {
            System.out.println("Inserted Successfully");
        }

        String value = JAXRDeleteConcept.readRegistry("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU" , "Project_Key");
        System.out.println(value);
    }
}

但是我不知道如何在注册表中插入一个键并读取我插入的特定键。。请帮帮我。。 提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    使用JRegistry库编辑注册表比执行命令容易得多