Using the SHA1WITHRSA algorithm to sign the source code under iOS
Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p>First of all, to understand a few related concepts to facilitate the resolution of the problems encountered Later: RSA Algorithm: 1977 by Ron rivest, Adi Shamirh and Lenadleman invented, RSA is taken from the names of their three people. The algorithm is based on a number theory: it is easy to multiply two large primes, but it is very difficult to factorization the results of this product, so the product can be exposed as a public key. This algorithm is capable of resisting all known password attacks. RSA algorithm is an asymmetric algorithm, the algorithm needs a pair of keys, using one of the encryption, you need to use another to Decrypt. When we are doing RSA encrypted communication, we put the public key on the client, the private key is left on the Server. PEM: since using RSA requires a pair of keys, of course we have to use tools to generate such a pair of keys First. Under linux, unix, the simplest and most convenient is to use the OpenSSL command line. Der and Pem are the two file formats that can be selected for the generated key. Der is the abbreviation of Distinguished Encoding rules, which is an information transmission grammar rule, defined in the ITU x.690. On the iOS side, our public key needs to be in such a format that we can get from certificate, key, and Trust Services Reference is seen in the description of the data parameter of the Seccertificatecreatewithdata function of this document. The PEM format is a format that encapsulates der, and he simply base64 the contents of Der and adds a Tail-to-kinsoku Description. The OpenSSL command line is output by default in Pem format, and to be able to be used under ios, we need to specify that der or Mr. to PEM and then convert der. And those keystore,pkcs,p7b,p12. introduction to iOS client encryption and decryption first we need to import security.framework, in ios, we focus on four functions <ul> <ul> <li> <ul> <li><strong>seckeyencrypt</strong>: Encrypting data with a public key</li> <li><strong>Seckeydecrypt</strong>: Decrypting data with the private key</li> <li><strong>seckeyrawverify</strong>: Use the public key to validate digital signatures and data to verify the source legitimacy of the Data. What is the digital signature, can refer to Baidu encyclopedia this article?</li> <li><strong>seckeyrawsign</strong>: Digest The data with the private key and generate a digital signature</li> </ul><p>RSA algorithm has 2 functions one is to encrypt one is to add a SIGN. From these functions, we can see that the first is to use the public key to encrypt the data on the client side, and to decrypt the server with the private key.</p><p>The second is to use the private key to sign the client, and then use the public key to verify the public key on the server Side. The first is purely for encryption, the second is to deny, to prevent others from impersonating our client to attack our servers, resulting in paralysis.</p></li> </ul> </ul><p><p>1.RSA Encryption Decryption:<br>(1) get the key, here is the generated key, the actual application can be read from the various storage media key (2) encryption (3) decryption<br>2.RSA Signing and verification<br>(1) get the key, here is the generated key, the actual application can read the key from a variety of storage media (2) to be signed hash code (3) get the signed string (4) authentication</p></p><p><p>3. Understanding of public and private keys:<br>(1) The private key is used for decryption and Signature.<br>(2) the public key is disclosed by me, used for encrypting and verifying the signature, which is for Others.<br>(3) when the user sends a file, signed with a private key, someone else uses the public key he gave to verify the signature, can guarantee that the information is sent by Him. When the user accepts the file, someone else encrypts it with his public key, and he decrypts it with the private key, which guarantees that the information can only be received by Him.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>First Add header file</p></p><p><p>#import <CommonCrypto/CommonDigest.h></p></p><p><p>#import <CommonCrypto/CommonCryptor.h></p></p><p><p>#import <Security/Security.h></p></p><p><p>#import "nsdata+base64.h"</p></p><p><p></p></p><p><p>#define KCHOSENDIGESTLENGTH cc_sha1_digest_length//SHA-1 The data bits of the message digest 160 bits</p></p><p><p></p></p><pre>-(nsdata *) gethashbytes: (nsdata *<span style="color: #000000;"><span style="color: #000000;">) PlainText {cc_sha1_ctx CTX; uint8_t</span></span>* Hashbytes =<span style="color: #000000;"><span style="color: #000000;">NULL; NSData</span></span>* Hash =<span style="color: #000000;"><span style="color: #000000;">nil; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Malloc A buffer to hold Hash. </span></span>Hashbytes = malloc (kchosendigestlength *<span style="color: #0000ff;"><span style="color: #0000ff;">sizeof</span></span><span style="color: #000000;"><span style="color: #000000;">(uint8_t)); Memset ((voidvoid</span></span>*) hashbytes,<span style="color: #800080;"><span style="color: #800080;">0x0</span></span><span style="color: #000000;"><span style="color: #000000;">, kchosendigestlength); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Initialize the Context. </span></span>Cc_sha1_init (&<span style="color: #000000;"><span style="color: #000000;">ctx); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Perform the Hash. </span></span>Cc_sha1_update (&ctx, (voidvoid *<span style="color: #000000;"><span style="color: #000000;">) [plaintext bytes], [plaintext length]); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Finalize the Output. </span></span>Cc_sha1_final (hashbytes, &<span style="color: #000000;"><span style="color: #000000;">ctx); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Build up the SHA1 blob. </span></span>hash = [nsdata Datawithbytes: (<span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span>Voidvoid *<span style="color: #000000;"><span style="color: #000000;">) hashbytes length: (nsuinteger) kchosendigestlength]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(hashbytes) Free (hashbytes); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">hash; }</span></span></pre><p><p></p></p><pre>-(nsstring *) Signthedatasha1withrsa: (nsstring *<span style="color: #000000;"><span style="color: #000000;">) PlainText {uint8_t</span></span>* Signedbytes =<span style="color: #000000;"><span style="color: #000000;">NULL; size_t signedbytessize</span></span>=<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">; Osstatus Sanitycheck</span></span>=<span style="color: #000000;"><span style="color: #000000;">noErr; NSData</span></span>* Signedhash =<span style="color: #000000;"><span style="color: #000000;">nil; NSString</span></span>* Path = [[nsbundle mainbundle]pathforresource:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">KeyStore</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>OfType:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">P12</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSData</span></span>* data =<span style="color: #000000;"><span style="color: #000000;">[nsdata datawithcontentsoffile:path]; Nsmutabledictionary</span></span>* options = [[nsmutabledictionary alloc] init];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set the private key to query Dictionary. </span></span>[options setobject:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">the password for your p12 file</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Forkey: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">) ksecimportexportpassphrase]; Cfarrayref Items</span></span>= Cfarraycreate (NULL,<span style="color: #800080;"><span style="color: #800080;">0</span></span>,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">, NULL); Osstatus Securityerror</span></span>= Secpkcs12import (cfdataref) data, (cfdictionaryref) options, &<span style="color: #000000;"><span style="color: #000000;">items); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(securityerror!=<span style="color: #000000;"><span style="color: #000000;">NoErr) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">nil; } cfdictionaryref identitydict</span></span>= Cfarraygetvalueatindex (items,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">); Secidentityref Identityapp</span></span>=<span style="color: #000000;"><span style="color: #000000;">(secidentityref) Cfdictionarygetvalue (identitydict,ksecimportitemidentity); Seckeyref Privatekeyref</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil; Secidentitycopyprivatekey (identityapp,</span></span>&<span style="color: #000000;"><span style="color: #000000;">privatekeyref); Signedbytessize</span></span>=<span style="color: #000000;"><span style="color: #000000;">seckeygetblocksize (privatekeyref); NSData</span></span>*plaintextbytes =<span style="color: #000000;"><span style="color: #000000;">[plaintext datausingencoding:nsutf8stringencoding]; Signedbytes</span></span>= malloc (signedbytessize *<span style="color: #0000ff;"><span style="color: #0000ff;">sizeof</span></span>(uint8_t));<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Malloc A buffer to hold Signature. </span></span>memset (voidvoid *) signedbytes,<span style="color: #800080;"><span style="color: #800080;">0x0</span></span><span style="color: #000000;"><span style="color: #000000;">, signedbytessize); Sanitycheck</span></span>=<span style="color: #000000;"><span style="color: #000000;">seckeyrawsign (privatekeyref, ksecpaddingpkcs1sha1, (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span>uint8_t *<span style="color: #000000;"><span style="color: #000000;">) [[self gethashbytes:plaintextbytes] bytes], kchosendigestlength, (uint8_t</span></span>*<span style="color: #000000;"><span style="color: #000000;">) signedbytes,</span></span>&<span style="color: #000000;"><span style="color: #000000;">signedbytessize); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(sanitycheck = =<span style="color: #000000;"><span style="color: #000000;">NoErr) {signedhash</span></span>= [nsdata Datawithbytes: (<span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span>Voidvoid *<span style="color: #000000;"><span style="color: #000000;">) signedbytes length: (nsuinteger) signedbytessize]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">nil; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(signedbytes) {free (signedbytes); } nsstring</span></span>*signatureresult=[nsstring stringwithformat:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [signedhash base64encodedstring]]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">signatureresult; } </span></span></pre><p><p></p></p><pre>-<span style="color: #000000;"><span style="color: #000000;">(seckeyref) getpublickey{nsstring</span></span>*certpath = [[nsbundle mainbundle] pathforresource:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">KeyStore</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>OfType:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">p7b</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Seccertificateref mycertificate</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil; NSData</span></span>*certificatedata =<span style="color: #000000;"><span style="color: #000000;">[[nsdata alloc] initwithcontentsoffile:certpath]; Mycertificate</span></span>=<span style="color: #000000;"><span style="color: #000000;">seccertificatecreatewithdata (kcfallocatordefault, (cfdataref) certificatedata); Secpolicyref MyPolicy</span></span>=<span style="color: #000000;"><span style="color: #000000;">SecPolicyCreateBasicX509 (); Sectrustref mytrust; Osstatus Status</span></span>= Sectrustcreatewithcertificates (mycertificate,mypolicy,&<span style="color: #000000;"><span style="color: #000000;">mytrust); Sectrustresulttype trustresult; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(status = =<span style="color: #000000;"><span style="color: #000000;">NoErr) {status</span></span>= Sectrustevaluate (mytrust, &<span style="color: #000000;"><span style="color: #000000;">trustresult); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">Sectrustcopypublickey (mytrust); } </span></span>-(nsstring *) Rsaencrypotothedata: (nsstring *<span style="color: #000000;"><span style="color: #000000;">) plaintext {seckeyref publickey</span></span>=<span style="color: #000000;"><span style="color: #000000;">nil; PublicKey</span></span>=<span style="color: #000000;"><span style="color: #000000;">[self getpublickey]; size_t cipherbuffersize</span></span>=<span style="color: #000000;"><span style="color: #000000;">seckeygetblocksize (publickey); uint8_t</span></span>*cipherbuffer =<span style="color: #000000;"><span style="color: #000000;">NULL; Cipherbuffer</span></span>= malloc (cipherbuffersize *<span style="color: #0000ff;"><span style="color: #0000ff;">sizeof</span></span><span style="color: #000000;"><span style="color: #000000;">(uint8_t)); Memset ((voidvoid</span></span>*) cipherbuffer,<span style="color: #800080;"><span style="color: #800080;">0</span></span>*<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">, cipherbuffersize); NSData</span></span>*plaintextbytes =<span style="color: #000000;"><span style="color: #000000;">[plaintext datausingencoding:nsutf8stringencoding]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>BlockSize = cipherbuffersize-<span style="color: #800080;"><span style="color: #800080;"></span> one</span>;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This place is more important is the encryption asked the team leader degree</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Numblock = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) Ceil ([plaintextbytes length]/(<span style="color: #0000ff;"><span style="color: #0000ff;">Double</span></span><span style="color: #000000;"><span style="color: #000000;">) blockSize); Nsmutabledata</span></span>*encrypteddata =<span style="color: #000000;"><span style="color: #000000;">[[nsmutabledata alloc] init]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>I=<span style="color: #800080;"><span style="color: #800080;">0</span></span>; i<numblock; i++<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>buffersize = MIN (blocksize,[plaintextbytes length]-i*<span style="color: #000000;"><span style="color: #000000;">blockSize); NSData</span></span>*buffer = [plaintextbytes Subdatawithrange:nsmakerange (i *<span style="color: #000000;"><span style="color: #000000;">blockSize, buffersize)]; Osstatus Status</span></span>=<span style="color: #000000;"><span style="color: #000000;">Seckeyencrypt (publickey, kSecPaddingPKCS1, (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span>uint8_t *<span style="color: #000000;"><span style="color: #000000;">) [buffer bytes], [buffer length], C ipherbuffer,</span></span>&<span style="color: #000000;"><span style="color: #000000;">cipherbuffersize); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(status = =<span style="color: #000000;"><span style="color: #000000;">NoErr) {nsdata</span></span>*encryptedbytes =<span style="color: #000000;">[[[ <span style="color: #000000;">nsdata alloc] initwithbytes: (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Const</span></span>Voidvoid *<span style="color: #000000;"><span style="color: #000000;">) Cipherbuffer length:cipherbuffersize] autorelease]; [encrypteddata appenddata:encryptedbytes]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">nil; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(cipherbuffer) {free (cipherbuffer); } nsstring</span></span>*encrypotoresult=[nsstring stringwithformat:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [encrypteddata base64encodedstring]]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">encrypotoresult; } </span></span></pre><p><p></p></p><p><p></p></p><p><p>Related Links:</p></p><p><p>Using the SHA1WITHRSA algorithm to sign the source code under iOS</p></p><p><p>Encrypt and decrypt data using RSA in iOS</p></p><p><p>Using RSA algorithm encryption with Java background decryption with demo under iOS</p></p><p><p>android, iOS and Server-side PHP use RSA encryption to decrypt communications</p></p><p><p><span class="link_title">using the SHA1WITHRSA algorithm to sign the source code under iOS</span></p></p><p><p><span class="link_title">RSA implementations in Objective C</span></p></p><p><p>Using the SHA1WITHRSA algorithm to sign the source code under iOS</p></p></span>
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