/// <summary> /// Encrypts the ballot selections /// </summary> /// <param name="selections"></param> /// <param name="expectedNumberOfSelected"></param> /// <param name="electionGuardConfig"></param> /// <param name="externalIdentifier"></param> /// <param name="exportPath"></param> /// <param name="exportFileName"></param> /// <returns>EncryptBallotResult containing the encrypted ballot, its external id, its tracker string, /// and the file that includes the ballot</returns> public static EncryptBallotResult EncryptBallot( bool[] selections, int expectedNumberOfSelected, ElectionGuardConfig electionGuardConfig, string externalIdentifier, string exportPath = "./", string exportFileName = "") { var apiConfig = electionGuardConfig.GetApiConfig(); var serializedBytesWithGcHandle = ByteSerializer.ConvertFromBase64String(electionGuardConfig.JointPublicKey); apiConfig.SerializedJointPublicKey = serializedBytesWithGcHandle.SerializedBytes; var encryptedBallotMessage = new SerializedBytes(); var trackerPtr = new IntPtr(); try { var success = API.EncryptBallot( selections.Select(b => (byte)(b ? 1 : 0)).ToArray(), (uint)expectedNumberOfSelected, apiConfig, externalIdentifier, out encryptedBallotMessage, exportPath, exportFileName, out IntPtr outputFilename, out trackerPtr); if (!success) { throw new Exception("ElectionGuardAPI EncryptBallot failed"); } var result = new EncryptBallotResult() { ExternalIdentifier = externalIdentifier, EncryptedBallotMessage = ByteSerializer.ConvertToBase64String(encryptedBallotMessage), Tracker = Marshal.PtrToStringAnsi(trackerPtr), OutputFileName = Marshal.PtrToStringAnsi(outputFilename), }; return(result); } finally { // Free unmanaged memory API.FreeEncryptBallot(encryptedBallotMessage, trackerPtr); serializedBytesWithGcHandle.Handle.Free(); } }