【发布时间】:2014-12-17 23:39:23
【问题描述】:
我面临一个问题,在我看来,Apple 文档中没有大量描述。
我需要使用后台提供的 RSA 私钥对 NSData 进行签名。私钥以字符串形式接收。
如何做到这一点?我不想创建自己的密钥对,我只想使用单个 PRIVATE 密钥来签署 NSData。
我找到了几个使用 OPENSSL 的解决方案,但它们都不起作用,我无法找到任何适合我的本地 CommonCrypto 库问题的解决方案。
其实这是我需要复制的一段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);
- 这个返回的私钥是从存储在应用程序数据库中的字符串生成的
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);
}
}
- 这会以 base64 字符串的形式返回有符号字节数组
如何在 iOS 中实现这一点?我花了很多时间搜索网络并尝试了几种方法,但都没有成功。
我会非常感谢任何代码 sn-ps,因为像“CommonCrypto 应该这样做”这样的提示对我不起作用。
非常感谢
【问题讨论】:
-
也许这会对你有所帮助:github.com/nicklockwood/CryptoCoding
-
@DavidKaszas 这似乎不包括 RSA,只有 AES。
标签: ios objective-c openssl private-key commoncrypto