如何使用OpenSSL证书加密数据在Microsoft Visual C#中创建MySQL连接? [英] How do I Create a MySQL connection in Microsoft Visual C#, using OpenSSL Certificates to Encrypt Data?

查看:109
本文介绍了如何使用OpenSSL证书加密数据在Microsoft Visual C#中创建MySQL连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我试图解决,现在我需要帮助。



我正在为一个类的软件项目工作。我是一群8人。我们正在开发一款将在Unity游戏引擎内运行的视频游戏。启动器是用Microsoft Visual C#编写的。游戏分为两半:一个启动器(客户端)和游戏本身,作为计算机上的本地服务器运行。其他人可以从他们的客户连接到其他服务器以获得多玩家的乐趣。我负责我们的发射器。我们在使用Ubuntu Linux Server 12.04的虚拟专用服务器上运行MySQL数据库。它使用标准的MySQL端口3306.作为该类要求的一部分,我们必须使用某种形式的加密。我们选择使用OpenSSL创建证书颁发机构(CA),然后使用CA证书创建服务器和客户端证书。



我在MySQL中创建了一个用户需要SSL加密连接。我还创建了CA,服务器证书和客户端证书,使用CA对它们进行签名。如果我使用来自另一台机器的mysql命令行客户端(如我笔记本电脑上的Linux虚拟机)连接到服务器上的MySQL,如果我指定SSL参数ca-cert,ssl-cert和ssl-key,那么我可以正确登录需要SSL加密连接的MySQL用户。



在MS Windows 7中使用时,我使用OpenSSL转换签名服务器和客户端证书PEM(通常的OpenSSL格式)使用PKCS12选项到PFX。我将这些文件导入了我的Windows 7虚拟机。在Visual Studio的启动器项目中,我使用 Mysql.Data.MySqlClient

I have an issue that I have tried to resolve and now I need assistance.

I am working on a software project for a class. I'm in a group of 8 people. We are working on a video game that will run inside the Unity game engine. The launcher is being written in Microsoft Visual C#. The game is divided into two halves: a launcher(the client) and the game itself, running as a local server on the computer. Other people can connect from their clients to others servers for multi-player fun. I am responsible for our launcher. We have a MySQL database running on a virtual private server that uses Ubuntu Linux Server 12.04. It is using the standard MySQL port 3306. As part of the requirements for the class, we have to use some form of encryption. We opted to use OpenSSL to create a Certificate Authority (CA) and then use the CA certificate to create a server and client certificate.

I created a user in MySQL that requires SSL encrypted connections. I also created the CA, the server certificate, and the client certificate, using the CA to sign both of them. If I connect to MySQL on the server using the mysql command-line client from another machine (like my Linux virtual machine on my laptop), if I specify the SSL parameters ca-cert, ssl-cert, and ssl-key, then I can properly log in as the MySQL user that requires SSL-encrypted connections.

For use in MS Windows 7, I used OpenSSL to convert the signed server and client certificates from PEM(usual OpenSSL format) to PFX using the PKCS12 option. I imported these files to my Windows 7 virtual machine. In the launcher project in Visual Studio, I imported the appropriate library:

导入了相应的库:

, then I added the code to open a connection to our database.

When I test the code, I get an error message:

"An unhandled exception of type 'System.IO.IOException' occurred in MySql.Data.dll
Additional information: The handshake failed due to an unexpected packet format."

Notice it is not a MySQLException thrown, which leads me to assume its something with the certificates. I followed the official documentation from the OpenSSL project and from Oracle to setup the database and to generate and convert the certificates.

This is not my first time developing a program to communicate with a MySQL database, but this is my first time having to do anything with SSL and certificates. I do not know how to go about resolving this. Any assistance would be very appreciated.

Here is the code segment where the IOException is thrown:

public DBClient()
        {

            Initialize();

        }

        private void Initialize()
        {

            connectionString = "HOST=" + mySQLServer + ";"
                             + "DATABASE=" + dbName + ";"
                             + "UID=" + uid + ";"
                             + "PASSWORD=" + passwd + ";"
                             + "SslMode=Required;"
                             + "CertificateFile=C:\\Project\\signed-server-cert.pfx;";

            sqlConn = new MySqlConnection(connectionString);
            sqlComm = new MySqlCommand();

            try
            {

                sqlConn.Open(); //THIS IS WHERE THE IOEXCEPTION IS THROWN!!!
                sqlComm.Connection = sqlConn;

            }
            catch (MySqlException sqle)
            {

                MessageBox.Show("ERROR: Could not connect to database. \n\n Error Message:  " + sqle.Message + "\n\n", "Register Form", MessageBoxButtons.OK);

            }

推荐答案

这篇关于如何使用OpenSSL证书加密数据在Microsoft Visual C#中创建MySQL连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆