private void WriteAllPRofiles(object sender, EventArgs e) { if ((_currentConnectoinContext != null)) { _authorizeCheckBehavior = true; for (int index = 0; index < profilesListBox.Items.Count; index++) { profilesListBox.SetItemCheckState(index, CheckState.Checked); } _authorizeCheckBehavior = false; try { RG_ENDPOINT portEndpoin = _currentConnectoinContext.ReaderPort; byte address = _currentConnectoinContext.ReaderAddress; uint errorCode = UnmanagedContext.Instance.RG_ClearProfiles(ref portEndpoin, address); if (errorCode != 0) { throw new ApiCallException("Ошибка при очистке профилей", errorCode); } RG_PROFILE_DATA apiProfileData = new RG_PROFILE_DATA(); apiProfileData.AccessFlags = 0; apiProfileData.MfKey = new byte[6]; apiProfileData.MfAesKey = new byte[16]; for (int index = 0; index < profilesListBox.Items.Count; index++) { ProfileData localData = _defaultProfiles[index]; var accessFlag = (localData.CryptoOneKeyB ? 1 : 0) | ((localData.AesKeyB ? 1 : 0) << 1); apiProfileData.AccessFlags = Convert.ToByte(accessFlag); Buffer.BlockCopy(localData.CryptoOneKey, 0, apiProfileData.MfKey, 0, apiProfileData.MfKey.Length); Buffer.BlockCopy(localData.AesKey, 0, apiProfileData.MfAesKey, 0, apiProfileData.MfAesKey.Length); errorCode = UnmanagedContext.Instance.RG_WriteProfile(ref portEndpoin, address, Convert.ToByte(index), localData.BlockNumber, ref apiProfileData); if (errorCode != 0) { throw new ApiCallException("Ошибка при записи профиля.", errorCode); } } MessageBox.Show(this, "Команда выполнена успешно.", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(this, string.Format("({1}) {0}", ex.Message, (ex is ApiCallException) ? (ex as ApiCallException).ApiCallErrorCode.ToString() : "..."), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void WriteSelectedProfiles(object sender, EventArgs e) { if ((_currentConnectoinContext != null)) { try { if (profilesListBox.CheckedItems.Count > 0) { RG_ENDPOINT portEndpoin = _currentConnectoinContext.ReaderPort; byte address = _currentConnectoinContext.ReaderAddress; uint errorCode = UnmanagedContext.Instance.RG_ClearProfiles(ref portEndpoin, address); if (errorCode != 0) { throw new ApiCallException("Ошибка при очистке профилей", errorCode); } RG_PROFILE_DATA apiProfileData = new RG_PROFILE_DATA(); apiProfileData.AccessFlags = 0; apiProfileData.MfKey = new byte[6]; apiProfileData.MfAesKey = new byte[16]; byte profileIndex = 0; byte[] accesFlagsStorage = new byte[1]; foreach (var checkedProfile in profilesListBox.CheckedItems) { ProfileData localData = checkedProfile as ProfileData; BitArray accessFlagsArray = new BitArray(8); switch (localData.ProfileType) { case ProfileType.Mifare: accessFlagsArray.Set(0, localData.CryptoOneKeyB); accessFlagsArray.Set(1, localData.AesKeyB); accessFlagsArray.Set(5, false); accessFlagsArray.Set(6, false); break; case ProfileType.AppleGooglePay: case ProfileType.MobileAccess: accessFlagsArray.Set(0, localData.KeyBitGenerated); accessFlagsArray.Set(1, localData.KeyBitDirect); accessFlagsArray.Set(2, localData.KeyBitEmited); accessFlagsArray.Set(5, localData.ProfileType == ProfileType.AppleGooglePay); accessFlagsArray.Set(6, localData.ProfileType != ProfileType.AppleGooglePay); break; } accessFlagsArray.CopyTo(accesFlagsStorage, 0); apiProfileData.AccessFlags = accesFlagsStorage[0]; Buffer.BlockCopy(localData.CryptoOneKey, 0, apiProfileData.MfKey, 0, apiProfileData.MfKey.Length); Buffer.BlockCopy(localData.AesKey, 0, apiProfileData.MfAesKey, 0, apiProfileData.MfAesKey.Length); errorCode = UnmanagedContext.Instance.RG_WriteProfile(ref portEndpoin, address, profileIndex++, localData.BlockNumber, ref apiProfileData); if (errorCode != 0) { throw new ApiCallException("Ошибка при записи профиля.", errorCode); } } MessageBox.Show(this, "Команда выполнена успешно.", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show(this, string.Format("({1}) {0}", ex.Message, (ex is ApiCallException) ? (ex as ApiCallException).ApiCallErrorCode.ToString() : "..."), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }