title: AES string encryption in Objective-C
body: <p>My Objective-C App requires text / string encryption (specifically <a href="/questions/tagged/nsstring" class="post-tag" title="show questions tagged 'nsstring'" rel="tag">nsstring</a>). </p>
  
  <p>I know AES is the most secure encryption method available for consumer use. I also understand how to convert strings to data and back... (just a beginner). </p>
  
  <p>Many webpages and Q/As about encryption with AES are unclear, and none of them state how to use the code given. For example, a webpage might say: "here is the code... here is what it does..." but no explanation for how to use it. </p>
  
  <p>I've found this code through lots of research:</p>
  
  <pre><code>#import "&lt;CommonCrypto/CommonCryptor.h&gt;"
  @implementation NSMutableData(AES)
  </code></pre>
  
  <p>For encryption:</p>
  
  <pre><code>- (NSMutableData*) EncryptAES:(NSString *)key {
      char keyPtr[kCCKeySizeAES256+1];
      bzero( keyPtr, sizeof(keyPtr) );
  
      [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF16StringEncoding];
      size_t numBytesEncrypted = 0;
  
      NSUInteger dataLength = [self length];
  
      size_t bufferSize = dataLength + kCCBlockSizeAES128;
      void *buffer = malloc(bufferSize);
  
      NSMutableData *output = [[NSData alloc] init];
  
      CCCryptorStatus result = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL, [self mutableBytes], [self length], buffer, bufferSize, &amp;numBytesEncrypted);
  
      output = [NSMutableData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
  
      if(result == kCCSuccess) {
          return output;
      }
          return NULL;
      }
  </code></pre>
  
  <p>For Decryption:</p>
  
  <pre><code>- (NSMutableData*)DecryptAES: (NSString*)key andForData:(NSMutableData*)objEncryptedData {
  
      char  keyPtr[kCCKeySizeAES256+1];
      bzero( keyPtr, sizeof(keyPtr) );
  
      [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF16StringEncoding];
  
      size_t numBytesEncrypted = 0;
  
      NSUInteger dataLength = [self length];
  
      size_t bufferSize = dataLength + kCCBlockSizeAES128;
      void *buffer_decrypt = malloc(bufferSize);    
      NSMutableData *output_decrypt = [[NSData alloc] init];
      CCCryptorStatus result = CCCrypt(kCCDecrypt , kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL, [self mutableBytes], [self length], buffer_decrypt, bufferSize, &amp;numBytesEncrypted);
  
      output_decrypt = [NSMutableData dataWithBytesNoCopy:buffer_decrypt length:numBytesEncrypted];
  
      if(result == kCCSuccess) {
          return output_decrypt;
      } 
          return NULL;
      }
  }
  </code></pre>
  
  <p>This is the code I made that I would like to correspond with the above code:</p>
  
  <pre><code>- (void)Encrypt {
      //Convert NSString to NSData so that it can be used to encrypt the Input
      NSString *Input  = [Inputbox text];
      NSData *InputData = [Input dataUsingEncoding:NSUTF8StringEncoding];
      //What to do here
  }
  </code></pre>
  
  <p>How do I use this code, these methods? Where does it go in my Implementation file?</p>