Friday, July 29, 2005

Via Virtual Earth - Homepage

Here you will find everything you need to get started creating applications that use Microsoft’s Virtual Earth. Via Virtual Earth - Homepage

RSA Public/Private key encryption/decryption in .NET

RSA Public/Private key encryption/decryption in .NET Here is some example code in C# that shows how to perform encryption/decryption using an assymetric scheme. Here are some points to remember: 1. The idea behind assymetric encryption/decryption is that its a scheme that allows others to provide you with data in a secure way. So you will generate the public-private key pair. You will then provide the public key to all those people who wish to send you secure data. Those people will use the public key to encrypt the data that they wish to send to you. When you receive the data, you will decrypt it using the private key. 2. Thus: use the public key for encryption and the private key for decryption 3. When using the RSACryptoServiceProvider, you will encrypt and decrypt bytes. Normally the data that you wish to encrypt will be a string. Its best to use Unicode encoding to do the GetBytes and GetString. Using any of the other encoding schemes can lead to loss of bytes. So while decryption if you get a bad data or bad key exception, then check if your loosing bytes between the encryption-decryption. Here is the example code:------------------------------------------------ using System.Security.Cryptography; using System.Text; UnicodeEncoding ByteConverter = new System.Text.UnicodeEncoding(); string privateKey = "l1iFuLPXWfkISoqSyyl1GAPIMK2+a7f9FuwH0P0BGMy+78k9CBulH0S1ETuGwOzbadMdXrujbI9IqDMCBGqX+Z9BbimhP/8Hy3ZNIRdcj6mfAMILBUSaa2N4xfyxdR01KZ4k+x8gXWwF+ZOWfp5TimEEGZ2hTLwZSGfh2GjyYRE=AQAB


string publicKey = "l1iFuLPXWfkISoqSyyl1GAPIMK2+a7f9FuwH0P0BGMy+78k9CBulH0S1ETuGwOzbadMdXrujbI9IqDMCBGqX+Z9BbimhP/8Hy3ZNIRdcj6mfAMILBUSaa2N4xfyxdR01KZ4k+x8gXWwF+ZOWfp5TimEEGZ2hTLwZSGfh2GjyYRE=AQAB"; //use the following lines to generate your own set of keys //RSACryptoServiceProvider rsaCSProviderKeyGen = new RSACryptoServiceProvider(); //string publicKey = rsaCSProviderKeyGen.ToXmlString(false); //string privateKey = rsaCSProviderKeyGen.ToXmlString(true); string toEncrypt = "Hello World of Public/Private key encryption-decryption"; RSACryptoServiceProvider rsaCSProviderEncrypter = new RSACryptoServiceProvider(); rsaCSProviderEncrypter.FromXmlString(publicKey); byte []encryptedStringAsByte = rsaCSProviderEncrypter.Encrypt(ByteConverter.GetBytes(toEncrypt),false); string encryptedString = ByteConverter.GetString(encryptedStringAsByte); RSACryptoServiceProvider rsaCSProviderDecrypter = new RSACryptoServiceProvider(); rsaCSProviderDecrypter.FromXmlString(privateKey); byte []decryptedStringAsByte = rsaCSProviderDecrypter.Decrypt(ByteConverter.GetBytes(encryptedString),false); string decryptedString = ByteConverter.GetString(decryptedStringAsByte); Console.WriteLine(decryptedString);


The CAIDA Web Site - CAIDA : HOME: "CAIDA, the Cooperative Association for Internet Data Analysis, provides tools and analyses promoting the engineering and maintenance of a robust, scalable global Internet infrastructure."

Antenna on the Cheap & the Pringles Yagi antenna design

Antenna on the Cheap (er, Chip) " Cantenna " - yagi design for 802.11b wireless application

Thursday, July 28, 2005

Q: How do I write a method that accepts a variable number of parameters?

Q: How do I write a method that accepts a variable number of parameters?

A: Languages like C and C++ have always offered some means for using and creating functions capable to accept a variable number of parameters. The most widely known example of a function which takes a variable number of parameters is printf():

int printf(char *format, …); // the ellipsis means "variable number of params"

Using this function is pretty easy:

printf("Hello, world\n");

printf("The sum of %d and %d is %d\n", a, b, a+b);

However, the creation of such functions in these languages relays on a set of predefined macros and is not particularly elegant or intuitive.

C# offers an elegant solution to this problem through parameter arrays. A parameter array is a single-dimensional array included as the last parameter in the parameter list of a method:

public string Concat(string separator, params string[] strings)


string result = "";

for (int i = 0; i <>


if (i > 0)

result += separator;

result += strings[i];


return result;


Such a function can be called in two different ways:

a) Passing the function an array instance argument:

string[] names = { "Anders", "Eric", "Scott", "Duncan" };

MessageBox.Show(Concat("+", names) + " = great team");

b) Using zero or more type-compatible arguments for the parameter array:

MessageBox.Show(Concat("+", "Anders", "Eric", "Scott", "Duncan") +

" = great team");

In this case, the invocation will create an array from the supplied arguments and use it as the actual argument.

Thanks to the unified .NET type system, object[] can be used as “common denominator” for arguments of different types:

public int SumTheIntegers(params object[] list)


// sum all the integers included in list

int sum = 0;

foreach (object o in list)

if (o.GetType() == typeof(int))

sum += (int) o;

return sum;


Tuesday, July 26, 2005

Passing Function Pointer to Unmanaged Class from Managed Class

While working with the FlexLM library, I had to register some callbacks. As I was working with managed C++, I had to come up with a way of passing a function pointer from my managed class to the unmanaged FLexLM library calls. (I am not using the DLL, but staticly linking to the FlexLM library). What I found is there is no way to pass a managed function to the unmanaged function. __gc public class managedClass { public: void myCallBack() { } managedClass() { //set_callback is a function from the unmanaged library set_callback( (void*) myCallBack) //this wont work } } The above code will not work and you will get a compiler error, which says something like cannot convert __gc* to void *. I tried delegates which also wont work here. One thing that one could do, is to create the myCallBack as a static function outside of the managedClass. This would work. But to make my solution look elegant, what I did was to create a __nogc class which was nested within the managedClass. __gc public class managedClass { private: __nogc class unmanagedClass { public: static void CallBack() { managedClass::Instance->myCallBack(); } } static managedClass *thisClass; public: void myCallBack() { } __property static managedClass* get_Instance() { return thisClass; } managedClass() { thisClass = this; //set_callback is a function from the unmanaged library set_callback( (void*) unmanagedClass::CallBack) //this will work } } By doing it like this, when the unmanaged library decides to call my unmanagedClass's CallBack, it will call the ManagedClass's instance's callback. I can then do custom processing thats tailored to the current instance! Coolio!

Friday, July 22, 2005

Finding GUIDS using Visual Studio.NET's regular expression

Ever wanted to find all the GUIDS defined in your code. This regular expression will help you find them using Visual Studio .NET's find dialog [a-zA-Z0-9]^8(-[a-zA-Z0-9]^4)^3(-[a-zA-Z0-9]^12) this will find all guids with the following format f9f739bf-0f66-4aee-b0f1-6af9bfe34751

Wednesday, July 20, 2005

E.W.Dijkstra Archive: Home page

"The competent programmer is fully aware of the strictly limited size of his own skull;therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague" -Edsger Dijkstra The manuscripts of Edsger W. Dijkstra (1930–2002)

Tuesday, July 19, 2005

Viewing the contents of the GAC

One can rename the shfusion.dll file (found in the Windows/System32) folder, or Add a binary value named "DisableCacheViewer" to the registry key HKLM\Software\Microsoft\Fusion and set it to a non zero value.

Tuesday, July 12, 2005

Thursday, July 07, 2005

Is System.String a value type or a reference type?

First, what is the difference between a value type and a reference type? A value type stores its data. When a value type is passed around, its data is copied and passed. This means that manipulating a copy of a value type, does nothing to the object that was copied. int i = 100; int j = i; j = 200; at this point i = 100 and j = 200. This is the behavior of value types. A reference type on the other hand stores only a reference to the data that it stores. This means that a copy of a reference type is just an alias to the original object. Classes are an example of a reference type. Lets look at an example. public class refType { public string s = ""; public refType() { } } public class MyClass { public static void Main() { refType r1 = new refType(); r1.s = "refType r1"; refType r2 = r1; r2.s = "refType r2"; Console.WriteLine("R1 " + r1.s); Console.WriteLine("R2 " + r2.s); } } The output of the above code will be R1 refType2 R2 refType2 This is because by writing r2 = r1, we have made r2 into an alias to r1, as r1 and r2 reference the same data. Thats the reason that they are called reference types. Now, about the topic: is System.String a reference type or a value type? Lets look at some code first: public class refType { public string s = ""; public refType() { } } public class MyClass { public static void Main() { int i = 100; int j = i; j = 200; Console.WriteLine("i " + i.ToString()); Console.WriteLine("j " + j.ToString()); refType r1 = new refType(); r1.s = "refType r1"; refType r2 = r1; r2.s = "refType r2"; Console.WriteLine("R1 " + r1.s); Console.WriteLine("R2 " + r2.s); string s1 = "empty"; string s2 = s1; s2 = "hello"; Console.WriteLine("S1 " + s1); Console.WriteLine("S2 " + s2); } } The output of the above code will be: i 100 j 200 (which is what we would expect of a value type) R1 refType r2 R2 refType r2 (which is what we would expect of a class, a reference type) S1 empty S2 hello (does this mean string is a value type?) As the output shows: S2 is not an alias to S1, so strings must not be a reference type and hence must be a value type. But that is not true! If you look at the .NET documentation, System.String is a reference type. Then why is s2 not an alias of s1, just like r2 is an alias of r1. (ie, why does s1 not change when s2 changes, whereas r1 changes when r2 is assigned a different value?) The reason is that the assignment operator for a string is overloaded. It actually creates a new instance of the string object. So s1 = "new string" is actually s1 = new string(). When s2 is assigned s1, .NET creates a new string object and copies the data from s1 into s2. This is the reason that s2 is not an alias to s1, because .NET behind the scenes created a new string object for you. System.String behaves like a value type, even when its passed as a parameter to a method. This is because a copy of it is made and sent to the method. So changing its value within the method, will not get reflected outside the scope of the method. (unless the parameter was sent in with the ref keyword). This demonstrated by the following code: public class refType { public string s = "empty"; public refType() { } } public class MyClass { public static void Main() { int i = 0; Console.WriteLine("Before " + i.ToString()); intFunc(i); Console.WriteLine("After " + i.ToString()); Console.WriteLine(); refType r1 = new refType(); Console.WriteLine("Before " + r1.s); refFunc(r1); Console.WriteLine("Main " + r1.s); string s1 = "hello world"; Console.WriteLine("Before " + s1); StringFunc(s1); Console.WriteLine("After " + s1); Console.WriteLine(); } static void StringFunc( string s ) { s = "Changed the data"; Console.WriteLine("StringFunc " + s); } static void intFunc( int s ) { s = 1000; Console.WriteLine("intFunc " + s.ToString()); } static void refFunc( refType s ) { s.s = "refFuncSaysHello"; Console.WriteLine("refFunc " + s.s); } } The output of which would be : Before 0 intFunc 1000 After 0 (as expected of a value type) Before empty refFunc refFuncSaysHello Main refFuncSaysHello (as expected of a reference type) Before hello world StringFunc Changed the data After hello world (string a reference type, behaves like a value type)