如何在OpenJDK中使用ECC
曾經試圖在Java和OpenJDK中使用橢圓曲線密碼術 (ECC)的每個人要么被迫使用Bouncy Castle,要么被SunEC提供者弄糊涂了 。 SunEC提供程序根據文檔 (報價)提供以下算法:
| AlgorithmParameters | 歐共體 |
| KeyAgreement | ECDH |
| KeyFactory | 歐共體 |
| KeyPairGenerator | 歐共體 |
| Signature | ECDSA沒有 SHA1withECDSA SHA256withECDSA SHA3??84withECDSA SHA512withECDSA |
不幸的是,OpenJDK并未附帶該提供程序。 但是任何真正想嘗試Java內置ECC功能的人都可能會嘗試將sunec.jar(包含提供程序)簡單地添加到jre / lib / ext /文件夾中。 但是,當嘗試使用提供程序時,這些家伙一定會驚訝地擦著眼睛。 事情與剛開始時看起來不一樣...
假設我們將庫添加到了正確的文件夾中,我們的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”)運行它,則會得到以下輸出:
KeyFactory: EC AlgorithmParameters: EC哇! 這不是一個非常有用的提供程序.....承諾的算法在哪里? 讓我們嘗試使用Oracle JDK來運行代碼,只是為了好玩:
KeyFactory: EC AlgorithmParameters: EC Signature: NONEwithECDSA Signature: SHA1withECDSA Signature: SHA256withECDSA Signature: SHA384withECDSA Signature: SHA512withECDSA KeyPairGenerator: EC KeyAgreement: ECDH驚喜,驚喜! 那是您開始揉眼睛的時刻! 這里是算法,但是為什么僅在使用Oracle JDK時才可用?
這樣做的原因隱藏在提供程序的代碼中。 以下幾行摘自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類中找到以下內容:
/** Register the algorithms below only when the full ECC implementation* is available*/ if (!useFullImplementation) {return; }好的,這說明了行為。 僅當可以成功加載本機庫(Windows計算機上的libsunec.so或sunec.dll)時,才存在算法。 在我們的情況下,顯然缺少該庫(因為我們僅復制了sunec.jar文件)。
不幸的是,如果我們閱讀了提供者文檔,我們將了解以下內容:
“ […] Java類打包到JRE擴展目錄中已簽名的sunec.jar中,而C ++和C函數打包到JRE本機庫目錄中的libsunec.so或sunec.dll中。 如果不存在本機庫,則該提供者已注冊為支持較少的ECC算法(省略了KeyPairGenerator,Signature和KeyAgreement)。”
不幸的是,這是我們自己急于采取的行動,這浪費了我們寶貴的開發時間。 摘自:有時候閱讀JavaDocs確實很有幫助……。 (但不像調試工作那樣富有教育意義)。
翻譯自: https://www.javacodegeeks.com/2013/10/how-to-use-ecc-with-openjdk.html
總結
以上是生活随笔為你收集整理的如何在OpenJDK中使用ECC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 优酷与 BBC Studios 合作,将
- 下一篇: 戴草莓晶越戴越倒霉是真的吗 戴草莓晶越戴