title: Web service to Xcode encryption
body: <p>I am trying encrypt data on an iPhone and send up the encrypted text to a web service for them to decrypt it. If the decryption works then it returns the First name in the xml as a confirmation things worked. Here is my Xcode </p>
  
  <p>Note: <em>The 'key' is the same in both xcode and web service</em></p>
  
  <p>The information I want encrypted:</p>
  
  <pre><code>NSString *fnameencrypted = [[NSString alloc] AES256EncryptWithKey:f_name.text withKey:key]];    
  NSString *lnameencrypted = [[NSString alloc] AES256EncryptWithKey:l_name.text withKey:key]];
  </code></pre>
  
  <p>The NSString method</p>
  
  <pre><code>-(NSString *)AES256EncryptWithKey:(NSString *)plaintext withKey:(NSString *)key{
      NSData *plainData = [plaintext dataUsingEncoding:NSASCIIStringEncoding];
      NSData *encryptedData = [plainData AES256EncryptWithKey:key];  
      NSString *encryptedString = [encryptedData base64Encoding];
      return encryptedString;
  }
  </code></pre>
  
  <p><strong><em>EDIT</em></strong></p>
  
  <p>The encryption method</p>
  
  <pre><code>-(NSData *)AES256EncryptWithKey:(NSString *)key{
      char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
      bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
  
      // fetch key data
      [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSASCIIStringEncoding];
  
      NSUInteger dataLength = [self length];
  
      //See the doc: For block ciphers, the output size will always be less than or 
      //equal to the input size plus the size of one block.
      //That's why we need to add the size of one block here
      size_t bufferSize = dataLength + kCCBlockSizeAES128;
      void *buffer = malloc( bufferSize );
  
      size_t numBytesEncrypted = 0;
  
      CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode + kCCOptionPKCS7Padding,
                                        keyPtr, kCCKeySizeAES256,
                                        NULL /* initialization vector (optional) */,
                                        [self bytes], dataLength, /* input */
                                        buffer, bufferSize, /* output */
                                        &amp;numBytesEncrypted);
      if(cryptStatus == kCCSuccess){
          //the returned NSData takes ownership of the buffer and will free it on deallocation
          return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
      }
  
      free(buffer); //free the buffer
      return nil;
  }
  </code></pre>
  
  <p>Here is my web service code</p>
  
  <pre><code>private static string Decrypt(string encryptedText, string completeEncodedKey, int keySize)
  {
      RijndealManaged aesEncryption = new RijndealManaged();
      aesEncryption.KeySize = keySize; //keySize is 256
      aesEncryption.BlockSize = 128;
      aesEncryption.Mode = CipherMode.ECB;
      aesEncryption.Padding = PaddingMode.PKCS7;
      aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.ACSII.GetString(Convert.FromBase64String(completeEncodedString)).Split(',')[0]);
      aesEncryption.Key = Convert.FromBase64String(ASCIIEncoding.ACSII.GetString(Convert.FromBase64String(completeEncodedString)).Split(',')[1]);
      ICryptoTransform decrypto = aesEncryption.CreateDecryptor();
      byte[] encryptedBytes = Convert.FromBase64CharArray(encryptedText.ToCharArray(), 0, encryptedText.Length);
      return ASCIIEncoding.ASCII.GetString(decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
  }
  </code></pre>
  
  <p>This code does not work because it returns </p>
  
  <pre><code>&lt;response&gt;&lt;return&gt;0&lt;/return&gt;&lt;/response&gt;
  </code></pre>