⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proc.cpp

📁 关于SQL SERVER 的加密和解密包
💻 CPP
字号:
// proc.cpp
//
// The xp_blowfishdecrypt extended stored procedure
//
// Ver. 0.9.0.0: Updated 1/19/2006 to fix memory fragmentation problems that show up
//               when run on very large datasets
//
#include "../common/stdafx/stdafx.h"
#include "../common/base64/base64.h"
#include "../common/params/params.h"
#include "blowfish.h"

#define XP_NOERROR              0
#define XP_ERROR                1
#define MAXCOLNAME				25
#define MAXNAME					25
#define MAXTEXT					255

#define ERR_USAGE			"Usage:  EXEC xp_blowfishdecrypt @param1, @param2 [, param3 OUTPUT]"
#define ERR_KEY_SIZE		"Key (@param2) must be 32 - 448 bits in length (8 - 56 bytes)"
#define ERR_OUTPUT			"If @param3 is specified, it must be OUTPUT parameter"

#ifdef __cplusplus
extern "C" {
#endif

RETCODE __declspec(dllexport) xp_blowfishdecrypt(SRV_PROC *srvproc);

#ifdef __cplusplus
}
#endif

void PrintError (SRV_PROC *srvproc, char *ErrorMsg);

RETCODE __declspec(dllexport) xp_blowfishdecrypt(SRV_PROC *srvproc)
{
	SRVRETCODE sqlerr = XP_NOERROR;

	int j = params::getparamcount(srvproc);

	params *P1 = new params;
	params *P2 = new params;	
	params *P3 = new params;

	BYTE *text = NULL;
	ULONG textlength = 0;
	BYTE *key = NULL;
	ULONG keylength = 0;

	if (j < 2 || j > 3) {  // Less than 2 or more than 3 parameters
		PrintError (srvproc, ERR_USAGE);
		sqlerr = XP_ERROR;
	} else {
		// Get the parameters
		params::getparam(srvproc, 1, P1); // @param1 contains our encrypted Base64 text
		params::getparam(srvproc, 2, P2); // @param2 is our key
		params::getparam(srvproc, 3, P3); // @param3 is our optional output parameter

		// Get our key length
		keylength = CBase64Utils::CalculateDecodeBufferSize(P2->length);
		key = new BYTE [keylength + 10];
		memset(key, 0,  keylength + 10);

		// Now decode our key from Base64
		keylength = CBase64Utils::Decode(P2->cdata, key, P2->length);

		if (keylength < 8 || keylength > 56) { // Is our keylength between 32 and 448 bits?
			PrintError (srvproc, ERR_KEY_SIZE);
			sqlerr = XP_ERROR;
		} else {
			
			// Now get our text length
			textlength = CBase64Utils::CalculateDecodeBufferSize(P1->length);
			text = new BYTE[textlength+10];
			memset(text, 0, textlength+10);
			textlength = CBase64Utils::Decode(P1->cdata, text, P1->length);
			
			// Now create and initialize the Blowfish object
			Blowfish b;
			b.Set_Passwd((char *)key, keylength);
			b.Decrypt((char *)text, textlength);

			while (textlength > 0 && text [textlength - 1] == 0) {
				textlength--;
			}
			if (j == 2) { // Only two parameters passed, so output a SQL result set
				DBCHAR colname[25];
				_snprintf(colname, 25, "DECRYPTED");
			    srv_describe(srvproc, 1, colname, SRV_NULLTERM, SRVVARCHAR, textlength, SRVVARCHAR, 0, 0);
				srv_setcoldata(srvproc, 1, text);
				srv_setcollen(srvproc, 1, textlength);
				srv_sendrow(srvproc);
				srv_senddone(srvproc, SRV_DONE_MORE | SRV_DONE_COUNT, (DBUSMALLINT)0, (DBINT)1);
			} else { // 3 parameters, so check if @param3 is an output param
				if (!P3->isoutput) {
					PrintError (srvproc, ERR_OUTPUT);
					sqlerr = XP_ERROR;
				} else { // Return the result in @param3
					srv_paramsetoutput(srvproc, 3, text, textlength, P1->isnull);
					srv_senddone(srvproc, SRV_DONE_MORE, (DBUSMALLINT)0, (DBINT)0);
				}
			}
		}
	}
	// Clean-up
	if (text != NULL) {
		memset(text, 0, textlength);
		delete [] text;
		text = NULL;
	}
	if (key != NULL) {
		memset(key, 0, keylength);
		delete [] key;
		key = NULL;
	}
	if (P1 != NULL) {
		delete P1;
		P1 = NULL;
	}
	if (P2 != NULL) {
		delete P2;
		P2 = NULL;
	}
	if (P3 != NULL) {
		delete P3;
		P3 = NULL;
	}
	return sqlerr;
}
// This routine sends an error message to the SQL Server Output Window
//
// Parameters:
//		SRV_PROC *srvproc		The server process pointer passed in by SQL Server
//		char *ErrorMsg			The error message string to print
//
void PrintError (SRV_PROC *srvproc, char *ErrorMsg)
{
	// Print the error message on the SQL Server output screen
	srv_sendmsg(srvproc, SRV_MSG_ERROR, XP_ERROR, SRV_INFO, 1,
		NULL, 0, (DBUSMALLINT) __LINE__, 
		ErrorMsg,
		SRV_NULLTERM);
	// Alert SQL Server that an error has occurred
	srv_senddone(srvproc, (SRV_DONE_ERROR | SRV_DONE_MORE), 0, 0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -