我想知道是否可以使用pkcs11interop创建3DES密钥并指定用于创建的密钥值,否则创建密钥并输出生成的密钥核心价值。基本上我需要将密钥导出到其他设备。使用pkcs11Interop创建3DES密钥并输出密钥值或创建密钥值用于创建
我试过使用CKA_VALUE属性并将密钥作为byte []数组传递,但没有成功。
请问这样的事情吗?有人可以帮我吗?
编辑:
这里是我有没有运气迄今为止代码:
public ObjectHandle generate3DESKey(string keyLabel)
{
ObjectHandle key = null;
// Generate symetric key
// Prepare attribute template of new key
List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>();
objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_DES3));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_ENCRYPT, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_EXTRACTABLE, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, keyLabel));
// Specify key generation mechanism
Mechanism mechanism = new Mechanism(CKM.CKM_DES3_KEY_GEN);
// Generate key
key = _session.GenerateKey(mechanism, objectAttributes);
List<CKA> retrieveTemplate = new List<CKA>();
retrieveTemplate.Add(CKA.CKA_VALUE);
var test = _session.GetAttributeValue(key, retrieveTemplate);
var testval = test[0].GetValueAsString();
return key;
}
所以我尝试使用此代码是创建一个3DES密钥,然后把它的值用GetAttributeValue如下所示。我已经尝试了GetValueAsByteArray和GetValueAsString,但都没有成功。我注意到,即使我在创建时设置了可抽取属性,检索属性上的cannotread属性也设置为true。
除此之外,我还考虑在生成3DES关键字时传递关键值,但令我困惑的是文档说与CKA.CKA_VALUE一起使用的关键值应该是长度为24的字节数组。在我的情况下,我需要创建密钥是16的长度长,没有24.我想创建一个键与此类似,其在Hex在这里表示:1616161616161616 1010101010101010
我加入了一个工作代码示例我的答案。如果它不适用于您的库/设备,那么我相信您需要联系库/设备供应商。 BTW DES3密钥总是24个字节长。如果你只有16个字节,那么我相信你应该创建DES2密钥。 – jariq