简体   繁体   English

带有P12证书的Android签名数据

[英]Android Sign Data with P12 Certificate

So I've searched around for a while and couldn't really find what I needed (since every result that comes up is in regards to signing the actual package). 所以我已经搜索了一段时间并且无法真正找到我需要的东西(因为出现的每一个结果都是关于签署实际的包)。 So basically what I have going on is that the WebServices are protected with a Session Manager and in order to get a session number a GUID needs to be sent to the WebService. 所以基本上我所做的是使用会话管理器保护Web服务,并且为了获得会话号,需要将GUID发送到WebService。 On the iOS side we were able to get this setup (since the team that did the development of the WebService had a iOS developer there to help) but since no one on that team or here has much experience with Android were struggling on trying to figure out how to do the same thing. 在iOS方面,我们能够得到这个设置(因为开发WebService的团队有一个iOS开发人员可以提供帮助)但是因为那个团队中没有人或者这里有很多Android经验的人都在努力想如何做同样的事情。 Below is what I've tried so far, but the GUID that is produced is composed of some strange characters and doesn't work, so I'm stuck for now while I keep looking for other options. 下面是我到目前为止所尝试的内容,但是生成的GUID由一些奇怪的字符组成,并且不起作用,所以我一直在寻找其他选项。

        String password = "password";
        String text = "545048";
        KeyStore keyStore = KeyStore.getInstance("pkcs12");
        InputStream inputStream = activity.getResources().openRawResource(R.raw.am_client);
        keyStore.load(inputStream, password.toCharArray());

        String alias = keyStore.aliases().nextElement();
        PrivateKey privateKey = (PrivateKey)keyStore.getKey(alias, password.toCharArray());
        X509Certificate certificate = (X509Certificate)keyStore.getCertificate(alias);

        //Sign Data
        byte[] dataToSign = text.getBytes("UTF-8");
        Signature signature1 = Signature.getInstance("SHA1WithRSA");
        signature1.initSign(privateKey);
        signature1.update(dataToSign);
        byte[] signedData = signature1.sign();
        String signed = new String(signedData, "UTF-8");
        Log.d("MESSAGE", "string = " + signed);
        //This is what comes up in the Log: ��m(N� �xp�r���K�V>G3b���M��W08���6#�͞�+�/�]�,�o���N"�$��uVa�ƾ�~��

I figured out the problem, the code was fine but when I was converting the text to a byte array I was using the wrong format (UTF-8) since the service was using (UTF-16LE). 我想出了问题,代码很好但是当我将文本转换为字节数组时,我使用了错误的格式(UTF-8),因为服务正在使用(UTF-16LE)。 Just had to dig into the service to find that solution. 只需要深入研究服务就能找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM