AES symmetric encryption in iOS system

Source: Internet
Author: User

If you don't talk much about it, go directly to the Code:

Header file:

# Import
 
  
@ Class NSString; @ interface NSData (Encryption)-(NSData *) AES256ParmEncryptWithKey :( NSString *) key; // Encryption-(NSData *) AES256ParmDecryptWithKey :( NSString *) key; // decrypt @ end
 


. M implementation file:

# Import "Encryption. h" # import
 
  
@ Implementation NSData (Encryption)-(NSData *) Encryption :( NSString *) key // Encryption {char keyPtr [kCCKeySizeAES256 + 1]; bzero (keyPtr, sizeof (keyPtr )); [key getCString: keyPtr maxLength: sizeof (keyPtr) encoding: Counter]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + counter; void * buffer = malloc (bufferSize ); size_t encrypt = 0; CCCryptorStatus cryptStatus = CCCrypt (kCCEncrypt, encrypt, decrypt | decrypt, keyPtr, decrypt, NULL, [self bytes], dataLength, buffer, bufferSize, & numBytesEncrypted ); if (cryptStatus = kCCSuccess) {return [NSData dataWithBytesNoCopy: buffer length: numBytesEncrypted];} free (buffer); return nil;}-(NSData *) Restart :( NSString *) key // decrypt {char keyPtr [kmeaneysizeaes256 + 1]; bzero (keyPtr, sizeof (keyPtr); [key getCString: keyPtr maxLength: sizeof (keyPtr) encoding: NSUTF8StringEncoding]; NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + buffers; void * buffer = malloc (bufferSize); size_t buffers = 0; CCCryptorStatus cryptStatus = CCCrypt (kCCDecrypt, encrypted, california | kCCOptionECBMode, keyPtr, delimiter, NULL, [self bytes], dataLength, buffer, bufferSize, & bytes); if (cryptStatus = kCCSuccess) {return [NSData dataWithBytesNoCopy: buffer length: numBytesDecrypted];} free (buffer); return nil ;}
 


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.