How to insert and reterive key from registry editor

Posted by deepa on Stack Overflow See other posts from Stack Overflow or by deepa
Published on 2010-12-24T08:06:49Z Indexed on 2010/12/24 8:54 UTC
Read the original article Hit count: 251

Filed under:

Hi..

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 reterive the same key for decryption.. i done until getting the path of the registry ..

Here i given my code..

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

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);
}

}

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..

© Stack Overflow or respective owner

Related posts about java