如何在OpenJDK中使用ECC
曾經(jīng)試圖在Java和OpenJDK中使用橢圓曲線密碼術(shù) (ECC)的每個(gè)人要么被迫使用Bouncy Castle,要么被SunEC提供者弄糊涂了 。 SunEC提供程序根據(jù)文檔 (報(bào)價(jià))提供以下算法:
| AlgorithmParameters | 歐共體 |
| KeyAgreement | ECDH |
| KeyFactory | 歐共體 |
| KeyPairGenerator | 歐共體 |
| Signature | ECDSA沒有 SHA1withECDSA SHA256withECDSA SHA3??84withECDSA SHA512withECDSA |
不幸的是,OpenJDK并未附帶該提供程序。 但是任何真正想嘗試Java內(nèi)置ECC功能的人都可能會(huì)嘗試將sunec.jar(包含提供程序)簡單地添加到j(luò)re / lib / ext /文件夾中。 但是,當(dāng)嘗試使用提供程序時(shí),這些家伙一定會(huì)驚訝地擦著眼睛。 事情與剛開始時(shí)看起來不一樣...
假設(shè)我們將庫添加到了正確的文件夾中,我們的OpenJDK注意到了它,并且我們可以成功地編譯以下代碼而沒有任何例外:
package eccprovidertest;import java.security.Provider; import java.security.Provider.Service; import java.security.Security; import sun.security.ec.SunEC;/*** ECC Provider Test.* @author Christopher Meyer - christopher.meyer@rub.de* @version 0.1* Oct 23, 2013*/ public class ECCProviderTest {/*** @param args the command line arguments*/public static void main(final String[] args) {Provider sunEC = new SunEC();Security.addProvider(sunEC);for(Service service : sunEC.getServices()) {System.out.println(service.getType() + ": " + service.getAlgorithm());}}}如果最終使用OpenJDK(Java版本“ 1.7.0_25”)運(yùn)行它,則會(huì)得到以下輸出:
KeyFactory: EC AlgorithmParameters: EC哇! 這不是一個(gè)非常有用的提供程序.....承諾的算法在哪里? 讓我們嘗試使用Oracle JDK來運(yùn)行代碼,只是為了好玩:
KeyFactory: EC AlgorithmParameters: EC Signature: NONEwithECDSA Signature: SHA1withECDSA Signature: SHA256withECDSA Signature: SHA384withECDSA Signature: SHA512withECDSA KeyPairGenerator: EC KeyAgreement: ECDH驚喜,驚喜! 那是您開始揉眼睛的時(shí)刻! 這里是算法,但是為什么僅在使用Oracle JDK時(shí)才可用?
這樣做的原因隱藏在提供程序的代碼中。 以下幾行摘自sun.security.ec.SunEC :
private static final long serialVersionUID = -2279741672933606418L;// flag indicating whether the full EC implementation is present // (when native library is absent then fewer EC algorithms are available) private static boolean useFullImplementation = true; static {try {AccessController.doPrivileged(new PrivilegedAction() {public Void run() {System.loadLibrary("sunec"); // check for native libraryreturn null;}});} catch (UnsatisfiedLinkError e) {useFullImplementation = false;} }public SunEC() {super("SunEC", 1.7d, "Sun Elliptic Curve provider (EC, ECDSA, ECDH)");// if there is no security manager installed, put directly into// the provider. Otherwise, create a temporary map and use a// doPrivileged() call at the end to transfer the contentsif (System.getSecurityManager() == null) {SunECEntries.putEntries(this, useFullImplementation);} else {Map<Object, Object> map = new HashMap<Object, Object>();SunECEntries.putEntries(map, useFullImplementation);AccessController.doPrivileged(new PutAllAction(this, map));} }此外,在將某些條目添加到列表之后,可以在SunECEntries類中找到以下內(nèi)容:
/** Register the algorithms below only when the full ECC implementation* is available*/ if (!useFullImplementation) {return; }好的,這說明了行為。 僅當(dāng)可以成功加載本機(jī)庫(Windows計(jì)算機(jī)上的libsunec.so或sunec.dll)時(shí),才存在算法。 在我們的情況下,顯然缺少該庫(因?yàn)槲覀儍H復(fù)制了sunec.jar文件)。
不幸的是,如果我們閱讀了提供者文檔,我們將了解以下內(nèi)容:
“ […] Java類打包到JRE擴(kuò)展目錄中已簽名的sunec.jar中,而C ++和C函數(shù)打包到JRE本機(jī)庫目錄中的libsunec.so或sunec.dll中。 如果不存在本機(jī)庫,則該提供者已注冊為支持較少的ECC算法(省略了KeyPairGenerator,Signature和KeyAgreement)。”
不幸的是,這是我們自己急于采取的行動(dòng),這浪費(fèi)了我們寶貴的開發(fā)時(shí)間。 摘自:有時(shí)候閱讀JavaDocs確實(shí)很有幫助……。 (但不像調(diào)試工作那樣富有教育意義)。
翻譯自: https://www.javacodegeeks.com/2013/10/how-to-use-ecc-with-openjdk.html
總結(jié)
以上是生活随笔為你收集整理的如何在OpenJDK中使用ECC的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 优酷与 BBC Studios 合作,将
- 下一篇: 向SAML响应中添加自定义声明–(如何为