Cryptography Using the .Net Framework and C#
|
|
Overview. Microsoft .NET Framework is a development and
execution environment that allows interoperability between programming
languages, programming libraries, and the Windows platforms. The .NET
Framework also provides a variety of tools to support major encryption
algorithms, hashing algorithms, and digital signatures. Two programming
languages, C# and Visual Basic, can be used to implement these
algorithms through the .NET Cryptography classes. In this lab you will
lean how to implement some basic cryptographic algorithms provided by
the .NET class using C# programming.
This lab is divided into
six sections. The first section is an introduction to the lab,
which covers two basic C# program. The second section covers
encryption under the .NET Framework. The third and fourth section
covers authentication, hash algorithms, and digital signatures.
The remaining two sections is the lab exercises and resources.
This lab is used in
Cryptography TELECOM 2820/INFSCI 2170.
Requirements. To complete this tutorial you will need a PC with Microsoft Visual C# .NET installed. Visual C# .NET is
included in the Microsoft Visual Studio .NET package, which can found on
the computers in the SIS lab. All examples in this lab manual were
tested with Microsoft Development Environment 2003/.NET Framework
version 1.1. You should also have basic knowledge of
Object-Oriented Programming (OOP).
C# .NET Programming. C# is an OOP language; thus, the fundamental concepts of
a class and object are similar to
those of C++ or Java. The following examples should give you enough
information on how to write basic C# programs. However, if you require
additional resources you should read the
MSDN C# Tutorial, and spend sometime
reading some of the other online tutorials [2]-[4] found in the
references section. You can also refer to [5] and [6] for further studies
if you are very serious.
To understand how the .NET Framework works, you should skim through
reference [7]. Furthermore, if you are looking for anything
related to .NET or C# programming, the online MSDN library
http://msdn.microsoft.com/library/
is very useful.
Simple C# Sample Program 1. This section will introduce
you to simple Microsoft Visual Studio .NET commands and instructions
pertaining to programming in C#. You will also be given a simple
C# program, which will be an informal introduction to the language if
you are not familiar.
To launch Microsoft Visual Studio .NET open the program menu and
select it. After starting Visual Studio to create a new program
open the File Menu from the menu toolbar. Once inside the File
Menu select New Project, which will invoke the New Project window.
From the New Project window you can choose the type of project you wish
to create. Select the Visual C# Project option and click Console
Application for the template. Enter HelloWorld as the project
name, which will also be th ename o your executable file in the
HelloWorld/bin/Debug directory. Visual C# .NET will create a file Class1.cs by default. This file
should be opened automatically. If you do not see the file, double-click
on the file name shown in the Solution Explorer window to open it.
In the Solution Explorer window, right-click on Class1.cs and rename
it to HelloClass.cs. Replace the content created by default in HelloClass.cs with the
following code:
// HelloClass.cs
using System;
using System.Text;
class HelloClass
{
public static int Main()
{
Console.WriteLine("Hello World!\n");
string message_str = "Hello World!";
Console.WriteLine("String printout: {0}\n", message_str);
byte[] message_byte =
Encoding.Default.GetBytes(message_str);
Console.WriteLine("Byte printout (in hex):\n");
int count = 0;
foreach (byte b in message_byte)
{
Console.Write("{0:X2} ", b);
count++;
} // end of foreach
Console.WriteLine("\n\nThe number of bytes in {0} is{1}\n", message_str, count);
return 0;
} // end of main
} // end of class
Note:
- Main(), with a capital “M”, is the entry point for
the program.
- In this program, you are using two Namespaces, System and System.Text.
- The System namespace is the required namespace
that must be included in every C# program. The namespace restricts a name’s scope, making it meaningful only within the
defined namespace. The other namespace, System.Text is
included here because you are using the Encoding class to convert a
string into an array of bytes.
Compile the program by choosing Build or click the Build button.
Check for any error messages and if there are none run the program by
choosing Debug and select Start Without Debugging. Examine the
source code and output line by line to ensure complete understanding of
its workings. Basically, the program prints out the classic word
Hello World in different ways.
When working on this lab exercise, you may have several test
programs (*.cs) contained within your project. Make sure that the
program you want to compile is “included” in the project space. This
can be done by right-clicking on the file name in the Solution Explorer
to set the property to include In project. Similarly, the program or
class that will not be compiled must be set to Exclude From Project.
Simple C# Sample Program 2. To create another program,
in the Solution Explorer window, right-click on the project name, which
is HelloWorld, and select Add. Once inside the Add menu select Add
Class, and then enter HelloClass2.cs. Replace all the
content created by default in HelloClass2.cs with the following code:
// HelloClass2.cs
using System;
class HelloClass2
{
public byte[] Key;
public void InitializeKey()
{
Key = new byte[] { 0xFC, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12,
0x13, 0x14, 0x15, 0xAA };
} // end of InitializeKey
public void SayHi() {Console.WriteLine("Hi there!"); }
public string strY = "AES";
public int intX = 128;
} // end of SayHi
//Program entry point.
class HelloApp
{
public static int Main()
{
HelloClass2 c1 = new HelloClass2();
c1.SayHi();
c1.InitializeKey();
int num_byte = 0;
foreach (byte b in c1.Key)
{
Console.Write("{0:X2} ", b);
num_byte++;
} // end of foreach
Console.WriteLine("\n" + "The number of bytes:\t"
+ num_byte);
Console.WriteLine("\nWe're using {0} with {1}-bit key.\n",
c1.strY, c1.intX);
return 0;
} // end of main
} // end of class
Since the file HelloClass.cs in the previous example is still
included in your project you have to set that file to Exclude From
Project before compiling HelloClass2.cs. (see step 11).
If you do not remember how to do this refer to the previous example.
Compile and run the program by selecting Debug and then Start Without
Debugging. Examine the source code and the output line by line. Make sure
that you understand everything in the program. This example shows how to
access members of another class. Here the Main method is put inside the
class HelloApp, which is used for program entry point. The InitializeKey
method will be used later in an encryption example in the next section.
Now you have completed the basic C# programs. The next section will
describe how to use classes provided by the .NET Security Framework for
cryptography algorithm implementations.
|