title: How decrypt string in c# was encrypted in iOS using Rijndael
body: <p>I'm trying to encrypt and decrypt the string using objective c and C#. both are working fine in native code, but when I was try to decrypt string in c# was encrypted in iOS. I get some error.</p>
  
  <p>This was the code I used in the objective c</p>
  
  <pre><code>- (NSData *)AES256EncryptWithKey:(NSString *)key  Data: (NSData *) data
  {
      char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
  
      bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
  
      [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
  
      NSUInteger dataLength = [data length];
  
      NSData *iv =  [@"abcdefghijklmnopqrstuvwxyz123456" dataUsingEncoding:NSUTF8StringEncoding];
  
      size_t bufferSize = dataLength + kCCBlockSizeAES128;
  
      void *buffer = malloc(bufferSize);
  
      size_t numBytesEncrypted = 0;
  
      CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                            keyPtr, kCCKeySizeAES256,
                                            [iv bytes] /* initialization vector (optional) */,
                                            [data bytes], dataLength, /* input */
                                            buffer, bufferSize, /* output */
                                            &amp;numBytesEncrypted);
  
      if (cryptStatus == kCCSuccess)
      {
          return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
      }
  
      free(buffer); //free the buffer;
  
      return nil;
  }
  </code></pre>
  
  <p>In want to know how to decrypt in C#, I give blocksize is 256, ivsize to 32 and used "RijndaelManaged()". I'm not using salt &amp; password.
  Error: something like "Padding is invalid and cannot be removed." 
  I tried to set padding too like PKCS7, none, zero but nothing help to decrypt.</p>
  
  <p>can any one help this?</p>
  
  <p>Edit:
  My C# code here</p>
  
  <pre><code>public string DecryptString(string encrypted)
  {
      string result = null;
      _encoder = new UTF8Encoding();
      if (!string.IsNullOrWhiteSpace(encrypted) &amp;&amp; (encrypted.Length &gt;= 32))
      {
          var messageBytes = Convert.FromBase64String(encrypted);
          using (var rm = new RijndaelManaged())
          { 
              rm.BlockSize = _blockSize;
              rm.Key = _encoder.GetBytes("mykey_here");
              rm.IV = _encoder.GetBytes("abcdefghijklmnopqrstuvwxyz123456"); ;
              rm.Padding = PaddingMode.Zeros;
              var decryptor = rm.CreateDecryptor(rm.Key, messageBytes.Take(_ivSize).ToArray());
              result = _encoder.GetString(Transform(messageBytes.Skip(_ivSize).ToArray(), decryptor));
          }
      }
  
      return result;
  }
  
  protected byte[] Transform(byte[] buffer, ICryptoTransform transform)
  {
      byte[] result;
      using (var stream = new MemoryStream())
      using (var cs = new CryptoStream(stream, transform, CryptoStreamMode.Write))
      {
          cs.Write(buffer, 0, buffer.Length);
          cs.FlushFinalBlock();
          result = stream.ToArray();
      }
  
      return result;
  }
  </code></pre>