通过IAIK PKCS#11包装器生成AES密钥并显示其值

时间:2014-01-29 13:51:18

标签: java aes wrapper pkcs#11 iaik-jce

我想使用PKCS#11兼容的加密USB令牌生成AES密钥并在屏幕上显示其值。

为此,我想使用IAIK PKCS#11包装器。

我试图通过IAIK包提供的示例生成密钥,但没有成功。生成密钥但我看不到密钥的任何值。我该怎么做才能看到关键值以便在屏幕上显示它?

这是我的代码:

Module pkcs11Module = null;
pkcs11Module = Module.getInstance("pkcs11.dll");

Session session = null;
pkcs11Module.initialize(null);

Slot[] slots = pkcs11Module.getSlotList(Module.SlotRequirement.TOKEN_PRESENT);

if (slots.length == 0) {
    output_.println("No slot with present token found!");
    throw new TokenException("No token found!");
}

Slot selectedSlot;
// slot 0
selectedSlot = slots[0];

Token token = selectedSlot.getToken();

session = token.openSession(Token.SessionType.SERIAL_SESSION, Token.SessionReadWriteBehavior.RW_SESSION, null, null);

session.login(Session.UserType.USER, "12345678".toCharArray());

Mechanism keyGenerationMechanism = Mechanism.get(PKCS11Constants.CKM_AES_KEY_GEN);

AESSecretKey aesKey = new AESSecretKey();
aesKey.getValueLen().setLongValue(new Long(32));

AESSecretKey aesKeyNew = (AESSecretKey) session.generateKey(keyGenerationMechanism, aesKey);
output_.println("the AES Key is: ");
output_.println(aesKeyNew.toString());

session.closeSession();
pkcs11Module.finalize(null);

结果如下:

the AES Key is: 
  Object Class: Secret Key
  Token: false
  Private: false
  Modifiable: true
  Label: <NULL_PTR>
  Key Type: AES
  ID: <NULL_PTR>
  Start Date: <NULL_PTR>
  End Date: <NULL_PTR>
  Derive: true
  Local: true
  Key Generation Mechanism: CKM_AES_KEY_GEN
  Allowed Mechanisms: <Attribute not present>
  Sensitive: false
  Encrypt: true
  Decrypt: true
  Sign: false
  Verify: false
  Wrap: true
  Unwrap: true
  Extractable: true
  Always Sensitive: false
  Never Extractable: true
  Check Value: <Attribute not present>
  Wrap With Trusted: <Attribute not present>
  Trusted: <Attribute not present>
  Wrap Template: <Attribute not present>
  Unwrap Template: <Attribute not present>
  Value (hex): <NULL_PTR>
  Value Length (dec): 0

有值(十六进制):我想在屏幕上看到并显示。 它是关于加密令牌的特定配置吗?当我使用不同的令牌时,我会看到这个值。

1 个答案:

答案 0 :(得分:0)

根据您向我们展示的PKCS#11属性CKA_SENSITIVE设置为false表示您应该能够查看该值。但是,令牌可能不允许您提取值。大多数这类令牌都没有完全实现PKCS#11,只允许进行某些操作。如果这是真的那么应该不能将CKA_SENSITIVE设置为false,但那对你来说就是IT ......

我会跟踪令牌的制造商(或者,如果可能的话,开发人员)并询问令牌实现的特定功能。

相关问题