Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

API code help about converting C# code to Python!!!!

Banned
Banned
Joined
Jan 25, 2023
Messages
125
Reaction score
36
The conversion does not work. Every time I run Python, it shows an error that there is no data in the database.
Please help me out! I am providing C# code. I can write to the database using Winform, but not Python.

C#:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace RegisterAccount
{
    public class MainForm : Form
    {
        private TextBox tbLogin;
        private TextBox tbPwd;
        private TextBox tbConfirm;
        private TextBox tbEmail; // 新增的邮箱文本框
        private TextBox tbPhone; // 新增的电话号码文本框
        private Button btnRegister;
        private Label lblLogin;
        private Label lblPwd;
        private Label lblConfirm;
        private Label lblEmail;
        private Label lblPhone;
        private Label label1;
        private static readonly Regex PwdRegex = new Regex("^[\\x20-\\xFF]{4,16}$");

        public MainForm()
        {
            InitializeComponent();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (tbConfirm.Text != tbPwd.Text)
            {
                MessageBox.Show("密码与确认密码不符!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                byte[] value = null;
                string encryptPwd = string.Empty;
                try
                {
                    value = GetAccountPasswordHash(tbConfirm.Text);
                    encryptPwd = "0x" + BitConverter.ToString(value).Replace("-", "");
                }
                catch (ArgumentException)
                {
                    MessageBox.Show("密码必须是4至16个字母或数字!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                using (SqlConnection sqlConnection = new SqlConnection("Server=192.168.200.6;Database=AionAccounts;User Id=sa;Password=123456;Connection Timeout=200"))
                {
                    sqlConnection.Open();
                    using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
                    {
                        sqlCommand.CommandText = "agent_CreateAccount";
                        sqlCommand.CommandType = CommandType.StoredProcedure;
                        sqlCommand.Parameters.AddWithValue("@ggid", Guid.NewGuid());
                        sqlCommand.Parameters.AddWithValue("@account", tbLogin.Text);
                        sqlCommand.Parameters.Add("@password", SqlDbType.VarBinary).Value = GetAccountPasswordHash(tbConfirm.Text);
                        sqlCommand.Parameters.AddWithValue("@email", tbEmail.Text); // 使用用户输入的邮箱
                        sqlCommand.Parameters.AddWithValue("@mobile", tbPhone.Text); // 使用用户输入的电话号码
                        sqlCommand.Parameters.AddWithValue("@question1", string.Empty);
                        sqlCommand.Parameters.AddWithValue("@question2", string.Empty);
                        sqlCommand.Parameters.AddWithValue("@answer1", new byte[1]);
                        sqlCommand.Parameters.AddWithValue("@answer2", new byte[1]);
                        SqlParameter sqlParameter = sqlCommand.Parameters.Add("@ReturnVal", SqlDbType.Int);
                        sqlParameter.Direction = ParameterDirection.ReturnValue;
                        sqlCommand.ExecuteNonQuery();
                        int num = (int)sqlParameter.Value;
                        if (num == 0)
                        {
                            MessageBox.Show("账户创建失败!登录名已存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else if (num == 8)
                        {
                            MessageBox.Show("账户创建失败!邮箱地址已存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else if (num == 9)
                        {
                            MessageBox.Show("账户创建失败!手机号码已存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else
                        {
                            MessageBox.Show(string.Format("账户创建成功!", num), "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }
                    
                    }
                }
            }
        }

        private void InitializeComponent()
        {
            this.tbLogin = new System.Windows.Forms.TextBox();
            this.tbPwd = new System.Windows.Forms.TextBox();
            this.tbConfirm = new System.Windows.Forms.TextBox();
            this.tbEmail = new System.Windows.Forms.TextBox();
            this.tbPhone = new System.Windows.Forms.TextBox();
            this.btnRegister = new System.Windows.Forms.Button();
            this.lblLogin = new System.Windows.Forms.Label();
            this.lblPwd = new System.Windows.Forms.Label();
            this.lblConfirm = new System.Windows.Forms.Label();
            this.lblEmail = new System.Windows.Forms.Label();
            this.lblPhone = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // tbLogin
            //
            this.tbLogin.Location = new System.Drawing.Point(100, 20);
            this.tbLogin.Name = "tbLogin";
            this.tbLogin.Size = new System.Drawing.Size(150, 21);
            this.tbLogin.TabIndex = 1;
            //
            // tbPwd
            //
            this.tbPwd.Location = new System.Drawing.Point(100, 50);
            this.tbPwd.Name = "tbPwd";
            this.tbPwd.PasswordChar = '*';
            this.tbPwd.Size = new System.Drawing.Size(150, 21);
            this.tbPwd.TabIndex = 3;
            //
            // tbConfirm
            //
            this.tbConfirm.Location = new System.Drawing.Point(100, 80);
            this.tbConfirm.Name = "tbConfirm";
            this.tbConfirm.PasswordChar = '*';
            this.tbConfirm.Size = new System.Drawing.Size(150, 21);
            this.tbConfirm.TabIndex = 5;
            //
            // tbEmail
            //
            this.tbEmail.Location = new System.Drawing.Point(100, 110);
            this.tbEmail.Name = "tbEmail";
            this.tbEmail.Size = new System.Drawing.Size(150, 21);
            this.tbEmail.TabIndex = 7;
            //
            // tbPhone
            //
            this.tbPhone.Location = new System.Drawing.Point(100, 140);
            this.tbPhone.Name = "tbPhone";
            this.tbPhone.Size = new System.Drawing.Size(150, 21);
            this.tbPhone.TabIndex = 9;
            //
            // btnRegister
            //
            this.btnRegister.Location = new System.Drawing.Point(83, 210);
            this.btnRegister.Name = "btnRegister";
            this.btnRegister.Size = new System.Drawing.Size(100, 30);
            this.btnRegister.TabIndex = 10;
            this.btnRegister.Text = "注册";
            this.btnRegister.Click += new System.EventHandler(this.btnRegister_Click);
            //
            // lblLogin
            //
            this.lblLogin.AutoSize = true;
            this.lblLogin.Location = new System.Drawing.Point(20, 20);
            this.lblLogin.Name = "lblLogin";
            this.lblLogin.Size = new System.Drawing.Size(59, 12);
            this.lblLogin.TabIndex = 0;
            this.lblLogin.Text = "游戏账号:";
            //
            // lblPwd
            //
            this.lblPwd.AutoSize = true;
            this.lblPwd.Location = new System.Drawing.Point(20, 50);
            this.lblPwd.Name = "lblPwd";
            this.lblPwd.Size = new System.Drawing.Size(59, 12);
            this.lblPwd.TabIndex = 2;
            this.lblPwd.Text = "游戏密码:";
            //
            // lblConfirm
            //
            this.lblConfirm.AutoSize = true;
            this.lblConfirm.Location = new System.Drawing.Point(20, 80);
            this.lblConfirm.Name = "lblConfirm";
            this.lblConfirm.Size = new System.Drawing.Size(59, 12);
            this.lblConfirm.TabIndex = 4;
            this.lblConfirm.Text = "确认密码:";
            //
            // lblEmail
            //
            this.lblEmail.AutoSize = true;
            this.lblEmail.Location = new System.Drawing.Point(20, 110);
            this.lblEmail.Name = "lblEmail";
            this.lblEmail.Size = new System.Drawing.Size(59, 12);
            this.lblEmail.TabIndex = 6;
            this.lblEmail.Text = "电子邮箱:";
            //
            // lblPhone
            //
            this.lblPhone.AutoSize = true;
            this.lblPhone.Location = new System.Drawing.Point(20, 140);
            this.lblPhone.Name = "lblPhone";
            this.lblPhone.Size = new System.Drawing.Size(59, 12);
            this.lblPhone.TabIndex = 8;
            this.lblPhone.Text = "安全密码:";
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.Color.Transparent;
            this.label1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.ForeColor = System.Drawing.Color.Red;
            this.label1.Location = new System.Drawing.Point(18, 176);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(275, 19);
            this.label1.TabIndex = 11;
            this.label1.Text = "注意:安全密码用于密码重置及找回密码!";
            this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            //
            // MainForm
            //
            this.ClientSize = new System.Drawing.Size(315, 252);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.lblLogin);
            this.Controls.Add(this.tbLogin);
            this.Controls.Add(this.lblPwd);
            this.Controls.Add(this.tbPwd);
            this.Controls.Add(this.lblConfirm);
            this.Controls.Add(this.tbConfirm);
            this.Controls.Add(this.lblEmail);
            this.Controls.Add(this.tbEmail);
            this.Controls.Add(this.lblPhone);
            this.Controls.Add(this.tbPhone);
            this.Controls.Add(this.btnRegister);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "账号注册程序";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public static byte[] GetAccountPasswordHash(string input)
        {

            byte[] array = new byte[17];
            byte[] array2 = new byte[17];
            byte[] bytes = Encoding.ASCII.GetBytes(input);
            for (int i = 0; i < input.Length; i++)
            {
                array[i + 1] = bytes[i];
                array2[i + 1] = array[i + 1];
            }
            long num = (long)((ulong)array[1] + (ulong)array[2] * 256UL + (ulong)array[3] * 65536UL + (ulong)array[4] * 16777216UL);
            long num2 = num * 213119L + 2529077L;
            num2 -= num2 / 4294967296L * 4294967296L;
            num = (long)((ulong)array[5] + (ulong)array[6] * 256UL + (ulong)array[7] * 65536UL + (ulong)array[8] * 16777216UL);
            long num3 = num * 213247L + 2529089L;
            num3 -= num3 / 4294967296L * 4294967296L;
            num = (long)((ulong)array[9] + (ulong)array[10] * 256UL + (ulong)array[11] * 65536UL + (ulong)array[12] * 16777216UL);
            long num4 = num * 213203L + 2529589L;
            num4 -= num4 / 4294967296L * 4294967296L;
            num = (long)((ulong)array[13] + (ulong)array[14] * 256UL + (ulong)array[15] * 65536UL + (ulong)array[16] * 16777216UL);
            long num5 = num * 213821L + 2529997L;
            num5 -= num5 / 4294967296L * 4294967296L;
            array[4] = (byte)(num2 / 16777216L);
            array[3] = (byte)((num2 - (long)((int)array[4] * 16777216)) / 65536L);
            array[2] = (byte)((num2 - (long)((int)array[4] * 16777216) - (long)((int)array[3] * 65536)) / 256L);
            array[1] = (byte)(num2 - (long)((int)array[4] * 16777216) - (long)((int)array[3] * 65536) - (long)((int)array[2] * 256));
            array[8] = (byte)(num3 / 16777216L);
            array[7] = (byte)((num3 - (long)((ulong)array[8] * 16777216UL)) / 65536L);
            array[6] = (byte)((num3 - (long)((ulong)array[8] * 16777216UL) - (long)((int)array[7] * 65536)) / 256L);
            array[5] = (byte)(num3 - (long)((ulong)array[8] * 16777216UL) - (long)((int)array[7] * 65536) - (long)((int)array[6] * 256));
            array[12] = (byte)(num4 / 16777216L);
            array[11] = (byte)((num4 - (long)((ulong)array[12] * 16777216UL)) / 65536L);
            array[10] = (byte)((num4 - (long)((ulong)array[12] * 16777216UL) - (long)((int)array[11] * 65536)) / 256L);
            array[9] = (byte)(num4 - (long)((ulong)array[12] * 16777216UL) - (long)((int)array[11] * 65536) - (long)((int)array[10] * 256));
            array[16] = (byte)(num5 / 16777216L);
            array[15] = (byte)((num5 - (long)((ulong)array[16] * 16777216UL)) / 65536L);
            array[14] = (byte)((num5 - (long)((ulong)array[16] * 16777216UL) - (long)((int)array[15] * 65536)) / 256L);
            array[13] = (byte)(num5 - (long)((ulong)array[16] * 16777216UL) - (long)((int)array[15] * 65536) - (long)((int)array[14] * 256));
            array2[1] = (byte)(array2[1] ^ array[1]);
            int j = 1;
            while (j < 16)
            {
                j++;
                array2[j] = (byte)(array2[j] ^ array2[j - 1] ^ array[j]);
            }
            j = 0;
            while (j < 16)
            {
                j++;
                bool flag2 = array2[j] == 0;
                if (flag2)
                {
                    array2[j] = 102;
                }
            }
            byte[] array3 = new byte[16];
            Buffer.BlockCopy(array2, 1, array3, 0, 16);
            return array3;
        }

        public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "

        {
            string hexString = string.Empty;
            if (bytes != null)
            {
                System.Text.StringBuilder strB = new System.Text.StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    strB.Append(bytes[i].ToString("X2"));
                }
                hexString = strB.ToString();
            }
            return hexString;
        }

        public static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
        }
    }
}
Python:
Python:
from flask import Flask, request, jsonify
import re
import hashlib
import pyodbc
import uuid

app = Flask(__name__)

@app.route('/register', methods=['POST'])
def register():
    data = request.get_json()
    account = data['account']
    pwd = data['password']
    confirm = data['confirm']
    email = data['email']
    phone = data['phone']

    if confirm != pwd:
        return jsonify({'error': 'Password and confirm password do not match'})

    if not re.match("^[\\x20-\\xFF]{4,16}$", pwd):
        return jsonify({'error': 'Password must be 4 to 16 alphanumeric characters'})

    password_hash = get_account_password_hash(confirm)

    conn_str = 'DRIVER={SQL Server};SERVER=192.168.200.6;DATABASE=AionAccounts;UID=sa;PWD=123456'
    conn = pyodbc.connect(conn_str)
    cursor = conn.cursor()

    try:
        cursor.execute("EXEC agent_CreateAccount @ggid=?, @account=?, @password=?, @email=?, @mobile=?, @question1=?, @question2=?, @answer1=?, @answer2=?",
                       (str(uuid.uuid4()), account, password_hash, email, phone, '', '', bytearray([0]), bytearray([0])))
        conn.commit()
        return jsonify({'message': 'Account created successfully'})
    except pyodbc.Error as e:
        error_message = str(e)
        if '登录名已存在' in error_message:
            return jsonify({'error': 'Account creation failed! Account name already exists'})
        elif '邮箱地址已存在' in error_message:
            return jsonify({'error': 'Account creation failed! Email address already exists'})
        elif '手机号码已存在' in error_message:
            return jsonify({'error': 'Account creation failed! Phone number already exists'})
        else:
            return jsonify({'error': 'Account creation failed!'})

def get_account_password_hash(input):
    array = bytearray([0] * 17)
    array2 = bytearray([0] * 17)
    bytes = input.encode('ascii')
    for i in range(len(input)):
        array[i + 1] = bytes[i]
        array2[i + 1] = array[i + 1]
    
    num = int.from_bytes(array[1:5], byteorder='little')
    num2 = num * 213119 + 2529077
    num2 -= num2 // 4294967296 * 4294967296
    num = int.from_bytes(array[5:9], byteorder='little')
    num3 = num * 213247 + 2529089
    num3 -= num3 // 4294967296 * 4294967296
    num = int.from_bytes(array[9:13], byteorder='little')
    num4 = num * 213203 + 2529589
    num4 -= num4 // 4294967296 * 4294967296
    num = int.from_bytes(array[13:17], byteorder='little')
    num5 = num * 213821 + 2529997
    num5 -= num5 // 4294967296 * 4294967296

    array[4] = (num2 // 16777216) & 0xFF
    array[3] = (num2 // 65536) & 0xFF
    array[2] = (num2 // 256) & 0xFF
    array[1] = num2 & 0xFF
    array[8] = (num3 // 16777216) & 0xFF
    array[7] = (num3 // 65536) & 0xFF
    array[6] = (num3 // 256) & 0xFF
    array[5] = num3 & 0xFF
    array[12] = (num4 // 16777216) & 0xFF
    array[11] = (num4 // 65536) & 0xFF
    array[10] = (num4 // 256) & 0xFF
    array[9] = num4 & 0xFF
    array[16] = (num5 // 16777216) & 0xFF
    array[15] = (num5 // 65536) & 0xFF
    array[14] = (num5 // 256) & 0xFF
    array[13] = num5 & 0xFF

    for j in range(1, 17):
        array2[j] = array2[j] ^ array2[j - 1] ^ array[j]

    for j in range(16):
        if array2[j] == 0:
            array2[j] = 102

    return array2[1:].hex()

if __name__ == '__main__':
    app.run()
 
Back
Top