Sunday, 22 May 2011

Cryptography algorithm in C#.Net

  Substitution cypher:


using System;
using System.Text;
using System.IO;
using System.Net.Sockets;
class encriptclient
{
  public static void Main(string[] args)
 {
  string s1;
  string s2="";
  char[] c=new char[10];
  int a,b;
  string server=args[0];
  int port=Int32.Parse(args[1]);
  TcpClient client=null;
  NetworkStream netStream=null;

  try
 {
  client=new TcpClient(server,port);
  Console.WriteLine("Connected to server");
  netStream=client.GetStream();
  }
  catch(Exception e)
 {
  Console.WriteLine(e.Message);
  }

 for( ; ;)
 {
  Console.WriteLine("Enter the string");
  s1=Console.ReadLine();
  c=s1.ToCharArray();
  for(int i=0;i<s1.Length;i++)
  {
  a=(int)c[i];
  b=(a+2);
  if(b>122)
  b=b-26;
  c[i]=(char)(b);
  }
  for(int i=0;i<s1.Length;i++)
  s2=s2+c[i];
  Console.WriteLine("The encripted string is:{0}",s2);
  byte[] buffer=Encoding.ASCII.GetBytes(s2);
  netStream.Write(buffer,0,buffer.Length);
  byte[] buf=new byte[32];
  netStream.Read(buf,0,buf.Length);
  Console.WriteLine("The decripted string is:          {0}",Encoding.ASCII.GetString(buf,0,buf.Length));
 }
 netStream.Close();
 client.Close();
 }
}


Server side Decryption:

 using System;

 using System.Text;

 using System.Net;

 using System.Net.Sockets;

 class decriptserver

 {

   private const int size=32;

   public static void Main(string[] args)
  {
   char[] c=new char[10];
   int a,b;
   int port=Int32.Parse(args[0]);
   TcpListener listener=null;
   try
   {
    listener=new TcpListener(IPAddress.Any,port);
     listener.Start();
   }
   catch(Exception e)
  {
   Console.WriteLine(e.Message);
   }
  for(;;)
  {
  TcpClient client=null;
  NetworkStream netstream=null;
  client=listener.AcceptTcpClient();
  netstream=client.GetStream();
  Console.WriteLine("handling client");
byte[] buffer=new byte[size];
  netstream.Read(buffer,0,buffer.Length);
  Console.WriteLine("The string is{0}:",Encoding.ASCII.GetString(buffer,0,buffer.Length));
  string s1=Encoding.ASCII.GetString(buffer,0,buffer.Length);
  Console.WriteLine("the string is:{0}",s1);
  c=s1.ToCharArray();
 for(int i=0;c[i]!='\0';i++)
 {
 a=(int)c[i];
 b=(a-2);
 if(b<97)
 b=b+26;
 c[i]=(char)b;
 }
 string s2="";
 for(int i=0;i<s1.Length;i++)
 s2=s2+c[i];
 Console.WriteLine("The original text is:{0}",s2);
 byte[] buf=Encoding.ASCII.GetBytes(s2);
  netstream.Write(buf,0,buf.Length);
  netstream.Close();
  client.Close();
  }
 }



Monday, 9 May 2011

Introduction to C#


Define C# ?

    C# (pronounced as 'C sharp') is programming Language developed by Microsoft Corporation. It is a fully object oriented language like java and is the first component -oriented language. It is mainly used for NET Framework. It is a purely object oriented language suitable for developing Web-based applications.

What is new in C#?

  C# is mainly used for developing system for the .NET platform.
NET Framework provides some services such as   

  •    .NET infrastructure tools   
  •   .NET user experience
  •    .NET building block
  •    .NET device software

Orgins of .NET Technology:

    The .Net has developed through three important phases of development

  •         OLE  technology
  •         COM technology
  •         NET technology
  
OLE Technology
    
     OLE(Object Linking and Embedding) technology was developed by Microsoft .
It provides interprocess communications.

  •               To embed documents from one application into another application             
  •               To  enable one application to manipulate objects located in another application.
e.g)   C#.NET windows program store the data into MSAccess or SQL.

COM Technology
 
   COM(Component Object Model) is componentware .It is inter-module communication.


  •         Reduces the overall complexity of software.  
  •         Enables distributed development across multiple organizations or departments.
.NET Technology

 .NET is network technology provides Intermediate Language to web applications .It is intersite communication.

What is difference between Methods and Functions?

   Functions is defined  as outside of the class .  e.g.) main() in C++,whereas methods are declared inside of the class .e.g) Main()  in C#.

What is difference between  Constructors and Methods?
 
   Constructor is special member function .Constructor name same as class name, where as methods are not same as class name. whenever an object is created the constructor is automatically invoked.

e.g) class  sample 
     {
        Sample()   //constructor
        {
        //constructor without parameter
         }
       Sam()   //method
      {
        //method without parameter
       }
 }