C/C++如何连接MySQL服务器以及简单加密
下面先給出MySQL連接前的官方文檔
Application?programs?should?use?this?general?outline?for?interacting?with?MySQL:
1.?Initialize?the?MySQL?library?by?calling?mysql_library_init().?This?function?exists?in?both?the?libmysqlclient?C?client?library?and?the?libmysqldembedded?server?library,?so?it?is?used?whether?you?build?a?regular?client?program?by?linking?with?the?-libmysqlclient?flag,?or?an?embedded?server?application?by?linking?with?the?-libmysqld?flag.
2.?Initialize?a?connection?handler?by?calling?mysql_init()?and?connect?to?the?server?by?calling?mysql_real_connect().
3.Issue?SQL?statements?and?process?their?results.?(The?following?discussion?provides?more?information?about?how?to?do?this.)
4.Close?the?connection?to?the?MySQL?server?by?calling?mysql_close().
5.End?use?of?the?MySQL?library?by?calling?mysql_library_end().
可能有朋友會(huì)問,為什么要這么怎么做,為什么要做這么多步驟。
我給出的答案是,用某個(gè)東西就是更具官方給出的規(guī)則進(jìn)行使用,可以不必知道為什么要這么做,但必須要遵守規(guī)則。
由此我們易知首先得mysql_library_init()
然后mysql_init()和mysql_real_connect();
最后為mysql_close()和mysql_library_end();
下面是代碼
#include<stdio.h> #include<stdlib.h> #include<WinSock2.h> #include<mysql.h> #pragma comment(lib, "libmysql") int main() {if (mysql_library_init(0, NULL, NULL)){printf_s("could?not?initialize?MySQL?library\n");getchar();exit(1);}MYSQL conn;mysql_init(&conn);MYSQL *ret = mysql_real_connect(&conn, "127.0.0.1", "root", "123456", "demo", 0, NULL, 0);if (!ret){printf_s("Failed?to?connect?to?database:??%s\n", mysql_error(&conn));getchar();exit(1);}printf_s("successful database connection\n");mysql_close(&conn);mysql_library_end();getchar();return 0; }
運(yùn)行結(jié)果如下:
但我們進(jìn)入debug目錄,可以看到exe文件,我們打開他,直接對(duì)字符串進(jìn)行搜索,可以得到如下圖所示類容
這是非常不安全的。
雖然,當(dāng)任意用戶登錄數(shù)據(jù)庫時(shí)無需檢驗(yàn)(這些帳號(hào)和密碼是可以暴露的)
但我覺得這是一個(gè)有瑕疵的,所以我們對(duì)他進(jìn)行簡(jiǎn)單加密。
如下代碼所示:
#include <stdio.h> #include <stdlib.h> #include <WinSock2.h> #include <mysql.h> #pragma comment(lib, "libmysql")//簡(jiǎn)單加密 char* encrypt(char *Code, int Judge) {int CodeLen = strlen(Code);//1為ip地址解密if (Judge == 1){for (int i = 0; i < CodeLen; i++){if (i == 3 || i == 5 || i == 7)continue;Code[i] -= 2;}}//用戶名解密else if (Judge == 2){for (int i = 0; i < CodeLen; i++)Code[i] -= 3;}//密碼解密else if (Judge == 3){for (int i = 0; i < CodeLen; i++)Code[i] -= 1;}//表名加密else{for (int i = 0; i < CodeLen; i++)Code[i] -= 1;}return Code; } int main() {char host[10] = "349.2.2.3";char user[5] = "urrw";char passwd[7] = "234567";char db[5] = "efnp";if (mysql_library_init(0, NULL, NULL)){printf("could not initialize mysql library\n");exit(1);};MYSQL conn;mysql_init(&conn);MYSQL* ret = mysql_real_connect(&conn, encrypt(host, 1), encrypt(user, 2), encrypt(passwd, 3), encrypt(db, 4), 0, NULL, 0);if (!ret){printf_s("failed to connect to database: %s\n",mysql_error(&conn));}printf_s("successful database connection\n");mysql_close(&conn);mysql_library_end();getchar();return 0; }運(yùn)行結(jié)果如下:
現(xiàn)在我們打開exe文件,看看他所處的字符串的地方變成了什么值
如下圖所示:
總結(jié)
以上是生活随笔為你收集整理的C/C++如何连接MySQL服务器以及简单加密的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php mysql odbc_javas
- 下一篇: mysql中的联结_MySQL的联结(J