Problems using NCryptoki from ASP.NET MVC site

7/20/2011 6:29:25 PM
Gravatar
Total Posts 2

Problems using NCryptoki from ASP.NET MVC site

Hi Ugo,

 

I'm putting together a test site using NCryptoki (trial edition) to talk to a Safenet Luna HSM.  I have created a class library to wrap the NCryptoki functionality that only exposes a couple of functions.

using System;
namespace CryptoTest.Library.Interfaces
{
  public interface ICryptoLibrary
  {
    string EncryptString(string stringToEncrypt);
    string UnencryptString(string stringToUnencrypt);
  }
}

The first couple of lines of the encrypt function are:

public string EncryptString(string stringToEncrypt)
{
  // Initialise Cryptoki library
  Cryptoki cryptoki = new Cryptoki("C:\\Program Files\\LunaSA\\cryptoki.dll");
  cryptoki.Initialize();

  ....

Using this library in a console or windows app is fine.  After dismissing the trial licence warning the application functions as expected.

However, in my asp.net app after dismissing the warning dialog a further dialog is presented with the warning:

The specified Cryproki library is not found. Please check the path.

The web app is .NET4 ASP.NET MVC3 C# running in IIS Express on a Win7 x64 Enteprise machine.  I have tried compiling the app for Any CPU, x86, x64 all to no avail.  I have added the runtime startup element to the web.config but I can't find the solution to getting it working.

 

Any thoughts would be appreciated.

7/20/2011 7:39:50 PM
Gravatar
Total Posts 300
Ugo Chirico http://www.ugochirico.com

Re: Problems using NCryptoki from ASP.NET MVC site

Hi,

Many customers are using NCryptoki in ASP.NET/MVC apps without any problem.
First of all make sure that NCryptokiMngd.dll (64bit) is in the bin folder.

The error "The specified Cryproki library is not found. Please check the path." appears when the PKCS#11 dll cannot be loaded.
Because you are in an ASP.NET / MVC app, maybe IIS Express and you app don't have the right to read from that path. Check it.
Then, are you sure that IISExpress is a 64bit executable?

 

7/21/2011 1:19:06 PM
Gravatar
Total Posts 2

Re: Problems using NCryptoki from ASP.NET MVC site

Hi Ugo,

No, as far as I understand it IIS Express is a 32bit application. If I set the platform target of the web application to x64 I get


System.BadImageFormatException: Could not load file or assembly 'CryptoTest.Web' or one of its dependencies. An attempt was made to load a program with an incorrect format

So it looks like I'm stuck until I get permission to purchase a full licence that will work under IIS 7.5 with 64bit app pools.

 

A second question if I may? So far my test console and WinForms apps only work correctly if both of the NCryptoki assemblies are registered in the GAC. If they are not in the GAC and instead the application references the files to copy locally to the /bin then the application fails.  Is this correct?  Do I have to have the assemblies registered?

7/22/2011 12:11:56 AM
Gravatar
Total Posts 300
Ugo Chirico http://www.ugochirico.com

Re: Problems using NCryptoki from ASP.NET MVC site

Considering that IISExpress is 32bit, you should use the 32bit version of NCryptoki dlls and you should load the 32bit version of the Luna Cryptoki dll.

The problem you got is that you are using the 64bit versions of Luna Cryptoki instead (the path is C:\\Program Files\\LunaSA\\cryptoki.dll and Program Files on a 64bit OS contains all 64bit programs while Program Files (x86) contains all 32bit programs).

You cannot load in a 32bit process (IISExpress) a 64bit dll (Luna cryptoki.dll).

 

Regarding the GAC, it's not strictly necessary the GAC registration. You can simply copy NCryptoki.dll and NCryptokiMngd.dll in the working directory of your app or in the bin folder of your webapp or in system32

Regards,

Ugo Chirico