Hello,
first of all: thanks for this _great_ book! I learned a lot from it!
I am currently trying to interact with an Aladdin eToken via PKCS11.
The PKCS11 provider is setup and I can read the 3 stored certificates and the key.
Setup file contains:
name = eTokenPKCS11
library = C:/windows/system32/eTPKCS11.dll
description = Aladdin eToken Pro PKCS11 Provider
Code:
char[] pin = "mypassword".toCharArray();
KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null,pin);
System.out.println("Token contains " + ks.size() + " entries.");
while (enumerator.hasMoreElements()) {
String alias = enumerator.nextElement();
Key privKey = ks.getKey(alias, null);
...
privKey.getAlgorithm() returns "RSA". privKey.getEncoded() returns null.
privKey is defined as
"SunPKCS11-eTokenPKCS11 RSA private key, 2048 bits (id 43450373, token object, sensitive, unextractable)" in Eclipse's variable inspector (it is unextractable because I cannot get it from the token of course)
---
Now I want to perform some tests:
Code:
Cipher cipher = Cipher.getInstance("RSA/None/NoPadding","BC");
cipher.init(Cipher.ENCRYPT_MODE, c1.getPublicKey()); // c1 = Certificate for Key "privKey"
byte[] output = cipher.doFinal(input);
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] plain = cipher.doFinal(output);
The code breaks at "cipher.init(DECRYPT_MODE, privKey") with "unknown key type passed to RSA".
I've alreasy tried to cast it to "RSAPrivateKey" but the cast was "invalid"..
---
Then I tried to sign something:
Code:
Signature sig = Signature.getInstance("DSA", "BC");
sig.initSign((PrivateKey) privKey);
sig.update(input);
Again I get "Can't identify DSA private key"...
What am I doing wrong?
How do I use my token for encryption/decryption, signing of data?
After all: Do I actually use "getInstance("XYZ/....")" with the provider BC?? I mean: The cryptographic operation is performed by the token not the BC code, isn't it? But "getInstance("XYZ/...", "PKCS11")" failes with "No such provider"
Thanks for your help!