perhaps someone need to use usb-key or other hardware token to generate digital signature , through microsoft cryptoapi. perhaps ms cryptoapi is the only way for us to access cryptography device such as usb-key. it is sure not a comfortable way because java developers have to call capi funtions throught jni(java native interface). so there are some java-library to call cryptoapi, but they are not free.
i hope to provide an opensource java library to do this thing : securex[https://sourceforge.net/projects/securex]
here is some demo of what securex library could do:
1, securex library arichtecture demo
2, securex usb-key demo
ok, came back to our topic, how to use java call cryptoapi to produce signature.
you should know at least :
1, cryptoapi are just a set of interface define by ms, and usb-key vendor just implement these interface so that our application can call the usb key to do some cryptographic operations(eg signature, hash, encryption). there are a lot of csps located in your windows system. csp is implementation, but we need not care about it, all we care is what cryptoapi could do. see msdn for more information.
2, for java developer, they should use jni to access cryptoapi but it is not an easy thing since there are some encoding difference between and windows. for example, they should know how to convert the binary private key stream to java privatekey object.
3, perhaps some usb-key vendor provide pkcs#11 csp other than cryptoapi csp. pkcs# csp is a rsa standard [], it will be a good optional implement instead of cryptoapi csp.
back to cryptoapi csp:
java developer should do such a thing to generate a signature:
byte
[] data
=
"
http://openssl.blogjava.net
"
.getbytes();
signatureutils sigutil
=
new
signatureutils(
"
md5
"
);
sigutil.initsign(privatekey);
sigutil.update(data,
0
,data.length);
byte
[] signature
=
sigutil.sign();
sigutil.initverify(publickey);
sigutil.update(data,
0
,data.length);
if
(
!
sigutil.verify(signature))
system.out.println(
"
the signature verification failed.
"
);
else
system.out.println(
"
the signature was successfully verified.
"
);
before we sign, we should provide a privatekey, in the java world, private key is stored in jks file(java keystore), we could get the keyentry out through:
keystorestream
=
new
fileinputstream(keystorefilename);
keystore
=
keystore.getinstance(keystore.getdefaulttype());
keystore.load(keystorestream, keystorepassword.tochararray());
key key
=
keystore.getkey(alias, keypassword);
if
(key
instanceof
privatekey)
return
(privatekey)key;
but on windows, private key is not stored in jks, they stored in windows local certstore or in usb-key,and in most cases, private key are not allowed to export!
in cryptoapi's world, you should do the following things.
call cryptacquirecontext get hcryptprov handle, also known as a csp handle
call cryptcreatehash to hash your data
call cryptsignhash to sign
cryptacquirecontext need key container name 、csp name、csp type、dwflags。
cryptcreatehash need hash agorithm
cryptsignhash need dwkeyspec,right! it's the private key spec.
so, java developer feel boring when he need to provide the private key.
the proper way is:
1, use alias to get the privatekey:
(a) if the private key is exportable, we can get it and change it to a java object
(b) if the private key is not exportable, we get the private key handle.
2, sign the hash
(a) if the private key is exportable, sigutil.initsign(privatekey) would do this job.
(b) if the private key is not exportable, we pass the private key handle to csp,
and let csp get the privatekey internally.
the program below is running under securex, and it can get the privatekey from
usb-key, and sign the data:
/** *//**
* 签名,并将xml签名结果保存到signaturefile中
* 需要3个条件
* 1,keyalias,用于获取私钥
* 2,source_filename_to_be_signed,确定被签名的数据源
* 3,签名算法
* @param save_signaturefile
*/
public static void sign()
{
byte abyte0[][];
if((abyte0 = corkeystorejni.getkey(getkeyalias())) == null)
{
system.err.println("这是一条不可导出的钥匙!");
return;
}
if(abyte0.length == 0)
{
jcapirsaprivatekey jcapikey =new jcapirsaprivatekey(getkeyalias().getbytes());
system.out.println(jcapikey.getalgorithm()":"jcapikey.getprivateexponent());
/** *//**
* 签名数据
*/
file sourcefile=new file(source_filename_to_be_signed);
byte[] data =null;
try {
data= fileutils.getbytesfromfile(sourcefile);
} catch (ioexception e1) {
e1.printstacktrace();
}
byte[] signature=null;
/** *//**
* 产生签名
* todo, 使用正确的签名算法,比如md5withrsa->md5
*/
signatureutils sigutil=new signatureutils(getsignagorithm());
try {
sigutil.initsign(jcapikey);
sigutil.update(data,0,data.length);
signature = sigutil.sign();
system.out.println("signature>>>.."new string(signature));
setrawsignature(signature);
} catch (invalidkeyexception e) {
e.printstacktrace();
} catch (signatureexception e) {
e.printstacktrace();
}
/** *//**
* 保存签名到save_signaturefile
* 也就是sign的参数
*/
try {
fileutils.writefile(getsavesignaturefile(),signature);
} catch (ioexception e) {
e.printstacktrace();
}
}
} how's it done?
1) well, suppose i use an usb e-key(csp vendor
:吉大正元)
i know my privatekey alias is , locate in "my"
i get the private key through corekeystorejni class which has native jni method(
getkey), by which i could
tell the csp which private key i want to use!
2) i get the file to bytearray which must be hash before sign, because for microsoft capi, its signobject can accept hash object only.
signatureutils sigutil=new signatureutils(getsignagorithm());
the getsignagorithm return "md5withrsa" "sha1withrsa" for most cases. it meas md5 data before rsa sign :)
3) when the
sigutil.sign(); is excute, a native pin-callback windows is open, it will ask you for private key protected password(you can see it in my swf demo above).
type the correct pin , my jit csp will call the correct sign cryptographic operations through the usb ekey drivers.
all the cryptographic operations(hash, sign) are performed on the
usb-key internally,
not by the key drivers.
ok,then csp get the signature and signature verify use only the public key and need not access the private key, it can perform by csp or verify using jce, as you like.
4) haha, i've not yet told you that you need a dll to load before you call these api. wait till i put my whole project to sourceforege securex(). any advice, please contact me on this blog or just join the qq group: 14966586