Implement RSA encryption on the IOS client and rsa on the ios Client

Source: Internet
Author: User

Implement RSA encryption on the IOS client and rsa on the ios Client

In the IOS app login module, if the user name and password are directly transmitted to the background server, it is easy to intercept and forge network requests,

If the RSA algorithm is used to decrypt the private key on each client using the public key, the server cannot decrypt the key even if the password is intercepted.

This section only describes how the client encrypts and decrypts the password.

First open the terminal to generate the public key and private key (openSSL tools are required)

Openssl genrsa-out private_key.pem 1024 openssl req-new-key private_key.pem-out rsaCertReq. csropenssl x509-req-days 3650-in rsaCertReq. csr-signkey private_key.pem-out rsaCert. crtopenssl x509-outform der-in rsaCert. crt-out public_key.der openssl pkcs12-export-out private_key.p12-inkey private_key.pem-in rsaCert. crtopenssl rsa-in private_key.pem-out rsa_public_key.pem-pubout openssl pkcs8-topk8-in private_key.pem-out pkcs8_private_key.pem-nocrypt

In step 2, enter the key information, including the password. Remember the password you entered.
The above command will generate 7 files, IOS uses public_key.der and private_key.p12
Do not drag it into the project. Instead, click the + sign in the lower-right corner of Xcode and select add files ""
Add the library Security. framework after the project is created.

Add the dependent files RSAEncryptor and NSData + Base64 to the project and download them here.
Add the header files of RSAEncryptor and NSData + Base64
Implementation Code
RSAEncryptor * rsaEncryptor = [[RSAEncryptor alloc] init]; NSString * publicKeyPath = [[NSBundle mainBundle] pathForResource: @ "public_key" ofType: @ "der"]; NSString * privateKeyPath = [[NSBundle mainBundle] pathForResource: @ "private_key" ofType: @ "p12"]; [privacy encryption: publicKeyPath]; [rsaEncryptor encryption: privateKeyPath password: @ "1234"]; // password NSString * restrinBASE64STRING = [rsaEncryptor rsaEncryptString: @ ""]; NSLog (@ "encrypted: % @", restrinBASE64STRING ); // encrypted NSString * decryptString = [rsaEncryptor rsaDecryptString: restrinBASE64STRING]; // decrypt NSLog (@ "decrypted: % @", decryptString );
Output result

16:44:22. 455 RSA [10579: 458963]Encrypted: Export/Rt9xgCmhbaP8VEGC6Zd2ZnBMlNnQ +/release + vq6GiAqnYVOOCwtlm0U =

16:44:22. 456 RSA [10579: 458963]Decrypted:Hi!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.