public byte[] EncryptAssymetricByPublic(byte[] toEncrypt, String xmlPublic) { //Encode with public key RSACryptoServiceProvider rsaPublic = new RSACryptoServiceProvider(); rsaPublic.FromXmlString(xmlPublic); SymmetricKey symmetricKey = GenerateSymmetricKey(); byte[] encryptedKey = rsaPublic.Encrypt(symmetricKey.toByteArray(), false); byte[] encrypted = EncryptSymmetric(toEncrypt, symmetricKey); byte[] combined = new byte[2 + encryptedKey.Length + encrypted.Length]; combined[0] = (byte)(encryptedKey.Length & 0x00FF); combined[1] = (byte)(encryptedKey.Length >> 8); for (int i = 0; i < encryptedKey.Length; i++) { combined[2 + i] = encryptedKey[i]; } for (int i = 0; i < encrypted.Length; i++) { combined[2 + i + encryptedKey.Length] = encrypted[i]; } return(combined); }