数字证书

[b]KeyTool证书管理[/b]
KeyTool是Java中的数字证书管理工具,用于数字证书的申请、导入、导出和撤销等操作。KeyTool与本地密钥库相关联,将私钥存于密钥库,公钥则以数字证书输出。KeyTool位于%JDK_HOME%\bin目录中,需要通过命令行进行相应的操作。

1. 构建自签名证书
keytool -genkeypair -keyalg RSA -keysize 2048 -sigalg SHA1withRSA -validity
36000 -alias www.zlex.org -keystore zlex.keystore

各参数的含义如下所示:
-genkeypair 表示生成密钥。
-keyalg 指定密钥算法,这里指定为RSA算法。
-keysize 指定密钥长度,默认1024位,这里指定为2048位。
-sigalg 指定数字签名算法,这里指定为SHA1withRSA算法。
-validity 指定证书有效期,这里指定为36000天。
-alias 指定别名,这里是www.zlex.org。
-keystore 指定密钥库存储位置,这里是zlex.keystore。
KeyTool工具支持RSA和DSA共2种算法,且DSA算法为默认算法。


[b]导出数字证书[/b]
keytool -exportcert -alias www.zlex.org -keystore zlex.keystore -file zlex.cer -rfc

各参数的含义如下所示:
-exportcert 表示证书导出操作。
-alias 指定导别名,这里为www.zlex.org。
-keystore 指定密钥库文件,这里为zlex.keystore。
-file 指定导出文件路径,这里为zlex.cer。
-rfc 指定以Base64编码格式输出。


这里通过KeyTool工具直接导出的证书,是一个自签名的X.509第三版类型的根证书,并以
Base64编码保存。自签名证书虽然可以使用,但未经过CA机构认证,几乎没有任何法律效力。


[b]构建CA签发证书[/b]
如果要获取CA机构认证的数字证书,需要将数字证书签发申请(CSR)导出,经由CA机
构认证并颁发,同时将认证后的证书导入本地密钥库和信任库。

导出数字证书签发申请
keytool -certreq -alias www.zlex.org -keystore zlex.keystore -file zlex.csr -v

-certreq 表示数字证书申请操作。
-alias 指定别名,这里为www.zlex.org。
-keystore 指定密钥库文件,这里为zlex.keystore。
-file 指定导出文件路径,这里为zlex.csr。
-v 详细信息。


获得签发后的数字证书后,需要将其导入信任库。导入数字证书操作

导入数字证书
keytool -importcert -trustcacerts -alias www.zlex.org -file zlex.cer -keystore zlex.keystore


导入证书后,我们可以通过相关命令查看该证书,命令如下:
keytool -list -alias www.zlex.org -keystore zlex.keystore

各参数的含义如下所示:
-list 表示导入数字证书。
-alias 指定别名,这里为www.zlex.org。
-keystore 指定密钥库文件,这里为zlex.keystore。


[b]证书使用:[/b]
Java 6提供了完善的数字证书管理实现,仅通过操作密钥库和数字证书就可完成相应的加密/解密和签名/验证操作。密钥库管理私钥,数字证书管理公钥,私钥和密钥分属消息传递两方,进行加密消息传递


	public static PrivateKey getPrivateKeyByKeyStore(String keyStorePath,String alias,String password)

throws Exception
{

//获取密钥库
KeyStore ks=getKeyStore(keyStorePath, password);

//获得私钥
return (PrivateKey)ks.getKey(alias, password.toCharArray());


}



// 由Certificate获得公钥

private static PublicKey getPublicKeyByCertificate(String certificatePath)
throws Exception
{

//获得证书
Certificate certificate=getCertificate(certificatePath);

//获得公钥
return certificate.getPublicKey();
}


//获得Certificate
private static Certificate getCertificate(String certificatePath)
throws Exception
{

CertificateFactory certificateFactory=CertificateFactory.getInstance(CERT_TYPE);

FileInputStream in=new FileInputStream(certificatePath);

Certificate certificate=certificateFactory.generateCertificate(in);

in.close();

return certificate;
}



private static KeyStore getKeyStore(String keyStorePath,String password)

throws Exception{

//实例化密钥库
KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType());

//获得密钥库文件流
FileInputStream is=new FileInputStream(keyStorePath);

//加载密钥库
ks.load(is,password.toCharArray());

//关闭密钥库文件流
is.close();

return ks;
}



私钥加密:
public static byte[] encryptByPrivateKey(byte[] data,String keyStorePath,String alias,String password)
throws Exception
{
//获取私钥
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath, alias, password);

//对数据加密
Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());

cipher.init(Cipher.ENCRYPT_MODE, privateKey);

return cipher.doFinal(data);

}



//私钥解密

public static byte[] decryptByPrivateKey(byte[] data,String keyStorePath,String alias,String password)
throws Exception
{

//获取私钥
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath, alias, password);

//对数据解密
Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());

cipher.init(Cipher.DECRYPT_MODE, privateKey);

return cipher.doFinal(data);

}



//公钥加密

public static byte[] encryptByPublicKey(byte[] data,String certificatePath)
throws Exception
{
//取得公钥
PublicKey publicKey=getPublicKeyByCertificate(certificatePath);

//对数据加密
Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

return cipher.doFinal(data);

}



//公钥解密
public static byte[] decryptByPublicKey(byte[] data,String certificatePath)
throws Exception
{
//取得公钥
PublicKey publicKey=getPublicKeyByCertificate(certificatePath);

Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());

cipher.init(Cipher.DECRYPT_MODE, publicKey);

return cipher.doFinal(data);


}



//签名
	public static byte[] sign(byte[] sign,String keyStorePath,String alias,String password)
throws Exception
{
//获得证书
X509Certificate x509Certificate=(X509Certificate)getCertificate(keyStorePath, alias, password);

//构建签名,由证书指定签名算法
Signature signature=Signature.getInstance(x509Certificate.getSigAlgName());

//获取私钥
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath, alias, password);

//初始化签名,由私钥构建
signature.initSign(privateKey);

signature.update(sign);

return signature.sign();



}



//验证签名
public static boolean verify(byte[] data,byte[] sign,String certificatePath)
throws Exception
{

//获得证书
X509Certificate x509Certificate=(X509Certificate)getCertificate(certificatePath);

//由证书构建签名
Signature signature=Signature.getInstance(x509Certificate.getSigAlgName());

//由证书初始化签名,实际上用到了证书中的公钥
signature.initVerify(x509Certificate);

signature.update(data);

return signature.verify(sign);


}


测试用例如下:
public class CertificateCoderTest {

private String password = "100889";
private String alias = "www.zlex.org";
private String certificatePath = "E:/jdk1.6/bin/zlex.cer";
private String keyStorePath = "E:/jdk1.6/bin/zlex,keystore";

@Test
public void test1() throws Exception
{
System.err.println("公钥加密-----私钥解密");
String inputStr="数字签名";

byte[] data=inputStr.getBytes();

//公钥加密
byte[] encrypt=CertificateCoder.encryptByPublicKey(data, certificatePath);

//私钥解密
byte[] decrypt=CertificateCoder.decryptByPrivateKey(encrypt, keyStorePath, alias, password);

String outputStr=new String(decrypt);

System.out.println("--"+outputStr);
System.err.println("加密前:\n"+inputStr);

System.err.println("解密后:\n"+outputStr);

assertArrayEquals(data, decrypt);

}



/**
* 私钥加密—公钥解密
* @throws Exception
*/
@Test
public void test2() throws Exception {
System.err.println("私钥加密—公钥解密");
String inputStr = "数字签名";
byte[] data = inputStr.getBytes();
// 私钥加密
byte[] encodedData = CertificateCoder.encryptByPrivateKey(data,
keyStorePath, alias, password);
// 公钥加密
byte[] decodedData = CertificateCoder.decryptByPublicKey(encodedData,
certificatePath);
String outputStr = new String(decodedData);
System.err.println("加密前:\n" + inputStr);
System.err.println("解密后:\n" + outputStr);
assertEquals(inputStr, outputStr);
}



/**
* 签名验证
* @throws Exception
*/
@Test
public void testSign() throws Exception
{
String inputStr = "签名";
byte[] data = inputStr.getBytes();
System.err.println("私钥签名—公钥验证");
// 产生签名
byte[] sign = CertificateCoder.sign(data, keyStorePath, alias, password);
System.err.println("签名:\n" + Hex.encodeHexString(sign));

// 验证签名
boolean status = CertificateCoder.verify(data, sign, certificatePath);
System.err.println("状态:\n" + status);


// 校验
assertTrue(status);
}

}
内容概要:本文围绕“考虑电动汽车聚合可调节能力的含波动性电源电氢耦合系统多目标优化运行”展开研究,提出了一种基于Matlab代码实现的多目标优化模型。该模型深度融合电-氢耦合系统与高比例波动性可再生能源(如风电、光伏),充分挖掘电动汽车(EV)集群作为移动储能单元的灵活调节潜力,通过聚合调控提升系统对新能源的消纳能力与运行经济性。研究系统构建了电动汽车可调度能力、电解水制氢与储氢动态过程、多能源协同互补的优化调度框架,并结合智能优化算法实现经济性、低碳性与运行稳定性等多重目标的协同优化。文中配套提供了完整的Matlab仿真代码、相关数据及可能的论文支撑材料,极大地方便了模型的复现、证与后续深化研究。; 适合人群:具备电力系统、综合能源系统、优化理论或新能源技术等相关领域基础知识的研究生、科研人员,以及从事新型电力系统规划、清洁能源消纳与智慧能源管理的工程技术人员。; 使用场景及目标:①开展高渗透率可再生能源接入下的综合能源系统多目标优化调度研究;②探究电动汽车集群在电网削峰填谷、平抑新能源出力波动及提供辅助服务方面的应用价值与潜力;③学习并掌握电氢耦合系统的建模方法、多目标优化求解技术及其在Matlab/Simulink环境下的仿真实现流程。; 阅读建议:此资源不仅提供可运行的代码,更蕴含了前沿的科研思路与创新方法,建议读者结合所提供的代码、数据与可能的论文文档,系统性地学习从问题建模、算法设计到仿真分析的完整科研过程,并重点关注其中关于需求侧资源聚合、多能互补协同与绿色低碳运行的核心理念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值