-1

I have a requirement for slate integration. I have a code for posting data but I want it to convert into java. Below is the code for reference:

'''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();'''

Please help in converting c code to java code or in generating a certificate from .pem file. I have checked many links in google but it's not working for me. It is throwing incomplete data or empty data while generating certificate from .pem file.

Thanks in advance,

user207421
  • 305,947
  • 44
  • 307
  • 483
Ananya
  • 1

1 Answers1

0

If you want read the certificate you can use this below java code.

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

If you want something else let me know

Vawani
  • 399
  • 10
  • 25
  • Hi Vawani, i have tried with this code and its throwing me the below error: Exception in thread "main" java.security.cert.CertificateException: Could not parse certificate: java.io.IOException: java.lang.IllegalArgumentExcepti on: Illegal base64 character 5c at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:110) at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:339) – Ananya Nov 22 '19 at 08:12