title: Pkcs11Interop read key value from HSM
body: <p>I'm trying to use Pkcs11Interop to extract the value of the key from the HSM. I know, the key has to stay in the HSM, but I need it, so...</p>
  
  <p>I already do it with NCryptoki and I'd like to do it also with Pkcs11Interop</p>
  
  <p>I tried this code:</p>
  
  <pre><code>// Prepare attribute template that defines search criteria
  List&lt;ObjectAttribute&gt; objectAttributes = new List&lt;ObjectAttribute&gt;();
  objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY));
  objectAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_DES));
  objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "MY_KEY"));
  
  // Find all objects that match provided attributes
  List&lt;ObjectHandle&gt; foundObjects = session.FindAllObjects(objectAttributes);
  
  var key = foundObjects[0];
   byte[] plainKeyValue = null;
   List&lt;ObjectAttribute&gt; readAttrs = session.GetAttributeValue(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();
  </code></pre>
  
  <p>But the plainKeyValue is all zeros, but, as you can imagine, this is not true.</p>
  
  <p>SO, how can I reach my goal?</p>