Having trouble decrypting in C# something encrypted on iPhone using RSA
I've spent two days on this so far and combed through every source at my disposal, so this is the last resort.

I have an X509 certificate whose public key I have stored in the iPhone's keychain (simulator only at this point). On the ASP.NET side, I've got the certificate in the cert store with a private key. When I encrypt a string on the iPhone and decrypt it on the server, I get a `CryptographicException` "Bad data."

I have compared the base-64 strings on both sides and they're equal. I've compared the raw byte arrays after decoding and they too are equal. If I encrypt on the server using the public key, the byte array is different from the iPhone's version and readily decrypts using the private key. The raw plaintext string is 115 characters so it's within the 256-byte limitation of my 2048-bit key.

Here's the iPhone encryption method (pretty much verbatim from the CryptoExercise sample app):

    + (NSData *)encrypt:(NSString *)plainText usingKey:(SecKeyRef)key error:(NSError **)err
    {
    	size_t cipherBufferSize = SecKeyGetBlockSize(key);
    	uint8_t *cipherBuffer = NULL;
    	cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));
    	memset((void *)cipherBuffer, 0x0, cipherBufferSize);
    	NSData *plainTextBytes = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    	OSStatus status = SecKeyEncrypt(key, kSecPaddingNone,
    		    		    		(const uint8_t *)[plainTextBytes bytes], 
    		    		    		[plainTextBytes length], cipherBuffer, 
    		    		    		&cipherBufferSize);
    	if (status == noErr)
    	{
    		NSData *encryptedBytes = [[[NSData alloc]
    		    		initWithBytes:(const void *)cipherBuffer 
    		    		    		    		length:cipherBufferSize] autorelease];
    		if (cipherBuffer)
    		{
    			free(cipherBuffer);
    		}
    		NSLog(@"Encrypted text (%d bytes): %@",
    		    		[encryptedBytes length], [encryptedBytes description]);
    		return encryptedBytes;
    	}
    	else
    	{
    		*err = [NSError errorWithDomain:@"errorDomain" code:status userInfo:nil];
    		NSLog(@"encrypt:usingKey: Error: %d", status);
    		return nil;
    	}
    }

And here's the server-side C# decryption method:

    private string Decrypt(string cipherText)
    {
    	if (clientCert == null)
    	{
    		// Get certificate
    		var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
    		store.Open(OpenFlags.ReadOnly);
    		foreach (var certificate in store.Certificates)
    		{
    			if (certificate.GetNameInfo(X509NameType.SimpleName, false) == CERT)
    			{
    				clientCert = certificate;
    				break;
    			}
    		}
	
    		if (clientCert == null)
    		{
    			throw(new ApplicationException("Certificate not found in store."));
    		}
    	}

    	using (var rsa = (RSACryptoServiceProvider)clientCert.PrivateKey)
    	{
    		try
    		{
    			var encryptedBytes = Convert.FromBase64String(cipherText);
    			var decryptedBytes = rsa.Decrypt(encryptedBytes, false);
    			var plaintext = Encoding.UTF8.GetString(decryptedBytes);
    			return plaintext;
    		}
    		catch (CryptographicException e)
    		{
    			throw(new ApplicationException("Unable to decrypt payload.", e));
    		}
    		catch (Exception e)
    		{
    			throw(new ApplicationException("Unable to init params.", e));
    		}
    	}
    }

My suspicion was that there was some encoding problems between the platforms. I know that one is big-endian and the other is little-endian but I don't know enough to say which is which or how to overcome the difference.
Having trouble decrypting in C# something encrypted on iPhone using RSA
===

I've spent two days on this so far and combed through every source at my disposal, so this is the last resort.

I have an X509 certificate whose public key I have stored in the iPhone's keychain (simulator only at this point). On the ASP.NET side, I've got the certificate in the cert store with a private key. When I encrypt a string on the iPhone and decrypt it on the server, I get a `CryptographicException` "Bad data."

I have compared the base-64 strings on both sides and they're equal. I've compared the raw byte arrays after decoding and they too are equal. If I encrypt on the server using the public key, the byte array is different from the iPhone's version and readily decrypts using the private key. The raw plaintext string is 115 characters so it's within the 256-byte limitation of my 2048-bit key.

Here's the iPhone encryption method (pretty much verbatim from the CryptoExercise sample app):

    + (NSData *)encrypt:(NSString *)plainText usingKey:(SecKeyRef)key error:(NSError **)err
    {
    	size_t cipherBufferSize = SecKeyGetBlockSize(key);
    	uint8_t *cipherBuffer = NULL;
    	cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));
    	memset((void *)cipherBuffer, 0x0, cipherBufferSize);
    	NSData *plainTextBytes = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    	OSStatus status = SecKeyEncrypt(key, kSecPaddingNone,
    		    		    		(const uint8_t *)[plainTextBytes bytes], 
    		    		    		[plainTextBytes length], cipherBuffer, 
    		    		    		&cipherBufferSize);
    	if (status == noErr)
    	{
    		NSData *encryptedBytes = [[[NSData alloc]
    		    		initWithBytes:(const void *)cipherBuffer 
    		    		    		    		length:cipherBufferSize] autorelease];
    		if (cipherBuffer)
    		{
    			free(cipherBuffer);
    		}
    		NSLog(@"Encrypted text (%d bytes): %@",
    		    		[encryptedBytes length], [encryptedBytes description]);
    		return encryptedBytes;
    	}
    	else
    	{
    		*err = [NSError errorWithDomain:@"errorDomain" code:status userInfo:nil];
    		NSLog(@"encrypt:usingKey: Error: %d", status);
    		return nil;
    	}
    }

And here's the server-side C# decryption method:

    private string Decrypt(string cipherText)
    {
    	if (clientCert == null)
    	{
    		// Get certificate
    		var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
    		store.Open(OpenFlags.ReadOnly);
    		foreach (var certificate in store.Certificates)
    		{
    			if (certificate.GetNameInfo(X509NameType.SimpleName, false) == CERT)
    			{
    				clientCert = certificate;
    				break;
    			}
    		}
	
    		if (clientCert == null)
    		{
    			throw(new ApplicationException("Certificate not found in store."));
    		}
    	}

    	using (var rsa = (RSACryptoServiceProvider)clientCert.PrivateKey)
    	{
    		try
    		{
    			var encryptedBytes = Convert.FromBase64String(cipherText);
    			var decryptedBytes = rsa.Decrypt(encryptedBytes, false);
    			var plaintext = Encoding.UTF8.GetString(decryptedBytes);
    			return plaintext;
    		}
    		catch (CryptographicException e)
    		{
    			throw(new ApplicationException("Unable to decrypt payload.", e));
    		}
    		catch (Exception e)
    		{
    			throw(new ApplicationException("Unable to init params.", e));
    		}
    	}
    }

My suspicion was that there was some encoding problems between the platforms. I know that one is big-endian and the other is little-endian but I don't know enough to say which is which or how to overcome the difference.