【发布时间】:2016-09-30 16:11:28
【问题描述】:
我想知道是否可以使用 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。我想创建一个类似于此的密钥,在此处以十六进制表示:1616161616161616 1010101010101010
【问题讨论】:
-
我在我的答案中添加了一个工作代码示例。如果它不适用于您的库/设备,那么我相信您需要联系库/设备供应商。 BTW DES3 密钥总是 24 字节长。如果您只有 16 个字节,那么我相信您应该创建 DES2 密钥。
标签: c# pkcs#11 pkcs11interop