在iOS上使用RSA公钥加密 [英] Encryption with RSA public key on iOS

查看:296
本文介绍了在iOS上使用RSA公钥加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个iOS应用程序与现有的服务器进行通信。服务器生成RSA密钥对(公共密钥和私钥),并将公钥发送给客户端。



客户端(iOS应用程序)必须仅使用公钥,并将加密的数据发送到服务器。



所以,我需要一个Objective-C函数来进行RSA加密。



输入:




  • 纯文本:hello world!

  • 公钥:

      ----- BEGIN PUBLIC KEY ----- 
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEChqe80lJLTTkJD3X3Lyd7Fj +
    zuOhDZkjuLNPog3YR20e5JcrdqI9IFzNbACY / GQVhbnbvBqYgyql8DfPCGXpn0 + X
    NSxELIUw9Vh32QuhGNr3 / TBpechrVeVpFPLwyaYNEk1CawgHCeQqf5uaqiaoBDOT
    qeox88Lc1ld7MsfggQIDAQAB
    ----- END PUBLIC KEY -----




流程:



pre> +(NSString *)encryptString:(NSString *)str publicKey:(NSString *)pubKey;

输出:



uwGuSdCgDxAAN3M5THMrNcec3Fm / KN + uUk7ty1s70oH0FNTAWz / 7FMnEjWZYOtHe37G3D4DjqiWijyUCbRFaz43oVDUfkenj70NWm3tPZcpH8nsWYevc9a1M9GbnNF2jRlami8LLUTZiogypSVUuhcJvBZBOfea9cOonX6BG + VW =



<强>问题:



如何实现此功能?

  +(NSString *) encryptString:(NSString *)str publicKey:(NSString *)pubKey; 

我已经挖了很久,在SO和谷歌和苹果的文档。我发现苹果需要一个.der文件来加密,不仅仅是公钥。

解决方案

我会回答我的问题:



1。使用公钥字符串创建SecKeyRef



我被这篇文章帮助: http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/#its-全部在格式



它导致我的代码: https://github.com/ideawu/Objective-C-RSA/blob/master/RSA.m#L69



2。使用SecKeyRef加密输入数据



使用Apple的 SecKeyEncrypt https://developer.apple.com/library/ios/documentation/Security/Reference/certifkeytrustservices/index.html



3。 github上的完整代码



https://github.com/ ideawu / Objective-C-RSA


I write an iOS app to communicate with an existing server. The server generates RSA key pair(public key and private key), and sends public key to the client.

The client(the iOS app) have to encrypt with ONLY the public key, and sends the encrypted data to server.

So, I need a Objective-C function to do RSA encryption.

Input:

  • Plain text: hello world!
  • The public key:

    -----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEChqe80lJLTTkJD3X3Lyd7Fj+
    zuOhDZkjuLNPog3YR20e5JcrdqI9IFzNbACY/GQVhbnbvBqYgyql8DfPCGXpn0+X
    NSxELIUw9Vh32QuhGNr3/TBpechrVeVpFPLwyaYNEk1CawgHCeQqf5uaqiaoBDOT
    qeox88Lc1ld7MsfggQIDAQAB
    -----END PUBLIC KEY-----
    

Process:

+ (NSString *)encryptString:(NSString *)str publicKey:(NSString *)pubKey;

Output:

uwGuSdCgDxAAN3M5THMrNcec3Fm/Kn+uUk7ty1s70oH0FNTAWz/7FMnEjWZYOtHe37G3D4DjqiWijyUCbRFaz43oVDUfkenj70NWm3tPZcpH8nsWYevc9a1M9GbnNF2jRlami8LLUTZiogypSVUuhcJvBZBOfea9cOonX6BG+vw=

Question:

How to implement this function?

+ (NSString *)encryptString:(NSString *)str publicKey:(NSString *)pubKey;

I have had digg for a long time, on SO and google and Apple's document. I found out Apple need a .der file to do encryption, not only the public key.

解决方案

I will answer my qustion:

1. create SecKeyRef with public key string

I am helped by this post: http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/#its-all-in-the-format

It led to my code: https://github.com/ideawu/Objective-C-RSA/blob/master/RSA.m#L69

2. use the SecKeyRef to encrypt input data

Use Apple's SecKeyEncrypt(https://developer.apple.com/library/ios/documentation/Security/Reference/certifkeytrustservices/index.html)

3. the full code on github

https://github.com/ideawu/Objective-C-RSA

这篇关于在iOS上使用RSA公钥加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆