Home > other >  Converting Registry data to usable String in C#
Converting Registry data to usable String in C#

Time:09-12

So I'm currently creating a CLI program for my encryption library. This is just a test program, as I will later build a GUI-based program for it. I am storing the encryption key in the registry, for now, might change this later. So far, I can write the key to the registry, but I can't convert the data from the registry back to a string to use as the actual key in the encryption.

Here's my current code:

internal String Encrypt()
        {
            Console.Clear();
            Console.Write("Encryption\nPlease enter the path to the file you want to encrypt: ");

            String FileInPath = Console.ReadLine();
            String FileOutPath = "";
            String FileData = "";

            while (FileInPath == "")
            {
                FileInPath = RetryPathIn();
            }

            String RetryPathIn()
            {
                Console.WriteLine(".>>");
                String PathInStr = Console.ReadLine();
                return PathInStr;
            }
            
            Console.Write("\nPlease enter the path of the file you want to decrypt: ");
            FileOutPath = Console.ReadLine();

            while (FileOutPath == "")
            {
                FileOutPath = RetryEncrypt();
            }

            String RetryEncrypt()
            {
                Console.WriteLine(".>>");
                String FileOutPathStr = Console.ReadLine();
                return FileOutPathStr;
            }
            object Key = Registry.CurrentUser.OpenSubKey(@"Software\NikkieDev Software").GetValue("Key");
            String KeyStr = Key.ToString();
            String FileOutData = "";

            if (Key!=null)
            {
               FileOutData = Crypt.Encrypt(FileData, KeyStr);
            }

            var file = File.Create(FileOutPath);
            file.Close();
            File.WriteAllText(FileOutPath, FileOutData);

            return $"Data has been encrypted and saved in {FileOutPath}";
        }

I am fairly new to the registry, and I've only recently found out how to use it as data storage in programming. I am using .NET 6 for this, and I hope that someone can help me.

[EDIT] I've shown below how I've created the register data. Due to people asking about it

internal void Initialize()
        {
            String SettingsFile = File.ReadAllText(CoreObject.DataFile);
            dynamic _JsonObj = JsonConvert.DeserializeObject(SettingsFile);

            if (_JsonObj["KeyGenerated"] == 0)
            {
                RegistryKey RegData = Registry.CurrentUser.CreateSubKey(@"Software\NikkieDev Software");
                String NewKey = Key.CreateKey();
                RegData.SetValue("Key", NewKey);
                
                _JsonObj["KeyGenerated"] = 1;

                dynamic NewData = JsonConvert.SerializeObject(_JsonObj, Formatting.Indented);
                File.WriteAllText(CoreObject.DataFile, NewData);
                RegData.Close();
            }
        }

Due to recent questions about missing code, I here provide my encryption library that I'm building this program around: https://github.com/NikkieDev/bungocrypt_cs

The CreateKey() returns a string (the key). Furthermore Crypt.Encrypt() takes the key and the data and scrambles them together and exchanges all the characters with one another.

CodePudding user response:

The answers have been provided by the community in the comments. Thanks to the people listed below

Topaco Raymond Chen

  • Related