Friday, July 17, 2009

.NET Runtime version 2.0.50727.3053 - Fatal Execution Engine Error – VS 2008 Crashing with Web Site Project

Visual Studio 2008 Development Team Edition kept crashing when opening a certain solution.  I looked in the event viewer and saw this:


I tried unloading and reloading each project in the solution individually to see which project was causing the problem.

After a while of Googling, I found others who had unistalled ReSharper and had success.  That didn’t work for me.  After a little bit more searching, I found someone who had success unistalling PowerCommands for Visual Studio.

That worked for me.  Hope that helps someone reduce brain damage from bashing their head against the wall.

Happy Coding!

Thursday, July 16, 2009

Triple DES Encryption implementation compatible between Java and .NET!

One problem that I have had to solve in the past was making encryption between Java and .NET possible.  Specifically, I was charged to read an encrypted query string parameter from a Java system in my ASP.NET application.  After a few iterations of incompatible implementations, I created an implementation in both .NET (c#) and Java to ensure compatibility.

Since this caused me so much pain, I thought I would share my implementation with you in hopes that it will work for you as well.

Let me know if you have any questions or want to provide a decrypt implementation for the java class.




.NET (C#) Implementation

/// <summary>
/// Encryption utility class that implements Triple DES algorithm
/// </summary>
public class TripleDESImplementation
    //Encryption Key
    private byte[] EncryptionKey { get; set; }
    // The Initialization Vector for the DES encryption routine
    private byte[] IV { get; set; }

    /// <summary>
    /// Constructor for TripleDESImplementation class
    /// </summary>
    /// <param name="encryptionKey">The 24-byte encryption key (24 character ASCII)</param>
    /// <param name="IV">The 8-byte DES encryption initialization vector (8 characters ASCII)</param>
    public TripleDESImplementation(string encryptionKey, string IV)
        if (string.IsNullOrEmpty(encryptionKey))
            throw new ArgumentNullException("'encryptionKey' parameter cannot be null.", "encryptionKey");
        if (string.IsNullOrEmpty(IV))
            throw new ArgumentException("'IV' parameter cannot be null or empty.", "IV");

        EncryptionKey = Encoding.ASCII.GetBytes(encryptionKey);
        // Ensures length of 24 for encryption key
        Trace.Assert(EncryptionKey.Length == 24, "Encryption key must be exactly 24 characters of ASCII text (24 bytes)");

        this.IV = Encoding.ASCII.GetBytes(IV);
        // Ensures length of 8 for init. vector
        Trace.Assert(IV.Length == 8, "Init. vector must be exactly 8 characters of ASCII text (8 bytes)");

    /// <summary>
    /// Encrypts a text block
    /// </summary>
    public string Encrypt(string textToEncrypt)
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = EncryptionKey;
        tdes.IV = IV;

        byte[] buffer = Encoding.ASCII.GetBytes(textToEncrypt);
        return Convert.ToBase64String(tdes.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));

    /// <summary>
    /// Decrypts an encrypted text block
    /// </summary>
    public string Decrypt(string textToDecrypt)
        byte[] buffer = Convert.FromBase64String(textToDecrypt);

        TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
        des.Key = EncryptionKey;
        des.IV = IV;

        return Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));

Java Implementation

public class TripleDesImplementation {
    private String key;
    private String initializationVector;
    public TripleDesImplementation(String key, String initializationVector)
        this.key = key;
        this.initializationVector = initializationVector;

    public String encryptText(String plainText) throws Exception{
    //----  Use specified 3DES key and IV from other source --------------
      byte[] plaintext = plainText.getBytes();
      byte[] tdesKeyData = key.getBytes();

      byte[] myIV = initializationVector.getBytes();

      Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
      SecretKeySpec    myKey = new SecretKeySpec(tdesKeyData, "DESede");
      IvParameterSpec ivspec = new IvParameterSpec(myIV);

      c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
      byte[] cipherText = c3des.doFinal(plaintext);

      return Base64Coder.encodeString(new String(cipherText));

Wednesday, July 8, 2009

Managing Session State – Using A Strongly Typed Session Store

Download the example VS 2008 solution here.

As an ASP.NET developer, I’ve found myself in situations where I’ve needed to manage state for the current use by using Session.  I know that there are other ways to keep track of a user’s state aside from Session, and that the use of Session should be very used with discretion. The point of this post is not to say that any time you need to keep track of state in your web application that you should use Session, but rather to present one way of effectively managing this if you ever find that Session is something that you want or need to use.

In my experience, I’ve seen many issues with developers using Session ineffectively and dangerously.  Here are some common follies of using (or misusing) Session:  assuming that something is in Session when it may or may not be, mistyping of the string literal when placing something in or retrieving it from Session (e.g. Session[“UserId”] and Session[“UserID”] are not referencing the same thing), and accidentally having the same Session variable mean two different things.

I created a pattern in the form of a SessionRepository class that will hopefully alleviate some of these issues that are commonly found throughout ASP.NET projects.

This is an example SessionRepository property:


This is an example snippet of code that you write while using the SessionRepository:


This gives us a uniform way of setting and getting things from the session without as much work that has to be done with casting and null checking.  It also gives uniform way of removing from the session.  We also are able to easily find all references to a given session variable without having to search our entire solution for strings.  We can just right click on the property and click ‘Find All References,’ and we’re done.

I’ve included a link to an example VS 2008 Web Application project that uses this pattern.  Refer to the SessionState folder within the project.  Hopefully this will prevent some of the head banging that I’ve experienced while working with Session.  Happy Coding!


Download it here.