IOS RSA encryption and decryption and signature verification, iosrsa

Source: Internet
Author: User
Tags key string openssl rsa

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

Related Article

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.