简体   繁体   English

使用RSA私钥对NSData签名

[英]Sign NSData with RSA private key

I am facing an issue which from my side lacks a lot of description in Apples documentation. 我所面临的问题从我的角度来讲在Apple文档中缺少很多描述。

I need to sign NSData with RSA private key which is provided from backoffice. 我需要使用由后台提供的RSA私钥对NSData进行签名。 Private key is received in form of string. 私钥以字符串形式接收。

How to achieve this? 如何实现呢? I do not want to create my own key pairs, I just want to use that single PRIVATE key to sign NSData. 我不想创建自己的密钥对,我只想使用单个PRIVATE密钥对NSData进行签名。

I found several solutions using OPENSSL, but none of them works and I am not able to find any suitable solution for my problem with native CommonCrypto library. 我发现了使用OPENSSL的几种解决方案,但是它们都不起作用,而且我找不到与本机CommonCrypto库有关的任何合适的解决方案。

In fact, this is a piece of Android code I need to replicate: 实际上,这是我需要复制的一段Android代码:

public static PrivateKey getPrivateKey() throws Exception {
String key = ContentHolder.getInstance(context).getClientPrivateKey();
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(android.util.Base64.decode(key, android.util.Base64.NO_WRAP));
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(spec);

- this one returns Private key which is generated from string stored in app's database -这个返回私钥,私钥是从存储在应用数据库中的字符串生成的

public String sign(byte[] array) throws SignatureException {
        try {
            Signature sign = Signature.getInstance("SHA1withRSA");
            sign.initSign(privateKey);
            sign.update(array);
            return android.util.Base64.encodeToString(sign.sign(), android.util.Base64.NO_WRAP);
        } catch (Exception ex) {
            throw new SignatureException(ex);
        }
    }

- this returns signed byte array in form of base64 string -这将以base64字符串的形式返回带符号的字节数组

How to achieve this in iOS? 如何在iOS中实现? I spent many hours searching web and trying several approaches, none of them was successful. 我花了很多时间搜索网络并尝试了几种方法,但没有一种成功。

I would be very thankful for any code snippets, since hints like "CommonCrypto should do this" do not work for me. 对于任何代码片段,我将深表感谢,因为“ CommonCrypto应该执行此操作”之类的提示对我不起作用。

Thank you very much 非常感谢你

The main problem is that Apple officially doesn't support signing using a string-key ( https://devforums.apple.com/message/641836#641836 ). 主要问题是Apple官方不支持使用字符串键( https://devforums.apple.com/message/641836#641836 )进行签名。 They insist on using .p12 and importing it using SecPKCS12Import. 他们坚持使用.p12并使用SecPKCS12Import导入。

Check this answer : https://stackoverflow.com/a/27945240/4324866 检查此答案https : //stackoverflow.com/a/27945240/4324866

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

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