This article reprinted to http://www.cocoachina.com/bbs/read.php?tid=235527Search a lot of information, did not find the right RSA method, a lot of people are asking this question, solved the comrades do not share, so let me share it, good to a praise!! Not good, do not spray, everyone modified to get up on the good ...
Can not only take, do not contribute ...
This project client does not use OpenSSL to generate the de, directly using this library to generate PublicKey and Privatekey.
The program was modified by the Https://github.com/kuapay/iOS-Certificate--Key--and-Trust-Sample-Project project, This item is encrypted when more than 100 of the length can not be encrypted (modified compatible many many bytes)!! Decryption because the project does not need, did not do the Shard decompression, only support more than 100 bytes (project urgency, no time to change), we can modify this method! I'll share it after writing.
-(NSString *) Decrypt: (NSString *) ciphertext
Key: (NSString *) key
Error: (Nserror *) error!!
Applicable environment:
1. The iOS application automatically generates the public key, the private key, and saves it. It's all going to be sandbox extraction.
2. The server public key generates the SECKEYREF format in der format for transmission to the server.
3, encryption has been Base64, after decryption has been base64.
Encryption:
rsacryptor *r = [Rsacryptor shareinstance];
Long randomdata = random ();
NSString *pubstr = [NSString stringwithformat:@ "%@", @ "12345"];
NSLog (@ "Rsaexchange%d", [str length]);
NSData *strdata = [pubstr datausingencoding:nsutf8stringencoding];
NSString *encryptstr = [R Rsa_encryptusingserverpublickeywithdata:strdata];
Decrypt:
nsstring *rsa_string = @ "xxxxxx";
Rsacryptor *r = [Rsacryptor shareinstance];
NSData *base64fromstring = [rsa_string datausingencoding:nsutf8stringencoding];
NSString *rsa_decryptdata = [R Rsa_decryptusingprivatekeywithdata:base64fromstring];
NSLog (@ "Rsa_decryptdata%@", rsa_decryptdata);
Ios-rsa and decryption sharing