将字符串转换为公钥android

时间:2014-03-01 05:52:45

标签: java android

我正在尝试开发短信加密应用程序。我必须通过短信传达公钥。现在我能够通过生成密钥对(在同一个移动客户端上)进行加密/解密,但是当我将密钥作为编码字符串发送时,然后在另一侧我无法从字符串中恢复公钥。代码如下。在strToPublicKey()函数中,参数不在obj。

中返回
byte[] keyBytes = Base64.decode(getPublicKey());
PublicKey publickey = strToPublicKey(new String(keyBytes));

//private String publicKey;
    private String getPublicKey()
    {
        // this string is received through SMS.. here it is hardcoded for testing purpose 
        // Base 64 encoded public key
        return "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0NCk1JR2ZNQTBHQ1NxR1NJYjNEUUVCQVFVQUE0R05BRENCaVFLQmdRRE1PZGtaN28yMmJiSTAxZnY5QWtvMXFmTHd6MzRBYlA3ejgrcUMNCi9XZGNHdEFmdGk1QUFvZWUxOTV2NUd1QTZhak9JS3Y1cDkzdEdFNitWamdRdG03d0phS25kMno1UGpvZHIvc2R5bW9hNGdZT2dXcHQNCmVvamRybFZmeHZBbUxUN0JsWEExNGNuQUxDc3prYUtmTktSeEIrWDZlZGIyQkVaVnlKYThQcTBLYndJREFRQUINCi0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQ==";
    }

    public static PublicKey strToPublicKey(String s)
    {

        PublicKey pbKey = null;

            BufferedReader br   = new BufferedReader( new StringReader(s) );
            PEMReader pr        = new PEMReader(br);
            Object obj = pr.readObject(); 

            if( obj instanceof PublicKey )
            {
                pbKey = (PublicKey) pr.readObject();
            }
            else if( obj instanceof KeyPair ) 
            {
                KeyPair kp = (KeyPair) pr.readObject();
                pbKey = kp.getPublic();
            }
            pr.close();
        return pbKey;
    }

logcat中没有错误

* 03-01 06:54:00.054:I / System.out(4033):调试器已解决(1412) 03-01 06:54:01.614:D / dalvikvm(4033):GC_CONCURRENT释放211K,12%免费2573K / 2904K,暂停75ms + 76ms,总计251ms

03-01 06:54:02.784:D / dalvikvm(4033):GC_CONCURRENT释放208K,11%免费2763K / 3096K,暂停77ms + 5ms,总计176ms

03-01 06:54:06.384:D / dalvikvm(4033):GC_CONCURRENT释放128K,8%免费3025K / 3280K,暂停75ms + 94ms,总计282ms

03-01 06:54:09.866:D / dalvikvm(4033):GC_CONCURRENT释放269K,12%免费3144K / 3540K,暂停76ms + 93ms,总计259ms

03-01 06:56:52.745:D / dalvikvm(4033):threadid = 1:撤消后仍然暂停(sc = 1 dc = 1) 03-01 06:56:52.765:D / dalvikvm(4033):GC_CONCURRENT释放235K,10%免费3316K / 3680K,暂停83ms + 9ms,总计208ms *

肯定有一些铸造问题......请有人帮助我:(

0 个答案:

没有答案