Peterson's Ponderings

Technical findings, ideas, thoughts and news directly from me.
in

April 2006 - Posts

Encrypted data in XML

It's been a few years since the W3C released the recommendation standard for XML Encryption within an XML document (http://www.w3.org/TR/xmlenc-core/).  To get the EncryptedData element into your XML was not very simple at all though, until now.  Visual Studio 2005 contains functions to support encrypting entire elements within a loaded XML document according to the XML standard.

The concept is pretty simple.  You have some confidential data in your XML that you don't want any Joe NetworkOp to read in the text file that you write somewhere to persist your data.  When you encrypt an element section, the element and all its contents are replaced with an EncryptedData element similar to the following:

    <EncryptedData Type='http://www.w3.org/2001/04/xmlenc#Element'
     xmlns='http://www.w3.org/2001/04/xmlenc#'>
      <CipherData>
        <CipherValue>A23B45C56</CipherValue>
      </CipherData>
    </EncryptedData>

To encrypt data in your XML document use the EncryptedXML class from the System.Security.Cryptography.Xml namespace.  There is an EncryptData and DecryptData method to perform the encryption.  You'll need to maintain the key that is used to encrypt/decrypt the data.  While the methods support encrypting the key into the EncryptedData result you still need to maintain the key that encrypts the key.  The EncryptedXML class accepts the key as a parameter on both the EncryptData and DecryptData methods.

MSDN has great sample code (http://msdn2.microsoft.com/en-us/library/system.security.cryptography.xml.encryptedxml.aspx) showing how easy it is to utilize encryption in an XML document.

An Option for Rounding

The Math.Round function in Visual Studio 2005 got a slight make over.  If you recall, in VS 2003 Math.Round always implemented the 'bankers rounding' which was if the number was at the half way point between an even an odd number it would always round to the even number.  i.e. 2.5 rounded to 2 and 3.5 rounded to 4.  This was obviously a bit confusing and unexpected to a lot of programmers with math backgrounds and not banker backgrounds, as in math you expect it to always round up if the decimal was at the half-way point. 

In VS 2005 you can now specify with a parameter whether you want to round with the bankers method or the mathematical.  The Round method now has an overload allowing a MidpointRounding enumeration for you to specify.  The options for the enumeration are ToEven and AwayFromZero.  Away from zero will always force rounding of a number at the half way point to round to the next higher number away from zero (i.e. -3.5 goes to -4 and 3.5 goes to 4).  If you don't use the overload then the method continues to work in the bankers rounding mode so make sure if you are porting code from 2003 to 2005 to change your Round method to utilize the overload in the proper scenarios.

 

Connecting to Oracle via ODBC (A How To)

Recently I needed to connect to an Oracle database from .NET code and found that utilizing the ODBC connection isn't as straight forward as using ODBC for some other RDBM systems.  Usually you just need the ODBC driver installed on your machine and the proper connection string when creating your database connection object, but right away I ran into a problem - Oracle needs several Oracle client dlls on the machine before you can even set up an ODBC DSN.  I had to do a lot of poking around to figure out the necessary components that needed to be installed so I thought I'd list it all in a single post to help anyone else looking to set this up.

If you go into the ODBC manager (via control panel) you'll see a Microsoft Oracle ODBC driver listed.  But when you select it to continue configuring your DSN you'll receive an error message stating that the wizard is unable to continue because it could not load the Oracle connectivity libraries.  To get by this you need to get a hold of an Oracle Client CD (the Oracle Client is free from Oracle but there its not downloadable from anywhere that I could find on their web site).  Install the Oracle Client and select to install the RunTime functionality (you only need the runtime set up to get ODBC to work, you don't need the full administrator functionality unless you plan on using the database management tools).  This will install the required Oracle network libraries as well as the Oracle Universal Installer which is needed to install any other Oracle add-in software.  Next you need to download the current Oracle ODBC drivers which can be found on the Oracle web site here (http://www.oracle.com/technology/software/tech/windows/odbc/index.html) (but beware, you need to create an account first, its free but you have to fill out a lot of information).

Extract the downloaded file and then run the Oracle Universal Installer off of your Start menu.  Point the installer to the products.jar file that exists in a sub-directory of the extracted download.  In the installer wizard select the Oracle ODBC driver.

Now you're set.  Open up the ODBC administrator from the Control Panel again and select to add a new DSN.  This time select the 'Oracle from Orahome92' (or similar depending on the version you installed) as the driver.  You could, at this point, use the Microsoft driver, but I found the version that comes with XP was older and I had problems connecting and executing some statements against Oracle 9.2 using it.  Switching to the specific driver from Oracle made my connection and statements begin working without any further changes.

It's a lot more work then you'd first think (why do I need a whole client install on my machine just to do ODBC for Oracle!!) but once you get past the installs you'll be in good shape.