Read registry key in C# - c#

I'm reading the value of a registry key:
Microsoft.Win32.RegistryKey key;
key = *someLongPathHere*;
and displaying the value to a label:
string a = (string)key.GetValue("");//a default value
label1.Text = a;
It displays:
{F241C880-6982-4CE5-8CF7-7085BA96DA5A}
which is mostly correct, except for the first underscore, which exists in the original value:
{_F241C880-6982-4CE5-8CF7-7085BA96DA5A}
Why does it happen? i.e the missing underscore?
Also, after reading the key, do I have to close the key or anything? How can I do it?

It's easy enough to test whether or not there is a systematic problem with string values containing underscores. You can simply create such a value in the registry editor and then read it into your C# program with GetValue(). When you do so you'll discover that the C# registry code doesn't lose underscores. So, there must be some other explanation for your problem.
My best guess is that your label component does not display the underscore. I'm not very familiar with the C# UI frameworks but that seems plausible. Try looking at the value of a under the debugger rather than in a label caption on your UI.
The other thing that comes to mind is that you have registry redirection because you have an x86 process running on x64, and your key is under a redirected key, HKLM\Software, for example. Perhaps if you look under the Wow6432Node you will see the underscore discrepancy.
As for managing the life of the key, the key is backed by an unmanged resource. Namely a Windows HKEY. The RegistryKey class implements IDisposable and so you should wrap your keys in a using statement.

Related

Write the string name of the Input key

In my game, the user can set a key to open the game-console.
I want to show an info during the game to the user, that they can open the console by pressing the key, they've set before.
For example:
The default key for the console is f1. Then it should show:
Press F1 to open Console
If the user sets the C key, it should write:
Press C to open Console
But i don't find any way to write down the key, the user set before, by code.
Edit:
I'm sorry that it wasn't that clear what i mean.
I added a screenshot of the Input-Configuration (which is the default Unity Input-Configuration).
In this Configuration the user can set a key for OpenConsole by double clicking on f1 (in the Primary row).
In Unity i can check if a specific Button is pressed like this:
if (Input.GetButtonDown("OpenConsole"))
{
...
}
But what i want is, that i can show the user which key they have chosen for OpenConsole. Something like this:
text.text = "Press the " + WhateverTheUserSet + " Key to open the Console!";
Okay, your question isn't clear as to how the default / redefined key is set / changed by the user but I'll give two solutions based on two scenarios:
Scenario 1: Assumes you are asking the user what key to use via an onscreen "Redefine Key" page.
In this situation, you will probably use something along the lines of Console.ReadKey() where you are capturing the user's keyboard press. You end up with a key code which could be one of around 100 keys on a keyboard. You will need to store internally a mapping between keycode and "text". If they user had pressed F1 and this is keycode (say 232) then you will use the mapping to give you the string "F1" which is what you display on screen. You would store the keycode in config for use in the game and for peristance between launches of the game.
Scenario 2: Assumes the user edits a config file and sets the value in there.
In this situation, you need the mapping going the other way. If the config file includes (by default):
ConsoleKey=F1
Then you need a similar internally stored mapping but in the other direction. If the user changes the value to "A" then this would need to map to a keycode (e.g. 65). The user can change the config file and the mapping will tell the user what key to detect in game. You can display the same text as in the config file for your on-screen "info".
If you can be clearer in how you are intending to implement this then we can provide a clearer answer.
Unfortunately, Unity doesn't have a way to check which keys are assigned from the input manager. If you need more control, you'd need to implement your own key mapping solution.

Wow6432Node subkeys inaccessible from 64 bit registry view

I am writing a program that traverses the registry. When encountering the HKEY_CLASSES_ROOT\VirtualStore\MACHINE\SOFTWARE\Wow6432Node-Key, the program stops because
var openedKey = key.OpenSubKey(/* any name of the present (!) subkeys*/);
// where "key" is "HKEY_CLASSES_ROOT\VirtualStore\MACHINE\SOFTWARE\Wow6432Node"
yields null.
Therefore, I tried to find this key using regedit.exe and reg.exe and the result was that there are indeed subkeys that can be seen using this methods. Furthermore,
key.GetSubKeyNames()
returns the exact same values for the names.
I tried compiling for 64- and 32-bit CPUs and passed both RegistryView.Registry32 and RegistryView.Registry64 to the first
RegistryKey.OpenBaseKey(hive, view)
call.
This did not solve the problem.
Why are the returned keys null?

ConsoleKeyInfo, the Question Mark and Portability

I have a little C# console application that reads a key and checks to see if the key was a question mark:
ConsoleKeyInfo ki = System.Console.ReadKey();
if (ki.ConsoleKey.Oem2) // Do something
I arrived at Oem2 by seeing what value is actually assigned in the debugger, because there is no ConsoleKey code for question mark.
Now I could certainly use ki.KeyChar instead, but the application also needs to respond to certain keys (e.g. media keys) that do not map to characters. It feels inelegant to check both ConsoleKey and KeyChar to determine which key has in fact been pressed. On the other hand, it does not feel safe to rely on Oem2 to always map to ? in all circumstances and regions.
Is it best practice to check both properties to determine which key was in fact pressed?
Any insight into why ConsoleKeyInfo was designed this way is appreciated.
In this case, you will have to check KeyChar == '?'. From MSDN:
Oem2: The OEM 2 key (OEM specific).
So you're just getting lucky in that it happens to be a ? on your equipment.
The ConsoleKeyInfo structure provides KeyChar (a Char value) as well as Modifiers (an enumeration) to help you decide what keys the user had pressed.
I think you should consider what happens when someone has different keyboard layout.
If you want to check for “the key with question mark on my computer”, then use ConsoleKey. But that's probably not a good idea and you should probably adhere to the user's settings and use KeyChar.
But for keys that don't map to to characters (and the user can't remap them by using different keyboard layout), you have to use ConsoleKey.
So, yes, I think you should check both properties in this case.
I guess the reason for this design is that Console.ReadKey() relies on a native function (ReadConsoleInput) that returns an array of KEY_EVENT_RECORD structures in case of a keypress, where each key event has an ASCII/Unicode character representation and a virtual key code. Notice the VK_OEM_2 in my previous link - this is where the ConsoleKey.Oem2 value comes from.

decrypt an encrypted value?

I have an old Paradox database (I can convert it to Access 2007) which contains more then 200,000 records. This database has two columns: the first one is named "Word" and the second one is named "Mean". It is a dictionary database and my client wants to convert this old database to ASP.NET and SQL.
However, we don't know what key or method is used to encrypt or encode the "Mean" column which is in the Unicode format. The software itself has been written in Delphi 7 and we don't have the source code. My client only knows the credentials for logging in to database. The problem is decoding the Mean column.
What I do have is the compiled windows application and the Paradox database. This software can decode the "Mean" column for each "Word" so the method and/or key is in its own compiled code(.exe) or one of the files in its directory.
For example, we know that in the following row the "Zymurgy"
exactly means "مبحث عمل تخمیر در شیمی علمی, تخمیر شناسی" since the application translates it like that. Here is what the record looks like when I open the database in Access:
Word Mean
Zymurgy 5OBnGguKPdDAd7L2lnvd9Lnf1mdd2zDBQRxngsCuirK5h91sVmy0kpRcue/+ql9ORmP99Mn/QZ4=
Therefore we're trying to discover how the value in the Mean column is converted to "مبحث عمل تخمیر در شیمی علمی, تخمیر شناسی". I think the "Mean" column value in above row is encoded in Base64 string format, but decoding the Base64 string does not yet result in the expected text.
The extensions for files in the win app directory are dll, CCC, DAT, exe (other than the main app file), SYS, FAM, MB, PX, TV, VAL.
Any kind of help is appreciated.
here is two more example and remember double quotes at start and end are not part of the strings:
word: "abdominal"
coded value: "vwtj0bmj7jdF9SS8sbrIalBoKMDvTbpraFgG4gP/G9GLx5iU/E98rQ=="
translation in Farsi: "شکمی, بطنی, وریدهای شکمی, ماهیان بطنی"
word: "cart"
coded value: "KHoCkDsIndb6OKjxVxsh+Ti+iA/ZqP9sz28e4/cQzMyLI+ToPbiLOaECWQ8XKXTz"
translation in Farsi: "ارابه, گاری, دوچرخه, چرخ, با گاری بردن"
here is the result in different encodings:
1- in unicode the result is: "ᩧ訋퀽矀箖�柖�섰᱁艧껀늊螹泝汖銴岔꫾也捆￉鹁"
2- in utf32 the result is: "��������������"
3- in utf7 the result is: "äàg\v=ÐÀw²ö{Ýô¹ßÖg]Û0ÁAgÀ®²¹ÝlVl´\\¹ïþª_NFcýôÉÿA"
4- in utf8 the result is: "��g\v�=��w���{����g]�0�Ag��������lVl���\\����_NFc����A�"
5- in 1256 the result is: "نàg\vٹ=ذہw²ِ–{فô¹كضg]غ0ءAg‚ہ®ٹ²¹‡فlVl´’”\\¹ï‏ھ_NFc‎ôةےA"
yet i discovered that the paradox database system is very complex when it comes to key management and most of the time the keys are "compound keys" and that's why it's problematic and that's why it's abandoned!
UPDATE: i'm trying to do the automation by using AutoIt v3 because the decryption process as i understand can't be done in one or two days. now i have another problem which is related to text/font. when i copy the translated text to notepad it will change to some unrecognizable text unless i change the font of notepad to the font of the translation software. if i type something in the notepad in Farsi it will show it correctly regardless of what font i've been chosen. more interesting is when i copy the text to any other program like MS Office Word it'll be shown correctly no matter what font i choose.
so how can i get around this ?
In this situation, I would think about writing a script/program to simply pull all the data out through the existing program.
You could write an application to send keypresses to the app which would select and copy each value in turn.
It would take a while to run, but you could just leave it overnight (how big is your database?) and it only has to run once.
Not sure how easy this would be, since I haven't seen this app of course - might this work?
Take a debugger like ollydbg/softice. Find the place where the mean is decoded/encoded and then step through the instructions one by one, check all registers to find out what is done. I have done so numerous times. That should help you getting started, since you have the application which is able to decode this stuff. You also have a reference word. That's all you need.
Also take into consideration: Unicode can be Little or Big Endian. So you might try swapping the bytes. UTF-8 can be a pain, since some words are stored as one byte and some as two bytes.
You can also try to take words which are almost identical in Farsi and try to compare the outputs. That could lead to a reconstruction of a custom code page, if there is one.

serial key validation in c#

I completed my project in C#.Net.
I Added a serial key in it.
At the time of installation it asks me for the key.
The key format is also given by me, but I don't know how to validate that key.
Please give me some way how to validate that key.
Now my key is validated by visual studio or I don't know who. How is the key's validation done?
But I want to do the validation by my own. Is it possible? And if yes, how?
i added the customer information dialog.
and format is given by using the serial number template.
but for validation it create the problem that visual studio's logic is so simple & it is if the some of digits is / by 7 then key is valid.so i want to use the my logic like use sha1,MD5 so where i have to do coding.
The design of the customer info dialog is not show,if it is shown then i do the coding on validate button.
But now what i have to do?
to write this code you can make an md5 hash according to an algorithm of your own so every user must have a key related to lets say his user name and email address that will unlock if the key is typed correctly. lets say if you have a user name "muster" and an email address "mymail#mail.web" you can combine it in an algorith of your own "simplest one for example is concatination: "mustermymail#mail.web" now encoding md5 hash to it will give a 32 char "9192ec1632022ab6b3706dc053d7b20f" which forms a serial and this serial can't be decrypted unless you know the algorithm that it was encrypted first.
to make an md5 hash value try this:
string text="mustermymail#mail.web";
byte []bytes=System.Text.Encoding.Default.GetBytes(text);
byte []md5Bytes=System.Security.Cryptography.MD5.Create().ComputeHash(bytes);
string md5Text=System.Text.Encoding.Default.GetString(md5Bytes);
Check : http://blogs.msdn.com/b/danielfe/archive/2005/07/10/437293.aspx for starters...

Categories