如何在java中使用.pem文件证书发布数据

问题描述 投票:0回答:1

我有一个 slate 集成的需求。我有一个用于发布数据的代码,但我希望将其转换为java。以下是参考代码:

'''string host = @url;
string certName = @"myfile.pfx"; // i am having .pem file
string password = @"password"; // no password
var certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(certName, 
 password);        
System.Net.ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
var req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(host);
req.PreAuthenticate = true;
req.Credentials = new System.Net.NetworkCredential("username", "");
req.ClientCertificates.Add(certificate);
req.Method = "POST";
req.ContentType = "text/xml";
string postData = "<hello>world</hello>";
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postData);
req.ContentLength = postBytes.Length;
req.GetRequestStream().Write(postBytes, 0, postBytes.Length);
req.GetRequestStream().Close();
var resp = req.GetResponse();'''

请帮助将 c 代码转换为 java 代码或从 .pem 文件生成证书。我已经检查了谷歌中的许多链接,但它对我不起作用。从 .pem 文件生成证书时抛出不完整的数据或空数据。

提前致谢,

java ssl certificate
1个回答
0
投票

如果你想阅读证书,你可以使用下面的java代码。

CertificateFactory fact = CertificateFactory.getInstance("X.509");
FileInputStream is = new FileInputStream (pemfilepath);
X509Certificate cer = (X509Certificate) fact.generateCertificate(is);
PublicKey key = cer.getPublicKey();

如果您还想要其他东西,请告诉我

© www.soinside.com 2019 - 2024. All rights reserved.