Click here to Skip to main content
13,095,218 members (73,404 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads


29 bookmarked
Posted 16 Oct 2007

Password Safe Database Reader Library in C# for .NET

, 16 Oct 2007
An independent library implementation to read Password Safe Password Manager V3 database files
<!--                           INTRODUCTION                                

 The Code Project article submission template (HTML version)

Using this template will help us post your article sooner. To use, just 
follow the 3 easy steps below:
     1. Fill in the article description details
     2. Add links to your images and downloads
     3. Include the main article text

That's all there is to it! All formatting will be done by our submission
scripts and style sheets. 

<!--                        IGNORE THIS SECTION                            -->
    <title>The Code Project</title>
BODY, P, TD { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt }
H2,H3,H4,H5 { color: #ff9900; font-weight: bold; }
H2 { font-size: 13pt; }
H3 { font-size: 12pt; }
H4 { font-size: 10pt; color: black; }
PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }
CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }
    <link rel="stylesheet" type="text/css" href="">
<body bgcolor="#FFFFFF" color="#000000">
    <!-------------------------------     STEP 1      --------------------------->
    <!--  Fill in the details (CodeProject will reformat this section for you) -->
Title:       Password Safe Database Reader
Author:      Svante Seleborg
Member ID:   229979
Language:    C# 2.0
Platform:    .NET 3.0
Technology:  Cryptograhpy
Level:       Intermediate
Description: An independent implementation to read Password Safe Password Manager V3 database files.
Section      General C#
SubSection   C# Libraries
    <!-------------------------------     STEP 2      --------------------------->
    <!--  Include download and sample image information.                       -->
    <ul class="download">
        <li><a href="">Download source - 60 Kb</a></li>
    <!-------------------------------     STEP 3      --------------------------->
    <!--  Add the article text. Please use simple formatting (<h2>, <p> etc)   -->
        Password Safe is an open source password manager available for download at <a href="">
            sourceforge</a>, written in MFC/C++. This is a useful program, but I had a need
        to integrate the possibility to import such content to my <a href="">
            online</a> password manager which uses a format based on <a href="">
                Encrypted XML</a>.
        There are many potentially creative things to do with Password Safe files, but many
        such ideas may be stopped by the apparent difficulty of decrypting and interpreting
        the database format used. This library provides an easy to use interface, patterned
        on the general .NET Framework readers, such as XmlReader.
        Read the background of Password Safe at their web site, but briefly this originated
        as a product from <a href="">Bruce Schneier</a>, at <a href="">
            Counterpane</a> who subsequently published the source code, and it now lives
        an independent life at <a href="">Sourceforge</a>.
        The code presented only implements a <code>PasswordSafeReader</code> at this time,
        but it should be relatively trivial to follow the general implmentation pattern
        to make a <code>PasswordSafeWriter</code>. If anyone makes such a beast, I'll be
        happy to integrate the source.
        Using the code
        The solution in the source code package contains two projects, one for the actual
        library, one for a simple demo and test using the <a href="">NUnit</a>
        framework to demonstrate usage as well as to provide a validation of the implementation.
        The basic reader loop, devoid of error checking (the reader will throw InvalidDataException
        for a bad key or bad database format, and InvalidOperationException for an internal
        implementation error) can look like this:
PasswordSafeHeader header;
List&lt;PasswordSafeRecord> records = new List&lt;PasswordSafeRecord>();

using (PasswordSafeReader reader = new PasswordSafeReader(stream))
    while (reader.Read())
        switch (reader.CurrentPartType)
            case PasswordSafePartType.Header:
                header = reader.Header;

            case PasswordSafePartType.Record:

        Points of Interest
        An interesting discovery when implementing this code, was that I discovered a minor
        security flaw in the format. The database is encrypted and also protected with a
        keyed hash, an HMAC to ensure the integrity of the data. The problem is that the
        HMAC does not actually protect all the the bits it should, it does not protect the
        format meta data, i.e. record lengths and field type codes. The real-world risk
        of this is low, since it is all encrypted, but it's still a flaw.
        Password Safe has gone through several generations, this code implements the Version
        3 format, which among other things of note use the <a href="">
            Twofish</a> block cipher for encryption. The Twofish implementation used was
        written by <a href="">Shaun
        The source code as published here is licensed under the GPL version 3.0 - but if
        this is a problem for your project, in most cases I'll be happy to license it to
        you for free under less restrictive terms. Just send me an e-mail.
        This is version .
    <!-------------------------------    That's it!   --------------------------->

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Svante Seleborg
Web Developer Axantum Software AB
Sweden Sweden
I've been working with all aspects of software development since 1979 - from compiler construction to management. Currently I'm an independent consultant mostly specializing in computer security. Please see my homepage for contact details.

I speak C like a native, and have a pretty good grasp of C++. The most recent five years C# has been the main development language. Traditionally Unix has been the dominating environment, but currently the scales have tipped over to Windows, due to market demands but I'm equally at home developing in both environments.

When I'm not coding I'm usually sitting on one of my 4 bikes, indoors or outdoors, on the road or in the woods.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 16 Oct 2007
Article Copyright 2007 by Svante Seleborg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid