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!