IOS RSA encryption and decryption and signature verification, iosrsa
1. First download openssl. Needless to say, download it directly from the official website or use brew install openssl.
2. The terminal generates the Private Key
2.1 generate a private key
openssl genrsa -out rsa_private_key.pem 1024
2.2 generate a key
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
2.3 convert RSA private key to pkcs8 format
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out rsa_private_key.pem
The private key and Key address are in the root directory file rsa_private_key.pem rsa_public_key.pem.
You can right-click the file editor to view the price
You can also run the command cat rsa_private_key.pem and cat rsa_pubilic_key.pem on the terminal to view the private key string.
3. Import openssl and HBRSAHandler folder
Tip: After dragging it in: librarySearchpath: it will be automatically set, and then copy the path to headSearchpath, and add/include later.
4. Use:
You can import the key in two ways and then call the corresponding method.
NSString * private_key_string = @ "examples/examples + examples/b5L14erGo52Jv/j/examples + 3fPxFeY7ya + examples + vvAYX/7fh + examples/ examples/lyAyZCKse2/examples + Ijedm/ZlZmHhtBtstKJgVAkBKma/examples + Qe/abcpKowkiT9zP + samples + ny1emEHUOs2fm8 "; NSString * public_key_string = @ "logs/7MhR99 + yswtvSr0dKenD/logs + df2 + logs/4/+ logs"; NSString * publicKeyFilePath = [[NSBundle mainBundle] pathForResource: @ "Define" ofType: nil]; NSString * privateKeyFilePath = [[NSBundle mainBundle] pathForResource: @ "Define" ofType: nil]; HBRSAHandler * handler = [HBRSAHandler new]; // two import methods // [handler: KeyTypePublic andPath: publicKeyFilePath]; // [handler: encryption andPath: privateKeyFilePath]; [handler importKeyWithType: KeyTypePrivate andkeyString: private_key_string]; [handler importKeyWithType: KeyTypePublic andkeyString: public_key_string]; NSString * sig = [handler signString: @ "signature string"]; NSString * sigMd5 = [handler signMD5String: @ "signature string"]; NSLog (@ "% @", sig, sigMd5); BOOL isMatch = [handler verifyString: @ "signature string" withSign: sig]; BOOL isMatchMd5 = [handler verifyMD5String: @ "signature string NSLog (@" % d ", isMatch, isMatchMd5); NSString * enString = [handler encryptWithPublicKey: @" encrypted string "]; NSString * deString = [handler decryptWithPrivatecKey: enString]; NSLog (@ "% @", deString );
4. Result verification (Note: The public key encryption results will be different each time, but the Private Key signature results will be the same each time)
IOSRSAHandler-master.zip