开发者

How to insert and retrieve key from registry editor

开发者 https://www.devze.com 2023-02-01 05:36 出处:网络
I am new to cryptography. I have to develop project based on cryptography..In part of my project I have to insert a key to the registry and afterwards I have to retrieve the same key for decryption..

I am new to cryptography. I have to develop project based on cryptography..In part of my project I have to insert a key to the registry and afterwards I have to retrieve the same key for decryption.. I done until getting the path of the registry ..

Here I am showing my code:

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 Successful开发者_开发问答ly");
        }

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

But i dont know how to insert a key in a registry and read the particular key which i inserted..Please help me.. Thanks in advance..


It would be a lot easier to use the JRegistry library to edit the registry, rather than execute commands.

0

精彩评论

暂无评论...
验证码 换一张
取 消