Using MD5 encryption in a Linux database

Source: Internet
Author: User
Tags md5 md5 encryption sprintf

MD5 encryption algorithm source code download: Https://pan.baidu.com/s/1nwyN0xV

After the download is complete, unzip and get two files

Environment Construction:

1. Copy the Md5.h file to the/usr/include/directory

sudo cp md5.h/usr/include

2. Compile and generate. o Files

Gcc-fpic-o md5.o-c MD5.C-LPTHREAD-LDL

3. Compile and generate. so files

Gcc-shared-fpic-o libmd5.so MD5.O-LPTHREAD-LDL

4. Copy all generated. So files in the/lib/directory for easy use

sudo cp libmd5.so/lib/

Where we used the data encryption function in the MD5

void Md5str (charChar *output); Parameters: Input: Data output to encrypt      : encrypted data

The program mysqlite3.c as follows:

1#include <sqlite3.h>2#include <stdio.h>3#include <string.h>4#include"md5.h"5 6 /*use a callback function query when defining this macro or simply query with a non-callback function*/7 //#define CALLBACK8 9 /*Define CREATE TABLE directive if not EXISTS: does not exist, AutoIncrement: Automatically increases primary key, NOT NULL: cannot be empty*/Ten #defineCreate "CREATE table IF not EXISTS passwd (ID integer primary key autoincrement,username text not Null,password text not NU LL) " One /*defining query Data directives*/ A #defineSelect "Select * from passwd where username= '%s ' and password= '%s '" - /*Defining Insert Data Directives*/ - #defineInsert "INSERT into passwd (Username,password) VALUES ('%s ', '%s ')" the  - /*If you query to multiple rows of data, the function is called multiple times (one call per row)*/ - intCallbackvoid*arg,intColChar**value,Char**name) - { +     intI=0; -      for(i=0;i<col; i++) +     { Aprintf"%s\t", Value[i]); at     } -printf"\ n"); -     return 0; - } -  -  in intMainvoid) - { to      +     //1. Open the database -Sqlite3 *ppdb =NULL; the     intret = Sqlite3_open ("./PASSWD", &ppdb);/*We're going to create a database named passwd first.*/ *     if(Ret! =SQLITE_OK) $     {Panax NotoginsengPerror ("Open Fail"); -         return-1; the     } +Sqlite3_exec (Ppdb,create,null,null,null);/*Create a table*/ A     Chartemp[ +]; the     Chartemp1[ +]; +     CharInsert[strlen (temp) +strlen (TEMP1) + $];/*the array here is going to be a little bigger, because when you encrypt it, you get a long string of data .*/ -printf"Please input your username:"); $scanf"%s", temp); $printf"Please input your password:"); -scanf"%s", TEMP1); -Md5str (TEMP1,TEMP1);/*Store the entered password in a database table using MD5 encryption*/ theprintf"1111\n"); -     Wuyi      thesprintf (INSERT,INSERT,TEMP,TEMP1);/*package data, ready to be inserted into the table*/ -      Wu sqlite3_exec (ppdb,insert,null,null,null); -      About      $     Charusername[ +]; -     Charpassword[ -]; -printf"input Username:"); -scanf"%s", username); Aprintf"Input Password:"); +scanf"%s", password); the      -Md5str (Password,password);/*use MD5 to encrypt the password in the database table.*/ $     CharSql[strlen (SELECT) +strlen (username) +strlen (password)]; the      the     /*Package A string, place the select string in SQL, and username and password both variables to enlarge the Select*/ the sprintf (Sql,select,username,password); the  -  in #ifdef CALLBACK the     //Callback Query the     Char*selectsql ="SELECT * FROM MyName"; AboutRET =sqlite3_exec (ppdb, Selectsql, callback, NULL, NULL); the     if(Ret! =SQLITE_OK) the     { thePerror ("Create fail"); + Sqlite3_close (ppdb); -         return-1; the     }Bayi      the     //non-callback query the     #else -     Char**result =NULL; -     introw =0; the     intCol =0; the     Char*error =NULL; theret = sqlite3_get_table (ppdb,sql,&result,&row,&col,&error); the     if(Ret! =SQLITE_OK) -     { thePerror ("Get table Fail"); the         return-1; the     }94      the     intI=0, j=0; the      for(i=0; i<row+1; i++) the     {98          for(j=0; j<col;j++) About         { -printf"%s\t", result[j+i*Col]);101         }102printf"\ n");103     }104      the     if(Row >0)/*Data Match Success*/106printf"checked ok\n");107     Else  /*Data match failed*/108printf"fail\n");109Sqlite3_free_table (result);//Release Results the     #endif111      the 113 Sqlite3_close (ppdb); the     return 0; the}
View Code

We will first create a name called passwd database, if you do not understand the creation can look at my article: Linux Database Environment Building

Sqlite3 passwd

Then we compile the program

Gcc-o Mysqlite3 MYSQLITE3.C-LSQLITE3-LMD5

 

The results of the operation are as follows:

Using MD5 encryption in a Linux database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.