Monday, March 14, 2011

Working with Encryption in Console Application:

Sending sensitive information from one party to another over distributed networks such as the Internet requires specialized techniques to secure the data. You use cryptography to secure communication over insecure channels so that data is not viewed or modified during transmission.

The Microsoft .NET Framework 2.0 provides a set of classes that you can use to perform symmetrical and asymmetrical data encryption and data hashing by using industry-standard cryptographic algorithms. These algorithms are easy to use, and you can implement them by using the safest possible default properties.

When your application communicates over public networks, you can use cryptography to achieve the following goals:
Confidentiality. To help protect users’ identities and ensure that unintended parties do not read the data that users send to one another.
Data integrity. To ensure that transmitted data is not altered in transit.
Authentication. To assure that sent data originates from a particular party.

The .NET Framework 2.0 class library contains a set of classes in the:
System.Security.Cryptography namespace that you can use to encrypt and hash data before you send it across the network and decrypt data after you have received it. Some of these classes are managed wrapper classes that call implementations of cryptographic algorithms in the Windows® operating system. Any class that has
CryptoServiceProvider appended to its name is a managed wrapper class. Wrapper class implementations and fully managed code cryptographic classes that are included in the .NET Framework. The .NET Framework also provides several classes that provide support functions for data encryption and hashing. These include random number generation and stream oriented design for .NET Framework cryptography. The RNGCryptoServiceProvider and CryptoStream classes implement these features.

Understanding Data Encryption:
• Transforms data to keep it from being read by third parties.
• Used when two parties communicate over a nonsecure channel.

Symmetric Encryption:
Symmetric encryption is also known as secret-key encryption. It is the oldest and bestknown
data encryption technique. A symmetric encryption algorithm applies a secret key, which can be a number, a word, or just a string of random letters, to the text of a message to change the content in a particular way. Depending on the algorithm, this may be as simple as shifting each letter by a number of places in the alphabet. As long as both sender and recipient know the secret key, they can encrypt and decrypt all messages that use this key. Symmetric encryption algorithms, which are also known as block ciphers, are extremely fast; this makes them a good choice for encrypting and decrypting large streams of data. You use these algorithms to encrypt one block of data at a time, and they transform an input block of a specified number of bytes into an output block of encrypted bytes.

Example of symmetric encryption :

using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

class RijndaelSample
{

static void Main()
{
try
{
// Create a new Rijndael object to generate a key
// and initialization vector (IV).
Rijndael RijndaelAlg = Rijndael.Create();

// Create a string to encrypt.
Console.WriteLine("entern data to encrypt");
string sData = Console.ReadLine();


string FileName = "CText.txt";
byte []b= RijndaelAlg.Key;
// // Encrypt text to a file using the file name, key, and IV.
EncryptTextToFile(sData, FileName, RijndaelAlg.Key, RijndaelAlg.IV);
Console.WriteLine("press enter to decrycpt the text");
Console.ReadLine();
// Decrypt the text from a file using the file name, key, and IV.
string Final = DecryptTextFromFile(FileName,RijndaelAlg.Key, RijndaelAlg.IV);

//// Display the decrypted string to the console.
Console.WriteLine("Decrypted Text "+Final);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Console.ReadLine();
}

public static void EncryptTextToFile(String Data, String FileName, byte[] Key, byte[] IV)
{
//try
//{
// Create or open the specified file.
FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);

// Create a new Rijndael object.
Rijndael RijndaelAlg = Rijndael.Create();

// Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateEncryptor(Key, IV),
CryptoStreamMode.Write);

// Create a StreamWriter using the CryptoStream.
StreamWriter sWriter = new StreamWriter(cStream);

// try
// {
// Write the data to the stream
// to encrypt it.
sWriter.WriteLine(Data);
//}
//catch (Exception e)
//{
// Console.WriteLine("An error occurred: {0}", e.Message);
//}
//finally
// {
// Close the streams and
// close the file.
sWriter.Close();
cStream.Close();
fStream.Close();
//}
//}
//catch (CryptographicException e)
//{
// Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
//}
//catch (UnauthorizedAccessException e)
//{
// Console.WriteLine("A file error occurred: {0}", e.Message);
//}

}

public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV)
{
try
{
// Create or open the specified file.
FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);

// Create a new Rijndael object.
Rijndael RijndaelAlg = Rijndael.Create();

// Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateDecryptor(Key, IV),
CryptoStreamMode.Read);

// Create a StreamReader using the CryptoStream.
StreamReader sReader = new StreamReader(cStream);

string val = null;

try
{
// Read the data from the stream
// to decrypt it.
val = sReader.ReadLine();


}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{

// Close the streams and
// close the file.
sReader.Close();
cStream.Close();
fStream.Close();
}

// Return the string.
return val;
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
return null;
}
}
}

Output is display below:

No comments:

Recent Posts