问题描述
我不知道如果我错过了一些片code本example.I我得到编译时错误的certHeader,certFooter在这个class.IF有人可以给我一下一个简短的想法会有所帮助。或者如果有人对证书的解析器一个更好的例子,这将是有益的。我用这个例子来分析证书。
尝试
{
字符串ABC =----- BEGIN CERTIFICATE ----- \ N+ALneIwerZ5Nu + z1Yjvdco9sOHfkhYW4nL + FIlGDGIS + YsyevB8YN2hBnog7gtQ6PB + sVF6o / 1UdU \ N+ //删除线为简便起见,rchFUEChHZ5G7AAk02K7 / iyqITc / IPNHHpilTg / NB6QhF9s = \ N+----- END CERTIFICATE -----;
INT headerIndex = abc.indexOf(certHeader);
如果(headerIndex == -1)
{
抛出新CertificateParsingException(找不到BEGIN CERTIFICATE);
}
INT的startIndex = headerIndex + certHeader.length();
INT endIndex的= abc.indexOf(certFooter);
如果(endIndex的== -1)
{
抛出新CertificateParsingException(找不到END CERTIFICATE); }
字符串证书= abc.substring(在startIndex,endIndex的);
byte []的certBytes = cert.getBytes();
InputStream的时间=新Base64InputStream(新ByteArrayInputStream的(certBytes));
CertificateFactory certFact = CertificateFactory.getInstance(X.509);
证书certGen = certFact.generateCertificate(上);
X509证书X509 =(x509证书)
certGen;
}
赶上(例外五)
{
Log.e(testapp,异常:+ e.getMessage());
}
certHeader
和 certFooter
应该是INT变量。从你所告诉我们,它并不像你在任何地方宣称他们。
从 String.indexOf 文档
I wonder if I am missing some piece of code in this example.I am getting compile-time error on certHeader,certFooter in this class.IF someone can give me a brief idea about it that will be helpful.or IF some one has a better example on certificate parser that will be helpful.I am using this example to parse a certificate.
try
{
String abc = "-----BEGIN CERTIFICATE-----\n" + "ALneIwerZ5Nu+z1Yjvdco9sOHfkhYW4nL+FIlGDGIS +YsyevB8YN2hBnog7gtQ6PB+sVF6o/1UdU\n" + // lines deleted for brevity "rchFUEChHZ5G7AAk02K7/iyqITc/IPNHHpilTg/NB6QhF9s=\n" + "-----END CERTIFICATE-----";
int headerIndex = abc.indexOf(certHeader);
if (headerIndex == -1)
{
throw new CertificateParsingException("cannot find BEGIN CERTIFICATE");
}
int startIndex = headerIndex + certHeader.length();
int endIndex = abc.indexOf(certFooter);
if (endIndex == -1)
{
throw new CertificateParsingException("cannot find END CERTIFICATE"); }
String cert = abc.substring(startIndex, endIndex);
byte[] certBytes = cert.getBytes();
InputStream in = new Base64InputStream(new ByteArrayInputStream(certBytes));
CertificateFactory certFact = CertificateFactory.getInstance ("X.509");
Certificate certGen = certFact.generateCertificate(in);
X509Certificate x509 = (X509Certificate)
certGen;
}
catch (Exception e)
{
Log.e("testapp", "exception: " + e.getMessage());
}
certHeader
and certFooter
are supposed to be int variables. From what you've shown us, it doesn't look like you've declared them anywhere.
From the String.indexOf documentation:
这篇关于在Android的证书分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!