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

时间:2019-11-22 07:32:09

标签: java ssl certificate 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文件生成证书。我已经检查了Google中的许多链接,但对我来说不起作用。从.pem文件生成证书时,它会抛出不完整的数据或空数据。

预先感谢

1 个答案:

答案 0 :(得分:0)

如果您想阅读证书,可以在Java代码下面使用它。

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

如果您还想让我知道