Encryption/Decryption doesnt look locally always for key file

Topics: Cryptography Application Block
Feb 22, 2007 at 3:47 AM
This is in 2.0.

When I do a unencrypt or a encrypt of a string file I use the following line.

Cryptographer.EncryptSymmetric(symmProvider, passwordTextBox.Text);

symmProvider = IslandBackupProvider

My config file looks like:

<add algorithmType="System.Security.Cryptography.RijndaelManaged, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"
protectedKeyFilename="IslandBackup.key" protectedKeyProtectionScope="LocalMachine"
type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=, Culture=neutral, PublicKeyToken=null"
name="IslandBackupProvider" />

They key file is named IslandBackup.key.

It is located in the same directory.

About 5% of the time the encryption/decryption will fail and it will say "Could not find file "c:\Documents and Settings\Administrator\My Documents\IslandBackup.key"

The key file is always in the same folder as the assembly which is not in My Documents.

This is on a XP or 2003 machine.

Any idea why this would happen?
Feb 22, 2007 at 5:23 PM
The protectedKeyFilename path can be an absolute or relative path to the key.

Above you are using a relative path of "IslandBackup.key", which means the Configuration Routines will look for the key relative to the root application directory.

Hence if you are running your executable from the My Documents Folder ( the root directory of the application in this case ) EntLib will try to load the key from the same location if you just provide a filename as above. The location of the assemblies that use the cryptography routines have no bearing on the location of the key.

Basically, relative paths always start at the main application directory.

My guess is that you sometimes run the application from the My Documents Folder ( or use it as the starting directory ), and you don't put the IslandBackup.key file in there.

If that isn't the case, I am not sure what the problem would be and I recommend creating an issue for it so someone can look into it a bit further.




David Hayden
Microsoft MVP C#
Feb 22, 2007 at 7:38 PM
The application is never run from My documents, it is ran from Program files when it is installed and the key is always generated on install. I verify that. In fact if I close the app and bring it back up 98% of the time it wont do it again. It's really weird.

Now if I put a absolute path in "C:\IslandBackup.key" it works 100% of the time. I tried to look through the application blocks but I am pretty sure that the code that loads the key file is not looking in my documents at all. It is really strange.

Thanks for the suggestions though David.