简体   繁体   English

Java安全性 - MSCAPI提供者:如何在没有密码弹出的情况下使用?

[英]Java security - MSCAPI provider: How to use without password popup?

I've managed to use Sun's MSCAPI provider in my application. 我已经设法在我的应用程序中使用Sun的MSCAPI提供程序。 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. 这是一个问题,因为我需要在Web服务中使用加密功能。

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? 我该如何预防呢?

The MSCAPI provider does not support providing the password to CAPI: MSCAPI提供程序不支持向CAPI提供密码:

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) (1)

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. 要通过CAPI设置密码,必须使用未记录的KP_KEYEXCHANGE_PIN或KP_SIGNATURE_PIN调用CryptSetKeyParam ,并希望您的底层硬件令牌提供程序支持它。 (They are not completely undocumented - the documentation for Windows CE and Windows Mobile mention them (2) and they are included in the header files). (它们没有完全没有记录 - Windows CE和Windows Mobile的文档提到它们(2)并且它们包含在头文件中)。

My guess is that Windows is popping up the pop up. 我的猜测是Windows正在弹出弹出窗口。

Import your key again using the Certificate Import Wizard, but make sure that you don't check the following option on the "Password" screen. 使用“证书导入向导”再次导入密钥,但请确保不要在“密码”屏幕上选中以下选项。

[_] Enable strong private key protection. [_]启用强大的私钥保护。 You will be prompted every time the private key is used by an application if you enable this option. 如果启用此选项,则每次应用程序使用私钥时都会提示您。

I resolved this problem setting the provider as follow: 我解决了这个问题,设置提供者如下:

signeData = gen.generate(content, ks.getProvider());

Where 哪里

ks is a KeyStore and ksKeyStore

gen is a CMSSignedDataGenerator genCMSSignedDataGenerator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM