0x01 关于思路

代码是用C#写的,核心思路是末尾两端的数据是存储水卡的数据
首4位是存储水卡数据,倒数四位是校验码计算后的结果 偏移量82.然后首位4位中12和34位置的数据位置互换。

0x02 结语

喜欢一句话:技术不分对错.人性才分善恶.
这个仅供学习测试,勿用于实际。否则后果自负!!

0x03 核心代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WaterCard
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;

        }
        int MaxMoney = 1000000;//100000为1k
        int MinMoney = 100;//必须以100为倍数
        /// <summary>
        /// 字符串转16进制
        /// </summary>
        /// <param name="num">字符串</param>
        /// <returns></returns>
        private string IntToHex(int num)
        {
            string result = num.ToString("X"); //转16进制
            return result;
        }
        /// <summary>
        /// 16进制转10进制
        /// </summary>
        /// <param name="num">16进制</param>
        /// <returns></returns>
        private int HexToInt(string num)
        {
            int result = 0;
            result = int.Parse(num, System.Globalization.NumberStyles.AllowHexSpecifier);//16转10
            return result;
        }
        /// <summary>
        /// 从右开始读取除F以外的字符
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        private string RightReadString(string num)
        {
            string result="",tmp="";
            for (int i = num.Length - 1; i >= 0; i--)
            {
                if(num.Substring(i, 1)!="F")
                tmp += num.Substring(i, 1);
            }
            for (int i = tmp.Length - 1; i >= 0; i--)
            {
                if (tmp.Substring(i, 1) != "F")
                    result += tmp.Substring(i, 1);
            }
            return result;
        }
        private void txtMoney_KeyDown(object sender, KeyEventArgs e)
        {
            bool shiftKey = (Keyboard.Modifiers & ModifierKeys.Shift) != 0;//判断shifu键是否按下
            if (shiftKey == true)                  //当按下shift
            {
                e.Handled = true;//不可输入
            }
            else                           //未按shift
            {
                if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.Tab || e.Key == Key.Enter || e.Key==Key.Decimal))
                {
                    e.Handled = true;//不可输入
                }
                else if(e.Key == Key.Enter )
                {
                    string Money = txtMoney.Text.ToString().Trim().Replace(".", "");
                    if (int.Parse(Money) > MaxMoney)//大于
                    {
                        txtMoney.Text = (MinMoney / 100).ToString() + ".00";
                        Money = MinMoney.ToString();
                        Money = txtMoney.Text.ToString().Trim().Replace(".", "");
                    }
                    string Money_body_Value = "110B1709320808";//总共32个字符  这个最长14位
                    string Money_head_Value = "";//最长12位
                    string Money_bottom_Value = "";//最长6位
                    string Money_Value = "";
                    if (Money != "")
                    {
                        string Money16_Sub1 = "";
                        string Money16_Sub2 = "";
                        string Zero = "0";
                        
                        int Money10 = int.Parse(Money);
                        
                        string Money16 = IntToHex(Money10);
                        
                        int Money16_Len = Money16.Length.GetHashCode();
                        
                        int Money_bottom_Value_SloveData = 0;
                        //
                        if (Money16_Len%2==0)
                        {
                            for (int i = 0; i < Money16_Len - 2; i += 2)
                            {
                                Money16_Sub2 = Money16.Substring(i, 2) + Money16_Sub2;
                                Money_bottom_Value_SloveData = Money_bottom_Value_SloveData + HexToInt(Money16.Substring(i, 2));
                            }
                            
                            Money16_Sub1 = Money16.Substring(Money16_Len-2, 2);
                            
                            Money_bottom_Value_SloveData = Money_bottom_Value_SloveData + HexToInt(Money16_Sub1);
                            
                        }
                        else
                        {
                            Money16 = "0"+Money16;
                            
                            Money16_Len = Money16.Length.GetHashCode();
                            
                            for (int i = 0; i <Money16_Len-2; i +=2)
                            {
                                Money16_Sub2 =  Money16.Substring(i, 2)+ Money16_Sub2;
                                Money_bottom_Value_SloveData = Money_bottom_Value_SloveData + HexToInt(Money16.Substring(i, 2));
                            }
                            
                            Money16_Sub1 = Money16.Substring(Money16_Len-2, 2);
                            
                            Money_bottom_Value_SloveData = Money_bottom_Value_SloveData + HexToInt(Money16_Sub1);
                        }
                        Money_bottom_Value_SloveData -= 130;//校验码计算
                        
                        string Money_bottom_Value_SloveData_Int16 = IntToHex(Money_bottom_Value_SloveData);//校验码计算 130是10进制 82 16进制
                        
                        Money_bottom_Value = RightReadString(Money_bottom_Value_SloveData_Int16);
                        
                        Money_head_Value = Money16_Sub1 + Money16_Sub2;
                        
                        int Money_bottom_Value_Len = Money_bottom_Value.Length.GetHashCode();
                        
                        int Money_head_Value_Len = Money_head_Value.Length.GetHashCode();

                        
                        if (Money_bottom_Value_Len<6)
                        {
                            for(int i=0;i<6- Money_bottom_Value_Len;i++)
                            {
                                Money_bottom_Value = Zero + Money_bottom_Value;
                            }
                        }
                        
                        if (Money_head_Value_Len<12)
                        {
                            for (int i = 0; i < 12 - Money_head_Value_Len; i++)
                            {
                                Money_head_Value +=Zero;
                            }
                        }
                        
                        Money_Value = Money_head_Value + Money_body_Value + Money_bottom_Value;
                        
                        txtMoney_Value.Text = Money_Value;
              
                    }
                }
            }
        }

        private void form1_Loaded(object sender, RoutedEventArgs e)
        {
            textBlock_info.Text += "最大上限值为:"+(MaxMoney/100.0).ToString()+"元"+"超过这个值的大小则生成的为"+ (MinMoney / 100.0).ToString() + "元的值";
        }
    }
}