Common iOS encryption algorithms

Source: Internet
Author: User

1, MD5//because the category is used, the parameters of the wood are passed in.-(NSString *) stringFromMD5 {if(self = nil | | [self length] = =0) {        returnNil; }    Const Char*value =[self utf8string]; unsignedCharOutputbuffer[cc_md5_digest_length];    CC_MD5 (value, strlen (value), OutputBuffer); Nsmutablestring*outputstring = [[Nsmutablestring alloc] Initwithcapacity:cc_md5_digest_length *2];  for(Nsinteger count =0; Count < Cc_md5_digest_length; count++) {[outputstring AppendFormat:@"%02x", Outputbuffer[count]]; }    return[outputstring autorelease];}2, Base64+ (NSString *) Base64encodedata: (NSData *) Objdata {ConstUnsignedChar* Objrawdata =[objdata bytes];Char*ObjPointer;Char*strresult;//Get the Raw data length and ensure we actually have dataintIntlength =[objdata length];if(Intlength = =0)returnNil;//Setup the string-based Result placeholder and pointer within that placeholderStrresult = (Char*) Calloc ((Intlength +2) /3) *4,sizeof(Char)); ObjPointer=strresult;//Iterate through everything while(Intlength >2) {//keep going until we have less than bits*objpointer++ = _base64encodingtable[objrawdata[0] >>2];*objpointer++ = _base64encodingtable[((objrawdata[0] &0x03) <<4) + (objrawdata[1] >>4)];*objpointer++ = _base64encodingtable[((objrawdata[1] &0x0f) <<2) + (objrawdata[2] >>6)];*objpointer++ = _base64encodingtable[objrawdata[2] &0x3f];//we just handled 3 octets (bits) of dataObjrawdata + =3; Intlength-=3;}//Now deal with the tail end of thingsif(Intlength! =0) {*objpointer++ = _base64encodingtable[objrawdata[0] >>2];if(Intlength >1) {*objpointer++ = _base64encodingtable[((objrawdata[0] &0x03) <<4) + (objrawdata[1] >>4)];*objpointer++ = _base64encodingtable[(objrawdata[1] &0x0f) <<2];*objpointer++ ='=';} Else {*objpointer++ = _base64encodingtable[(objrawdata[0] &0x03) <<4];*objpointer++ ='=';*objpointer++ ='=';}}//Terminate the string-based result*objpointer =' /'; NSString*rststr =[NSString Stringwithcstring:strresult encoding:nsasciistringencoding];    Free (ObjPointer); returnrststr;}3, AES-(nsdata*) Encryptaes: (NSString *) Key {Charkeyptr[kcckeysizeaes256+1]; Bzero (Keyptr,sizeof(keyptr)); [Key getcstring:keyptr MaxLength:sizeof(KEYPTR) encoding:nsutf8stringencoding]; Nsuinteger datalength=[self length]; size_t buffersize= Datalength +kCCBlockSizeAES128; void*buffer =malloc (buffersize); size_t numbytesencrypted=0; Cccryptorstatus Cryptstatus=Cccrypt (Kccencrypt, kCCAlgorithmAES128, kccoptionpkcs7padding|Kccoptionecbmode, Keyptr, kCCBlockSizeAES128,                                          NULL, [self bytes], datalength, Buffer, buffersize,&numbytesencrypted); if(Cryptstatus = =kccsuccess) {        return[NSData Datawithbytesnocopy:buffer length:numbytesencrypted];    } free (buffer); returnNil;}4, RSA-(NSData *) Encryptwithdata: (NSData *) content {size_t Plainlen=[content length]; if(Plainlen >Maxplainlen) {NSLog (@"content (%LD) is too long, must <%LD", Plainlen, Maxplainlen); returnNil; }    void*plain =malloc (Plainlen);    [Content Getbytes:plain Length:plainlen]; size_t Cipherlen= -;//currently RSA key length is set to bytes    void*cipher =malloc (Cipherlen); Osstatus ReturnCode=Seckeyencrypt (PublicKey, kSecPaddingPKCS1, Plain, Plainlen, cipher,&Cipherlen); NSData*result =Nil; if(ReturnCode! =0) {NSLog (@"Seckeyencrypt fail. Error Code:%ld", ReturnCode); }    Else{result=[NSData Datawithbytes:cipher Length:cipherlen];    } free (plain);    Free (cipher); returnresult;} Well, this is the article, if you want to know more, you can follow Sina Blog: blog.sina.com/zhongxiaoqiu

Common iOS encryption algorithms

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.