title: CreateObject PKCS#11 fail
body: <p>I tried to comment on this link <a href="https://stackoverflow.com/a/39798597/448266">https://stackoverflow.com/a/39798597/448266</a>, but could not due to the reputation #.</p>
  
  <p>I have tried the sample and run well, but when I changed to arbitrary value it returns exception <strong><em>Message: Net.Pkcs11Interop.Common.Pkcs11Exception : Method C_CreateObject returned 2147483968</em></strong></p>
  
  <p>I am using safenet HSM SW.</p>
  
  <pre><code>plainKeyValue = Common.HelperFunctions.StringToByteArray("112233445566778899001122334455665566998844335511");
  </code></pre>
  
  <p>Below is the snapshot of the code, I changed slightly on the key value (as above).</p>
  
  <pre><code> public static string generateAndCreateKeyObj()
      {
          using (IPkcs11 pkcs11 = Settings.Factories.Pkcs11Factory.CreatePkcs11(Settings.Factories, Configurations.Pkcs11LibraryPath, Settings.AppType))
          {
              // Find first slot with token present
              ISlot slot = Helpers.GetUsableSlot(pkcs11, Configurations.default_slot);
  
              // Open RW session
              using (Net.Pkcs11Interop.HighLevelAPI.ISession session = slot.OpenSession(SessionType.ReadWrite))
              {
                  // Login as normal user
                  session.Login(Configurations.user_type, "1234");
  
                  // Prepare attribute template of new key
                  List&lt;IObjectAttribute&gt; objectAttributes = new List&lt;IObjectAttribute&gt;();
                  objectAttributes.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_TOKEN, false)); //not stored in token
                  objectAttributes.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY));
                  objectAttributes.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_DES3));
                  objectAttributes.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_ENCRYPT, true));
                  objectAttributes.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_DECRYPT, true));
                  objectAttributes.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_WRAP, true));
                  objectAttributes.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_UNWRAP, true));
                  objectAttributes.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_EXTRACTABLE, true));
  
                  // Specify key generation mechanism
                  IMechanism mechanism = Settings.Factories.MechanismFactory.CreateMechanism(CKM.CKM_DES3_KEY_GEN);
  
                  // Generate key
                  IObjectHandle secret_key = session.GenerateKey(mechanism, objectAttributes);
  
  
                  ////////////////////////////////////////////////////////////////////////////////////////
  
                  // Export the key
                  byte[] plainKeyValue = null;
                  List&lt;IObjectAttribute&gt; readAttrs = session.GetAttributeValue(secret_key, new List&lt;CKA&gt;() { CKA.CKA_VALUE });
                  if (readAttrs[0].CannotBeRead)
                      throw new Exception("Key cannot be exported");
                  else
                      plainKeyValue = readAttrs[0].GetValueAsByteArray();
  
                  plainKeyValue = Common.HelperFunctions.StringToByteArray("112233445566778899001122334455665566998844335511");
  
                  // Prepare attribute template of new key
                  List&lt;IObjectAttribute&gt; oa = new List&lt;IObjectAttribute&gt;();
                  oa.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_LABEL, "Imported key"));
                  oa.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY));
                  oa.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_DES3));
                  oa.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_TOKEN, true));
                  oa.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_ENCRYPT, true));
                  oa.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_DECRYPT, true));
                  oa.Add(Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_VALUE, plainKeyValue));
  
  
                  IObjectHandle importedKey = session.CreateObject(oa);
  
  
                  // Test encryption with generated key and decryption with imported key
                  using (IMechanism mechanismx = Settings.Factories.MechanismFactory.CreateMechanism(CKM.CKM_DES3_CBC, session.GenerateRandom(8)))
                  {
                      byte[] sourceData = ConvertUtils.Utf8StringToBytes("Our new password");
                      byte[] encryptedData = session.Encrypt(mechanismx, secret_key, sourceData);
                      byte[] decryptedData = session.Decrypt(mechanismx, importedKey, encryptedData);
                      if (Convert.ToBase64String(sourceData) != Convert.ToBase64String(decryptedData))
                          throw new Exception("Encryption test failed");
                  }
                  // Destroy object
                  session.DestroyObject(importedKey);
                  session.DestroyObject(secret_key);
                  session.Logout();
  
                  return HelperFunctions.ByteArrayToString(plainKeyValue);
              }
          }
      }
  
   // convert from string to array
      public static byte[] StringToByteArray(string hex)
      {
          byte[] result;
          try
          {
              result = Enumerable.Range(0, hex.Length)
                           .Where(x =&gt; x % 2 == 0)
                           .Select(x =&gt; Convert.ToByte(hex.Substring(x, 2), 16))
                           .ToArray();
  
              return result;
          }
          catch (Exception e)
          {
              throw new Exception(e.Message); ;
          }
      }
  </code></pre>