<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Berthou.com &#187; jce</title>
	<atom:link href="https://www.berthou.com/us/tag/jce/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.berthou.com/us</link>
	<description>A french developper blog</description>
	<lastBuildDate>Mon, 09 Aug 2010 08:55:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>MS CAPI and Java (JCE SunMSCAPI)</title>
		<link>https://www.berthou.com/us/2007/12/05/ms-capi-and-java-jce-sunmscapi/</link>
		<comments>https://www.berthou.com/us/2007/12/05/ms-capi-and-java-jce-sunmscapi/#comments</comments>
		<pubDate>Wed, 05 Dec 2007 21:20:37 +0000</pubDate>
		<dc:creator>rberthou</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[certificat]]></category>
		<category><![CDATA[jce]]></category>

		<guid isPermaLink="false">http://www.berthou.com/us/2007/12/05/ms-capi-and-java-jce-sunmscapi/</guid>
		<description><![CDATA[Description This news is the following of &#8220;How to signing a PDF with Java&#8221; I continu my research to use certificate available in the MS Windows Keystrore.I wanted to use a certificate stored in a card (use for MS Windows signe on). Thanks to these researchs I discovered that with JDK 6 it is very [...]]]></description>
				<content:encoded><![CDATA[<h2 class="titre2">Description</h2>
<p>This news is the following of <a href="http://www.berthou.com/us/?p=18">&#8220;How to signing a PDF with Java&#8221;</a><br />
<br/><br />
I continu my research to use certificate available in the MS Windows Keystrore.I wanted to use a certificate stored in a card (use for MS Windows signe on).<br />
<br/><br />
Thanks to these researchs I discovered that with JDK 6 it is very simple to  use the <a href="http://en.wikipedia.org/wiki/Cryptographic_API" target="_blank"><b>MSCAPI</b></a>&#8216;s API .<br />
<span id="more-23"></span><br />
With Java SE 6 you have a large choice of tools and it is configurate with most of crypto &#8220;providers&#8221; as <b>SunMSCAPI Provider</b> (see $JRE_HOME/lib/security/java.security ).</p>
<pre class="alertCode">
#
# List of providers and their preference orders (see above):
#
security.provider.1=sun.security.provider.Sun
security.provider.2=sun.security.rsa.SunRsaSign
security.provider.3=com.sun.net.ssl.internal.ssl.Provider
security.provider.4=com.sun.crypto.provider.SunJCE
security.provider.5=sun.security.jgss.SunProvider
security.provider.6=com.sun.security.sasl.Provider
security.provider.7=org.jcp.xml.dsig.internal.dom.XMLDSigRI
security.provider.8=sun.security.smartcardio.SunPCSC
security.provider.9=sun.security.mscapi.SunMSCAPI
</pre>
<h2 class="titre2">Services supported by SunMSCAPI</h2>
<table class=tbl0>
<thead class="rh">
<tr>
<th>Type</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="r1">
<td>KeyPairGenerator</td>
<td>
<ul>
<li>RSA</li>
</ul>
</td>
<td>Generates RSA key pairs needed by other cryptographic services such as Signature and Cipher.</td>
</tr>
<tr class="r2">
<td>Signature</td>
<td>
<ul>
<li>SHA1withRSA</li>
<li>MD5withRSA</li>
<li>MD2withRSA</li>
</ul>
</td>
<td>Creates and validates signatures using various message digest and encryption algorithm as specified in the service name.</td>
</tr>
<tr class="r1">
<td>Cipher</td>
<td>
<ul>
<li>RSA</li>
<li>RSA/ECB/PKCS1Padding</li>
</ul>
</td>
<td>Performs RSA encryption and decryption.</td>
</tr>
<tr class="r2">
<td>KeyStore</td>
<td>
<ul>
<li>Windows-MY</li>
<li>Windows-ROOT</li>
</ul>
</td>
<td>Provides direct read-write access to MS Window&#8217;s keystores. The Windows-MY keystore contains the user&#8217;s private keys and the associated certificate chains. The Windows-ROOT keystore contains all root CA certificates trusted by the machine.</td>
</tr>
<tr class="r1">
<td>SecureRandom</td>
<td>
<ul>
<li>Windows-PRNG</li>
</ul>
</td>
<td>Generates random numbers for the random data that other cryptographic services need.</td>
</tr>
</tbody>
</table>
<p class="alertWarning">
Warning :By using this API I saw that the PIN&#8217;s code is asked even if you pass it in the code.  (I think it should be a bug in drivers card readers) .
</p>
<p><br/></p>
<h2 class="titre2">Exemple</h2>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">KeyStore</span> ks <span style="color: #339933;">=</span> <span style="color: #003399;">KeyStore</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Windows-ROOT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ks.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Enumeration</span> en <span style="color: #339933;">=</span> ks.<span style="color: #006633;">aliases</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>en.<span style="color: #006633;">hasMoreElements</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> aliasKey <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>en.<span style="color: #006633;">nextElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
		<span style="color: #003399;">Certificate</span> c <span style="color: #339933;">=</span> ks.<span style="color: #006633;">getCertificate</span><span style="color: #009900;">&#40;</span>aliasKey<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;---&gt; alias : &quot;</span> <span style="color: #339933;">+</span> sss<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;    Certificat : &quot;</span> <span style="color: #339933;">+</span> c.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aliasKey.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myKey&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		      <span style="color: #003399;">PrivateKey</span> key <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">PrivateKey</span><span style="color: #009900;">&#41;</span>ks.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span>aliasKey, <span style="color: #0000ff;">&quot;monPassword&quot;</span>.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		      <span style="color: #003399;">Certificate</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> chain <span style="color: #339933;">=</span> ks.<span style="color: #006633;">getCertificateChain</span><span style="color: #009900;">&#40;</span>aliasKey<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> ioe<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>ioe.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p class="alertLink">
<b>Voir aussi</b><br />
<a href="http://java.sun.com/developer/technicalArticles/J2SE/security/" target="_blank">http://java.sun.com/developer/technicalArticles/J2SE/security/</a><br />
<a href="http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html" target="_blank">http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html</a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.berthou.com/us/2007/12/05/ms-capi-and-java-jce-sunmscapi/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
