带有P12证书的Android签名数据(Android Sign Data with P12 Certificate)

编程入门 行业动态 更新时间:2024-10-28 21:14:14
带有P12证书的Android签名数据(Android Sign Data with P12 Certificate)

所以我已经搜索了一段时间并且无法真正找到我需要的东西(因为出现的每一个结果都是关于签署实际的包)。 基本上我所做的是WebServices受会话管理器保护,为了获得会话号,需要将GUID发送到WebService。 在iOS方面,我们能够得到这个设置(因为开发WebService的团队有一个iOS开发人员可以提供帮助)但是因为那个团队中没有人或者这里有很多Android经验的人都在努力想如何做同样的事情。 以下是我到目前为止所尝试的内容,但是生成的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�ƾ�~��

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. 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. 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.

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�ƾ�~��

最满意答案

我想出了问题,代码很好但是当我将文本转换为字节数组时,我使用了错误的格式(UTF-8),因为服务正在使用(UTF-16LE)。 只需要深入了解服务以找到解决方案。

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). Just had to dig into the service to find that solution.

更多推荐

本文发布于:2023-07-30 03:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1322183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:证书   数据   Android   Data   Sign

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!