android中的bad base64

时间:2012-03-08 14:49:15

标签: android security ssl-certificate

您好我正在使用以下代码来解析证书详细信息。除了下面提到的一点问题外,一切都很好。

package android.net.http;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.security.cert.Certif`enter code here`icate;
import java.security.cert.CertificateFactory;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.util.Base64InputStream;
import android.util.Log;

 public class SslCertificate1Activity extends Activity
 {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

try  {
String text = "-----BEGIN CERTIFICATE-----\n"+
    "MIIC/TCCAmagAwIBAgIBKjANBgkqhkiG9w0BAQQFADCBqDEiMCAGA1UEAxMZeG1sZ2F0ZXdheS5p\n"+
    "dHMudXRleGFzLmVkdTEoMCYGA1UECxMfSW5mb3JtYXRpb24gVGVjaG5vbG9neSBTZXJ2aWNlczEq\n"+
    "MCgGA1UEChMhVGhlIFVuaXZlcnNpdHkgb2YgVGV4YXMgYXQgQXVzdGluMQ8wDQYDVQQHEwZBdXN0\n"+
    "aW4xDjAMBgNVBAgTBVRleGFzMQswCQYDVQQGEwJVUzAeFw0wNDA1MDkwNTMwMTBaFw0wNTA1MDQw\n"+
    "NTMwMTBaMIGAMQswCQYDVQQGEwJVUzEOMAwGA1UECBMFVGV4YXMxDzANBgNVBAcTBkF1c3RpbjEq\n"+
    "MCgGA1UEChMhVGhlIFVuaXZlcnNpdHkgb2YgVGV4YXMgYXQgQXVzdGluMRMwEQYDVQQLEwpUb29s\n"+
    "cyBUZWFtMQ8wDQYDVQQDEwZDbGllbnQwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJ6PABjb\n"+
    "zXUkgo29S4uv1Qz9reo1/tP4pkQTGAldSbtA4hVtA/3sjw2+u3kgxYruAi2cXV2k0RPZhsUZjlDk\n"+
    "jMPb/dlY81bD8gqe3lu3ezugJrlArlpfWN6PlufbTjxHSqIA0XD9R5/ZECaUV9dD43K5KdWUCy99\n"+
    "YKDiSwVPO9F5AgMBAAGjXTBbMB0GA1UdDgQWBBRkCCpscEXxXu8Ba67p6zdh13ypjzAfBgNVHSME\n"+
    "GDAWgBR2RsZH2kSY782kBROo92FAWS6sADAJBgNVHRMEAjAAMA4GA1UdDwQHAwUBEiRIkDANBgkq\n"+
    "hkiG9w0BAQQFAAOBgQCtV1NzpdVBs5vyb8yLXNA3hA1LsmE/2QanXG4T3UN93BI4HQzx0idnkN1Y\n"+
    "0RAQ1rjGeQ1pk3l2DWsPi9mTkCGmYs/EMLkKOBee9ad3BIG6sKwXgbgLyNLgda+Y1bo+SIomq/a7\n"+
    "yP92UHMFEegfS/ssECA+Q3hHuU6in3AqLfWH1w==\n"+
    "-----END CERTIFICATE-----";
int startIndex = 0;
String cert = text.substring(startIndex,text.length()); 
    byte[] certBytes = cert.getBytes();
    InputStream in = new Base64InputStream(new ByteArrayInputStream(
            certBytes), 0);
CertificateFactory certFact = CertificateFactory.getInstance ("X.509");
    Certificate certGen = certFact.generateCertificate(in); 
    X509Certificate x509 = (X509Certificate) certGen; 
     Log.i("","certificate details:"+x509);
    } 
    catch (Exception e)  
    {
        Log.e("testapp", "exception: " + e.getMessage()); 
    }  
}
 }

我得到了android.util.BASE64DataException:当我启动调试器时,在下面一行显示错误的base-64: -

Certificate certGen = certFact.generateCertificate(in);

似乎Base64InputStream有问题。请帮助纠正异常。     提前致谢

1 个答案:

答案 0 :(得分:6)

不,Base64InputStream没有任何问题。如果有疑问,您应该怀疑您的自己的代码是错误的,而不是其他人的代码。

错误的是你在填充部分之后给出以“----- END CERTIFICATE -----”结尾的Base64InputStream数据。

你应该只在之间传递“----- BEGIN CERTIFICATE -----”和“----- END CERTIFICATE -----”。< / p>

看起来你已经开始考虑这个了:

int startIndex = 0;
String cert = text.substring(startIndex,text.length()); 

...但该代码不会执行任何操作 - 当startIndex为0时,substring将返回整个字符串...

就我个人而言,我会考虑使用Base64类进行Base64转换首先,以将字符串的base64部分(您仍然需要获取子字符串)转换为{{1然后围绕它创建一个byte[]