Archive

Tag Archives: FRAMEWORK

Following is the sample which shows how can you use relations in the LINQ for query and get the data out of various objects


class CustomerOrders
{
public IEnumerable GetCustomerOrders()
{
Customer[] customers = {
new Customer{ CustomerID =1, CustomerName = "Pratap Singh"},
new Customer{ CustomerID =2, CustomerName = "Sikandar"},
new Customer{ CustomerID =3, CustomerName = "Ram Parsad Patnaik"}
};

Order[] orders = {
new Order{ CustomerID = 1, OrderID = 1 , OrderDate = Convert.ToDateTime("10/Apr/2004")},
new Order{ CustomerID = 1, OrderID = 2 , OrderDate = Convert.ToDateTime("3/Jan/2005")},
new Order{ CustomerID = 3, OrderID = 1 , OrderDate = Convert.ToDateTime("23/Mar/2006")},
new Order{ CustomerID = 2, OrderID = 1 , OrderDate = Convert.ToDateTime("14/Nov/2005")},
new Order{ CustomerID = 3, OrderID = 2 , OrderDate = Convert.ToDateTime("19/Dec/2008")}
};

var Query = from clist in customers
join oList in orders
on clist.CustomerID equals oList.CustomerID
select clist;

return Query;
}

}

Here in sample look at the join part of the LINQ join oList in orders on clist.CustomerID equals oList.CustomerID. In smiliar way you can create various joins and create a relational model in LINQ

This time we will perform RSA Encryption(Asymmetric encryption) with the help of the RSACryptoServiceProvider Class. Now its key length depends upon the Cryptographic Provider you have. For MS Base Cryptographic Provider you can have key length from 384 to 512 bits in increment of 8 bits where as if you have installed MS Enhanced Cryptographic Provider then you can have key of 384 to 16384 bits in increment of 8 bits.


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

class CryptRSA    
{

static void main()
{
UnicodeEncoding bytConvertor = new UnicodeEncoding();
byte[] plainData = bytConvertor.GetBytes("Sample data");
RSACryptoServiceProvider RSAServiceProvider = new RSACryptoServiceProvider();

byte[] enData = Encrypt(plainData, RSAServiceProvider.ExportParameters(false));
Console.WriteLine("Encrypted Output: {0}", bytConvertor.GetString(enData));

byte[] deData = Decrypt(enData, RSAServiceProvider.ExportParameters(true));
Console.WriteLine("Decrypted Output: {0}", bytConvertor.GetString(deData));
}

static private byte[] Encrypt(byte[] DataToEncrypt, RSAParameters keyInfo)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(keyInfo);
return RSA.Encrypt(DataToEncrypt, false);
}

static private byte[] Decrypt(byte[] DataToDecrypt, RSAParameters keyInfo)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(keyInfo);
return RSA.Decrypt(DataToDecrypt, false);
}
}

Doing Encryption with various well know algorithm is not so easy. Writing those and managing is bit difficult. But microsoft provides assemblies/libraries and built in classes with the help of which you can peform encryption with even writting single code of encryption algorithm. 

There are two major kind of encryptions

1. Secret Key (Also know as Symmetric encryption since single key is used to encrypt and decrypt the data)

Here single shared key is maintained to secure the data from unauthorised access. These keys are used to perform the encryption and decryption of the data

2. Public Key

Here a pair of public and private key used in order to secure the data.

Now we will see example of Secret Key Encryption/Decryption (DES)


// Encrypt the string.

private void button1_Click(object sender, EventArgs e)
{
DESCryptoServiceProvider key = new DESCryptoServiceProvider();
byte[] buffer = Encrypt(textBox1.Text, key);
textBox2.Text = bytestostring(buffer);
textBox3.Text = Decrypt(buffer, key);
}

public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream();

// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

// Create a StreamWriter to write a string
// to the stream.
StreamWriter sw = new StreamWriter(encStream);

// Write the plaintext to the stream.
sw.WriteLine(PlainText);

// Close the StreamWriter and CryptoStream.
sw.Close();
encStream.Close();

// Get an array of bytes that represents
// the memory stream.
byte[] buffer = ms.ToArray();

// Close the memory stream.
ms.Close();

// Return the encrypted byte array.
return buffer;
}

// Decrypt the byte array.
public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
{
// Create a memory stream to the passed buffer.
MemoryStream ms = new MemoryStream(CypherText);

// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);

// Create a StreamReader for reading the stream.
StreamReader sr = new StreamReader(encStream);

// Read the stream as a string.
string val = sr.ReadLine();

// Close the streams.
sr.Close();
encStream.Close();
ms.Close();

return val;
}

Here make sure that the key for the encryption and the decryption should be same in order to make it work properly. More on this will be posted soon.

Follow

Get every new post delivered to your Inbox.