Java 安全 - MSCAPI 提供程序:如何在不弹出密码的情况下使用?

2022-01-10 00:00:00 cryptography security java cryptoapi

我已经设法使用 Sun 的 MSCAPI提供者 在我的应用程序中.我现在遇到的问题是它总是弹出一个窗口,要求输入密码,即使我已经在代码中提供了密码.这是个问题,因为我需要 web 服务中的加密功能.

I've managed to use Sun's MSCAPI provider in my application. The problem I'm having now is that it always pops up a window, asking for a password, even though I've provided it in the code. This is a problem, because I need the cryptography functionality in a webservice.

这是我现在拥有的代码:

Here's the code I have now:

String alias = "Alias to my PK";
char[] pass = "MyPassword".toCharArray();

KeyStore ks = KeyStore.getInstance("Windows-MY");
ks.load(null, pass);
Provider p =  ks.getProvider();

Signature sig = Signature.getInstance("SHA1withRSA",p);
PrivateKey key = (PrivateKey) ks.getKey(alias, pass)

sig.initSign(key);
sig.update("Testing".getBytes());
sig.sign();

这很好用,但是当最后一行运行时,我会弹出一个询问密码的窗口.我该如何预防?

This is working great, but I get a popup asking for the password when the last line is run. How do I prevent that?

推荐答案

MSCAPI提供者不支持向CAPI提供密码:

The MSCAPI provider does not support providing the password to CAPI:

假定必须提供密码的应用程序支持兼容模式.它允许(但忽略)非空密码.该模式默认启用.(1)

A compatibility mode is supported for applications that assume a password must be supplied. It permits (but ignores) a non-null password. The mode is enabled by default. (1)

要通过CAPI设置密码,必须调用CryptSetKeyParam 使用未记录的 KP_KEYEXCHANGE_PIN 或 KP_SIGNATURE_PIN 并希望您的底层硬件令牌提供商支持它.(它们并非完全没有文档 - Windows CE 和 Windows Mobile 的文档提到了它们 (2) 并且它们包含在头文件中).

To set the password through CAPI, you must call CryptSetKeyParam with the undocumented KP_KEYEXCHANGE_PIN or KP_SIGNATURE_PIN and hope your underlying hardware token provider supports it. (They are not completely undocumented - the documentation for Windows CE and Windows Mobile mention them (2) and they are included in the header files).

相关文章