Q:

Encrypt using RSA from Swift to C#

I am trying to encrypt username and password and send it to a website with RSA.
I've been searching the web and didn't find any thing.
I get the Public Key from a .Net WebServer in XML formal like this:
<RSAKeyValue>
<Modulus>vB/j1viHNHdSSnD1JwrZKu93GZtXO/oQAzp90w/QRQC7s7RO4PhTcW3ADOUVB1+BlmbaFsEreNUAOV5P4aZh+68T+InwmU1javFsGkjCcVoQO/uEpp2zjrM9Eh84OPaKH429GVmdfTgUj0YbmYVanM3HX4byMH25DKQD687b7x8=
</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>

So I have to encrypt it with this key, I am with XCode 8 and Swift 3.
With .Net C· is very Easy.
RSACryptoServiceProvider encryptKey = new RSACryptoServiceProvider(); 
UnicodeEncoding encoding = new UnicodeEncoding();
encryptKey.FromXmlString(XML PublicKey);
byte[] usr = encryptKey.Encrypt(encoding.GetBytes(txtUser.Text),false);
byte[] pwd = encryptKey.Encrypt(encoding.GetBytes(txtPassword.Text),false);

¿Does any one can help me, please?
thanks
UPDATED
i try this code but with publicKey was generated in Mac OS work fine, but with public key i got from server is not working, i don't know why? @Charles Srstka
let pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDzZtW7ETOkJGTwN/4adYI5oQZ7U7EPzfDtpZTf+cQ9zAcmcC6g6uAC6KuovBSsigcUNzw3s2eNh0RvYBl6ipJ71hH1awTBwVEWo4fl7uIqdpBjwvO1wWXg9UifpvSsV3GPff9YqMvuggDznOGc20CvsXusQKt9dDx8ESxP6yjqiwIDAQAB"
let keyData = NSData(base64Encoded: pubKey, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)            
var dict: [NSString: AnyObject] = [
      kSecClass: kSecClassKey,
        kSecAttrKeyType: kSecAttrKeyTypeRSA,
        kSecAttrKeySizeInBits: pubKey.utf8.count as AnyObject,
        kSecAttrKeyClass: kSecAttrKeyClassPublic,
        kSecAttrApplicationTag: "me.lhu" as AnyObject,
        kSecValueData: keyData as AnyObject,
        kSecReturnRef: true as AnyObject
];            
SecItemDelete(dict as CFDictionary)
var err = SecItemAdd(dict as CFDictionary, nil);
if ((err != noErr)) {
    print("error loading public key");
}        
var resultData: SecKey? = nil
var result: AnyObject?
err = SecItemCopyMatching(dict as CFDictionary, &result)
if err == noErr, let keyRef as! SecKey {       
    let plaintext = tf_taikhoan.text                    
    let plaintextLen = plaintext?.lengthOfBytes(using: String.Encoding.utf8)
    let plaintextBytes = [UInt8](plaintext!.utf8)                    
    var encryptedLen: Int = SecKeyGetBlockSize(keyRef)
    var encryptedBytes = [UInt8](repeating: 0, count: encryptedLen)                    
    err = SecKeyEncrypt(keyRef, SecPadding.PKCS1, plaintextBytes, plaintextLen!, &encryptedBytes, &encryptedLen);
    if (err != noErr) {
        print(encryptedBytes);
    }
}

A:

If you can require macOS 10.12 or iOS 10, use the SecKey APIs:
https://developer.apple.com/library/content/documentation/Security/Conceptual/CertKeyTrustProgGuide/Encryption.html
If you have to support older versions of macOS or iOS, you can do encryption and decryption on macOS/iOS using Security Transforms, for which you can find the documentation here:
https://developer.apple.com/library/content/documentation/Security/Conceptual/SecTransformPG/EncryptionandDecryption/EncryptionandDecryption.html#//apple_ref/doc/uid/TP40010801-CH3-SW1

