<link>http://ncryptoki.com/forum.aspx</link> <description /> <docs>http://www.rssboard.org/rss-specification</docs> <generator>mojoPortal Forum module</generator> <item> <title>Re: Session.encrypt CKR_OPERATION_NOT_INITIALIZED This is a test call and I execute it using a timer, calling it every 15 seconds. The timer is the only caller of Crypto.

Can be a multithreding issue? Or something else?

]]>
http://ncryptoki.com/Forums/Thread.aspx?pageid=9&t=323~-1#post1146 p13r0 http://ncryptoki.com/Forums/Thread.aspx?pageid= Wed, 22 Nov 2017 07:19:29 GMT Re: Session.encrypt CKR_OPERATION_NOT_INITIALIZED Are you callling it in multithreading?

If so, because you are calling initialize and finalize in the same call to "Crypto" function, you should make "Crypto" function as an atomic function by inserting something like a lock or a semaphore

 

]]>
http://ncryptoki.com/Forums/Thread.aspx?pageid=9&t=323~-1#post1144 p13r0 http://ncryptoki.com/Forums/Thread.aspx?pageid= Tue, 21 Nov 2017 17:25:30 GMT
Session.encrypt CKR_OPERATION_NOT_INITIALIZED Why is this code giving me, sometimes, the error 145? I'm trying to avoid it, but I am still missing something...

 

 

static public byte[] Crypto(Key key, byte[] input, bool encrypt, Mechanism mech, string command)
{
    //Session session = openSession();
    var tupla = openSessionTupla();
    var session = tupla.Item1;
    try
    {
                
        Utility.Logger("Crypto encrypt " + encrypt.ToSafeString() + " mech " + mech.ToSafeString(), command);

        if (encrypt)
        {
            session.EncryptInit(mech, key);
            byte[] enc = session.Encrypt(input);
            session.EncryptFinal();
            session.Logout();
            session.Close();
            tupla.Item2.Finalize(IntPtr.Zero);
            return enc;
        }
        else
        {
            session.DecryptInit(mech, key);
            byte[] decriptata = session.Decrypt(input);
            session.DecryptFinal();
            session.Logout();
            session.Close();
            tupla.Item2.Finalize(IntPtr.Zero);
            return decriptata;
        }
    }
    catch (Exception e)
    {
        session.Logout();
        session.Close();
        tupla.Item2.Finalize(IntPtr.Zero);
        Utility.Logger("Crypto " + e.ToSafeString(), command);
        return null;
    }
            
}

 

Where openSessionTupla is

 

public static Tuple<Session, Cryptoki> openSessionTupla()
{
    Cryptoki.Licensee = Settings.LICENSEE;
    Cryptoki.ProductKey = Settings.PRODUCTKEY;
    Cryptoki cryptoki = new Cryptoki(Settings.PATH);
    //Console.WriteLine(Settings.PATH);
    //Console.WriteLine(Settings.SessionKey);
    cryptoki.Initialize();
    SlotList slots = cryptoki.Slots;
    if (slots.Count == 0)
    {
        //Console.WriteLine("No slot available");
        return null;
    }
    // Gets the first slot available
    Slot slot = slots[0];
    if (!slot.IsTokenPresent)
    {
        //Console.WriteLine("No token inserted in the slot: " + slots[0].Info.Description);
        return null;
    }
    Token token = slot.Token;
    var flags = token.Info.Flags;
    //token.Info.Flags = 1609;
    Session session = token.OpenSession(Session.CKF_SERIAL_SESSION | Session.CKF_RW_SESSION,
                            null,
                            null);

    int nRes = session.Login(Session.CKU_USER, Settings.SessionKey);
            
    return new Tuple<Session, Cryptoki>(session, cryptoki);
}

]]>
http://ncryptoki.com/Forums/Thread.aspx?pageid=9&t=323~-1#post1136 p13r0 http://ncryptoki.com/Forums/Thread.aspx?pageid= Mon, 20 Nov 2017 09:24:56 GMT