Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A C# implementation of Unix crypt()

0.00/5 (No votes)
30 Dec 2004 2  
A C# version of the Unix crypt() algorithm.

Introduction

At my office, we are currently working on a major overhaul of some of our core systems. The 'strategic' decision was made to lose most of our Unix machines and move to a more Microsoft oriented platform. One of the changes caused by this decision was the use of the Microsoft Provisioning System, so all the scripts we made over the years to help us run the business have to be converted for use by the MPF (Microsoft Provisioning Framework). We are changing the systems in a few steps, so at one point, we will have an MPS-based front-end, talking to the Unix back-end. At that point, the MPS will create the new user accounts, but because almost none of the servers and applications using the data from those user accounts have yet been converted, we need to be able to provide them with the information they are expecting. In this case, some of the applications expect a user account to contain a password encrypted with the Unix crypt() system call. Thanks to the beauty of open source, the source code for that system call is widely available, and I was quickly able to find some source I could work with and port to C# (which we are using to create our custom MPF providers with).

Using the code

Using the code is very straightforward. Either put the class in your own application, or put it in a class library. There are two calls:

string Crypt(string textToEncrypt);

and:

string Crypt(string encryptionSalt, string textToEncrypt);

Both are static members. If you use the first method, it will randomly generate the salt characters for you and then call the second method. Salt, in this case, are two letters that are used to provide some extra randomness for the encryption, and allow you to repeat any previous encryption (if you also have the text that was encrypted).

Another thing users should know is that the text to encrypt should not be more then 8 characters long. If your text is longer than 8 characters, only the first 8 are used and the rest is ignored.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here