Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone,

Please do the needful on this:

Write a string in encrypted way in Windows registry using java. and have options to store multiple values in windows registry


Regards,

Arjun

What I have tried:

I have tried with java.util.prefs.Preferences, but it did not work.
Posted
Updated 3-Sep-16 19:10pm
Comments
Richard MacCutchan 2-Sep-16 3:22am    
What does "it did not work." mean?

Bad question: Your question is multiple questions

- How to encrypt a string ?
- implying: How to decrypt a string ?
- implying: Which encrypting/decrypting method ?
- How to save a string in registry ?
- implying: How to retrieve a string in registry ?
- How to save multiple values in registry ?

Tell what is your problem, show your code.
 
Share this answer
 
Comments
Arjun2016 2-Sep-16 1:59am    
My questions are : How to save a string in registry and i need to save multiple values in registry.
Encrypt/Decrypt a string is working fine with my sample code.

I have tried with this code:
http://www.javacoderanch.com/how-to-read---write-data-in-windows-registry.html
 
Share this answer
 
Comments
Mehdi Gholam 2-Sep-16 1:50am    
5'ed
Maciej Los 2-Sep-16 1:51am    
Thank you, Mehdi.
Hello Guys,


i have worked on this sample application using JNA jar, it's working. But i need to achieve the same without using external jar "Create a registry key and write the entries to registry". Any help on this!

here is my code:
C#
import com.sun.jna.platform.win32.Advapi32Util;
import static com.sun.jna.platform.win32.WinReg.HKEY_CURRENT_USER;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

public class Test {
	private static String encryptionKey;
	
	public Test(String encryptionKey)
    {
        Test.encryptionKey = encryptionKey;
    }	
	private static void SetEncryptiontKey(String string) {
		// TODO Auto-generated method stub
		Test.encryptionKey = string;
	}
	
    public String encrypt(String plainText) throws Exception
    {
        Cipher cipher = getCipher(Cipher.ENCRYPT_MODE);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());

        return Base64.encode(encryptedBytes);
    } 

    private Cipher getCipher(int cipherMode)
            throws Exception
    {
        String encryptionAlgorithm = "AES";
        SecretKeySpec keySpecification = new SecretKeySpec(
                encryptionKey.getBytes("UTF-8"), encryptionAlgorithm);
        Cipher cipher = Cipher.getInstance(encryptionAlgorithm);
        cipher.init(cipherMode, keySpecification);

        return cipher;
    }
    
    public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
    	SetEncryptiontKey("MZygpewJsCpRrfOr");
    	
    	System.out.println("Enter User Name\n");
    	String Username = in.nextLine();
    	
    	System.out.println("Enter Password\n");
    	String Password = in.nextLine();
    	    	
    	Test advancedEncryptionStandard = new Test(
            encryptionKey);
    	String cipherText = null;
		try {
			cipherText = advancedEncryptionStandard.encrypt(Password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	//System.out.println(Password);
    	//System.out.println(cipherText);
    	
    	Advapi32Util.registryCreateKey(HKEY_CURRENT_USER, "Software\\Dell Configuration");
       // System.out.println(Advapi32Util.registryKeyExists(HKEY_CURRENT_USER, "Software"));
        Advapi32Util.registrySetStringValue
            (HKEY_CURRENT_USER, "Software\\Dell Configuration", "Username", Username);
        Advapi32Util.registrySetStringValue
        (HKEY_CURRENT_USER, "Software\\Dell Configuration", "Password", cipherText);
    }	

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900