We help IT Professionals succeed at work.

Generate a byte-array value for registry

Camillia
Camillia asked
on
1,109 Views
Last Modified: 2008-07-10
I'm working with a new project. There's a line of code that reads a value from the registry.
I created the entry but the value in that key-registry should be byte. I cant leave it blank, i tried some random value like 570, then  tried something like 0A00XD (something like that) but this line of code gives me an error that cant convert string to byte.

 How can I just generate a byte-array value and copy it in this registry entry?
*** Key is the value in registry that i need to read back...
using (RegistryKey reg = Registry.LocalMachine.OpenSubKey(_keyRegistryPath, false))
            {
                if (reg != null)
                {
                    key = (byte[])reg.GetValue(_keyRegistryName, key);
                }
            }

Open in new window

Comment
Watch Question

Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
Which type is the registry key?
Which type is the 'key' variable (bad name, BTW)

if first is a string, and second is int, you can do something like:
key = int.Parse(reg.GetValue(_keyRegistryName, key))
CERTIFIED EXPERT

Author

Commented:
this is the routine below. I'm new to this code .Group of ex-developers developed this.
"key" is byte.  But the issue is I need to generate a byte value AND PUT it in the registry key value. In my registry...i have "software/IS/AnotherFolder". Then , if i right click on "Another Folder",  i see "string value"..."binary value", "DWORD value"...etc.

 I created a "string value". I named it "Payment". On  the right pane, i double click "Payment" and I should *** create and enter a byte" value in it. Then that line of code above needs to read this registry value...
I think creating a "string value" is wrong.I think i should create "binary value" and still somehow I need to create a byte value (any byte value) and put it in there...

So this wont help me without having a value in registry: key = int.Parse(reg.GetValue(_keyRegistryName, key))


CERTIFIED EXPERT

Author

Commented:
forgot the routine:
private static byte[] GetKeyFromRegistry()
{
byte[] key = new byte[0];
using (RegistryKey reg = Registry.LocalMachine.OpenSubKey(_keyRegistryPath, false))
{
if (reg != null)
{
//key = (byte[])reg.GetValue(_keyRegistryName, key);
}
}
return key;
}
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
I suggest to use a DWORD value for this.
My example should work with it.
Also, your use of byte[0] won't work anyway.
CERTIFIED EXPERT

Author

Commented:
I dont think this section was ever tested...
use DWORD but do I still need to put a value in there? what should I change byte[0] to? this is the orig code developed  by whomever?
CERTIFIED EXPERT

Author

Commented:
trying your suggestion now.
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
to retrieve the DWORD value, use:

key = int.Parse(reg.GetValue(_keyRegistryName, key))

key should of type int or uint
CERTIFIED EXPERT

Author

Commented:
i did this and it's not working:

int key;
key = int.Parse(reg.GetValue(_keyRegistryName, key));

get error:
The best overloaded method match for 'int.Parse(string)' has some invalid arguments
Argument '1': cannot convert from 'object' to 'string'

*** no way to use Byte? They have bytes in other places in that section of code..if not, how can i get your method working?
CERTIFIED EXPERT

Author

Commented:
this worked:
key = int.Parse(reg.GetValue(_keyRegistryName, key).ToString());

but still no way to use Byte?
CERTIFIED EXPERT

Author

Commented:
yeah,  i have to change the rest of the code if I use "int".
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
why a byte?
a value like 570 cannot be stored in a byte.
CERTIFIED EXPERT

Author

Commented:
because there's another routine (Rajindal's method something)  (below) that uses that other routine. That uses Byte and I cant change that( i tried but got an error):

private static Rijndael CreateRijndael(byte[] iv)
{
Rijndael aes = RijndaelManaged.Create();
aes.Key = GetKeyFromRegistry();
aes.IV = iv;
return aes;
}
CERTIFIED EXPERT

Author

Commented:
tried this as well:
int keytest;
keytest = (int)reg.GetValue(_keyRegistryName, key);
key = (byte[])keytest;
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
>>Rijndael aes = RijndaelManaged.Create();
>>aes.Key = GetKeyFromRegistry();

ah, you missed a little detail.
You have to store the data in registry as binary, so you can read the way you want.

byte[] key;
key = (byte[]) reg.GetValue(_keyRegistryName, key);
key = (byte[])keytest;
Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
sorry, forget the last line in the code snippet
CERTIFIED EXPERT

Author

Commented:
ok, i created the registry as "binary value". when i double click on it, the value inside it is "0000". I will use the code above but still i'm back to my orig question....how do I replace that "0000" with a random binary value?

Code has a check : if (key == null || key.Length == 0)

So, I dont think i can leave "0000" in the key-value... I found a link an hour ago ( i cant find it now) that did something  like byte[] myvalue = new byte[] {&H3,&H4};
or something like that...
 
CERTIFIED EXPERT

Author

Commented:
yeah, i need to replace that 0000 with some value. Ex-developer  said for their classic ASP code , they use a code to generate the key.
Software Architect
CERTIFIED EXPERT
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
brain is fried, will try it tomorrow morning and post back.Thanks again for your help and sticking with this. Kamila.
CERTIFIED EXPERT

Author

Commented:
you're the best, that fixed it. Kamila.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.