openjdk安装
曾经尝试在Java和OpenJDK中使用椭圆曲线密码术(ECC)的每个人要么被迫使用Bouncy Castle,要么被SunEC提供者 弄糊涂了。 SunEC提供程序根据 文档(报价)提供以下算法:
AlgorithmParameters | 欧共体 |
KeyAgreement | ECDH |
KeyFactory | 欧共体 |
KeyPairGenerator | 欧共体 |
Signature | ECDSA没有SHA1withECDSASHA256withECDSASHA384withECDSASHA512withECDSA |
不幸的是,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 library
return 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 contents
if (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安装