IOS RSA encryption decryption and signature verification

Source: Internet
Author: User
Tags install openssl key string openssl rsa

IOS RSA encryption decryption and signature verification

1. To download OpenSSL first, this needless to say, download directly from the official website or download with the brew install OpenSSL

2. Terminal generates private key key

2.1 Generating the private key

OpenSSL genrsa-out Rsa_private_key.pem 1024

2.2 Generate key

OpenSSL rsa-in rsa_private_key.pem-pubout-out Rsa_public_key.pem

2.3 Convert the RSA private key to PKCS8 format

OpenSSL pkcs8-topk8-inform pem-in rsa_private_key.pem-outform pem-nocrypt-out Rsa_private_key.pem

Private key and key address in root directory file Rsa_private_key.pem Rsa_public_key.pem

Ask price can be opened by right-click File Editor View

You can also view the private key string by using the terminal commands Cat Rsa_private_key.pem and Cat Rsa_pubilic_key.pem

3. Import OpenSSL, import the Hbrsahandler folder

Tip: Drag it in: Librarysearchpath: Automatically set, then copy the path to Headsearchpath, and add/include behind it.

4. Use:

Two ways to import the key and then call the appropriate method.

nsstring* private_key_string = @ "miicdqibadanbgkqhkig9w0baqefaascal8wggjbageaaogbalgv/syfh337kzc29kvr0p6cp+ glrqjdyaqno5ifafxzjgf1ehbjzblkv+hilaznbolyu1pnluookzj6pg1a5huzlpsbya5mwr1buhaljxlab3thcpzx51/b5l14ergo52jv/j/ 63yljetmm8almky8s+3fpxfey7ya+2vxmetplagmbaaecgyaguvauzwgpq37zuy+7clfa061plyau8tkyw+ qabqonupdqtq4vf3s2lqbwhzikvcxvovb70nm0onsisjfb1xjbpyfdbfug7d+y2f8yr6atoezoy5dbyef3svg9kp9ra+vvayx/7fh+ thcu0hovp0z8ikzirswzaq+3a2gicijrwqjbapkvji89hgamewljjfzapilbqzuwr2w/ rp7ely5ddkfjcoshhgghfob71bnrmom0h4s85gx6a87n9r2to0c51q0cqqdcx6yydt/ 9jgorynsxfzmfszyvormpio77r0ywka3uowwla56l2lc4ayo10/lyayzckse2/5d9zzub7xoyemgzakb8mejvpuoy/ bsc3rqenrjetersawzaobjcx4oac3agtxmhwv1fmqfbfktodbddze+ijedm/zlzmhhtbtstkjgvakbkma/ Dghrtuscit90qhbjb3f3fhjb4pbpcyzkscqmxxmy73/lg0ktxqnujlyy4zm6jnim0ozgroq6chgkubfezakbmcgf2tpfejh8xodovlw5adnuiq   +QE/ABCPKOWKIT9ZP+RYT9XJAX7QXCHJDWTZB6AHNJY1+NY1EMEHUOS2FM8 "; nsstring* public_key_string = @ "migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqc4l/7mhr99+yswtvsr0dkend/ ojuaow2aej6oyn2n12y4h9riqy2w5sr/h4iwmzqtpwfnt5y7jjpgy+qynqor1gs6bg2gutmk9w1bwc41y2gd0xwqwv+df2+s9ehqxqodib/4/+     T2jyxltjvac5pgpevt3z8rxmo8mvtlvzblazqidaqab ";   NSString *publickeyfilepath = [[NSBundle mainbundle] pathforresource:@ "Rsa_public_key.pem" ofType:nil]; NSString *privatekeyfilepath = [[NSBundle mainbundle] pathforresource:@ "Rsa_private_key.pem" ofType:nil]; hbrsahandler* handler = [Hbrsahandler new];//two ways of importing//[handler Importkeywithtype:keytypepublic Andpath:       Publickeyfilepath];   [Handler Importkeywithtype:keytypeprivate 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%d ", Ismatch,ismatchMD5);   nsstring* enstring = [Handler encryptwithpublickey:@ "encrypted string"];   nsstring* destring = [handler decryptwithprivateckey:enstring]; NSLog (@ "%@", destring);

4 Result Verification (note: Public key encryption, the result of each encryption will be different, but the result of the private key signature is the same every time)

Iosrsahandler-master.zip

IOS RSA encryption decryption and signature verification

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.