Wednesday, July 7, 2010

Create a key in the registry using C#

Introduction

Microsoft.win32 Namespace is containing register class; Register class is used to manipulate the system registry. It represents a Key level node in the windows registry.

The registry acts as information for the operating system and application on a computer. Register keys are the base unit of registry. Each key have multiple values alone with it.

C# Code for creates a key in the register.

CreateSubKey method to creates a new subkey or opens an existing subkey for write access


Example:
Microsoft.Win32.RegistryKey mykey;
mykey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(“Muhil Software”);

To set a value for the particular key is setValue method

Example:

mykey.SetValue(“Muhil”, “12345789”);


Some time exceptions will occur due to the name of the key is null or the user does not have permissions to create registry key or they key name exceeds the limit or if the key is closed or the register key is read only.


More secure to write data into register use Microsoft.Win32.Registry.CurrentUser instead of Microsoft.Win32.Registry.LocalMachine.
Complete listing for create a key.

Microsoft.Win32.RegistryKey mykey;
mykey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(“Muhil Software”);
mykey.SetValue(“Muhil”, “12345789”);
mykey.close();


Retrieve Registry key information.

RegistryKey.GetValue Method retrieves the value in a specified name. If returns null the name does not exist in the registry and it will return as a object.
Example:

key.GetValue(“Muhil”);

Complete listing to retrieve the registry key

Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Muhil Software");
String s = (String)key.GetValue("Muhil");

OpenSubKey method is used to open the register under {HKEY_CURRENT_USER\Muhil Software}. GetValue method return the object associated with name.

Hash Algorithm

Introduction

Hash Algorithm is one of cryptographic hash functions introduced by the national institute of standards and technology. There are few of secure hash algorithm are SAH1, MD5, SHA256, SHA384 and SHA512.

The hash is used, as a unique value of fixed size representing a large amount of data and the hashes of two sets of data should match if the corresponding data also matches. Small changes to the data result in large.

.Net
In .Net System.Security.Cryptography name-space provides the cryptographic services for secure encoding and decoding of data. It will provide the hashing, random number generation and message authentication.

.Net Framework supports the following hash algorithms.

· SHA1 is the secure hash algorithm (SHA). SHA! Uses 160 bits to encrypt the data
· MD5 is an algorithm gets an input of length and output digested. MD5 uses 128 bits to encrypt the data
· SHA256 is the SHA that uses 256 bits to encrypt the data
· SHA384 is the SHA that uses 384 bits to encrypt the data.
· SHA512 is the SHA that uses 512 bits to encrypt the data

Sha1 is the most widely used for the existing SHA hash functions and widely used for security applications and protocols
SHA1 Hashing Algorithm

System.Security.Cryptography namespace to write in top of using block and SHA1CryptoServiceProvider class is to assign with SHA1 Algorithm to be used

Example
Using System.Secuity.Cryptography;
SHA1 sha;

sha=new SHA1CryptoServiceProvider

ComputeHash method compute the hash value for the specified bytes
ComputeHash(System.Text.Encoding.UTF8.GetBytes("GivenString")

Complete Listing of SHA1 Algorithm
byte[] result = new byte[100];
SHA1 sha;

string FinalStr = "180066666password",sSignature;
sha = new SHA1CryptoServiceProvider();

result = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(FinalStr));

sSignature = Convert.ToBase64String(result);

Create instance of the SHA1CryptoServiceProvider and use Compute Hash to computes the hash value for the specified byte array. Again result converts into base64string method.

Output: K6DLPjiaIvN5JLQFQHaYLcUBAug=

Sunday, July 4, 2010

Bill Gates’ 10 advices to young people

Before retiring in July 2008, Bill Gates gave the following advices to high school students. Who knows you will become a second Bill Gates after learning from these valuable advices?


1. “Life is not fair - get used to it.”
-> The world is never fair. You know this? You can never change the whole world. Injustice still exists in the current society, so you should try to adapt.

2. “The world won't care about your self-esteem. The world will expect you to accomplish something BEFORE you feel good about yourself.”
-> Your extreme self-esteem may make your job inconvenient. Don't attach too much significance on your self-esteem as people care about your achievements rather than it.

3. “You will NOT make 40 thousand dollars a year right out of high school. You won't be a vice president with car phone, until you earn both.”
-> Normally you can't be rich if you have just finished high school. However to become an executive, you need to obtain both: a high school certificate and the money.

4. “If you think your teacher is tough, wait till you get a boss. He doesn't have tenure.”
-> Don’t complain that your boss is tough. When you are at school, your teachers always stand by you whenever you meet problems. However if you have thought that all your problems really come from the hard requirements of your teachers, you shouldn't look for a job. Simply if there aren't strict demands from your company, you will do nothing and quickly become unemployed. Then nobody will give you a hand.

5. “If you mess up, it's not your parents' fault, so don't whine about your mistakes, learn from them.”
-> Don't blame your failure on your destiny. All you need now is to stay calm and start from scratch.

6. “Before you were born, your parents weren't as boring as they are now. They got that way from paying your bills, cleaning your clothes and listening to you talk about how cool you are. So before you save the rain forest from the parasites of your parent's generation, try delousing the closet in your own room.”
-> You should show your gratefulness to your parents for spending most of their lives for your living and growth. All the “outdated” of your parents today is the price they have to pay for your growth.

7. “Your school may have done away with winners and losers, but life may not. In some schools they have abolished failing grades and they'll give you as many times as you want to get the right answer. This doesn't bear the slightest resemblance to ANYTHING in real life.”
-> Keep in mind that you can always become a leader, so that you will have more motivation to strive for your career.

8. “Life is not divided into semesters. You don't get summers off and very few employers are interested in helping you find yourself. Do that on your own time.”
-> Don’t always wait for holidays or you will be left behind your colleagues. That backwardness means elimination and unemployment.

9. “Television is NOT real life. In real life people actually have to leave the coffee shop and go to jobs.”
-> Everybody likes watching TV, but you shouldn't watch it too much. As that actually isn't your life and your thinking will be influenced. You yourself have to determine your own life.

10. “Be nice to nerds. Chances are you'll end up working for one.”
-> You should be nice to everyone. Life has happenings you could never expect. Be open to your boss, don’t say bad things behind his back as it will bring you nowhere.