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

📄

📁 一个加密解密示例算法的一个Vc++实现。 网络字符串加密时可以使用。
💻
字号:
#define _WIN32_WINNT 0x0400
#include <stdio.h> 
#include <windows.h>
#include <wincrypt.h> 
#include <tlhelp32.h> 
#pragma comment (lib,"Advapi32.lib")
#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
#define KEYLENGTH  0x00800000

void HandleError(char *s);

//--------------------------------------------------------------------
//   The following #define statements are also required.

#define ENCRYPT_ALGORITHM CALG_RC4 
#define ENCRYPT_BLOCK_SIZE 8 

//--------------------------------------------------------------------
//    Declare the function DecryptFile. Code for the function follows
//    main.

BOOL DecryptFile(
				 PCHAR szSource, 
				 PCHAR szDestination, 
				 PCHAR szPassword); 

void main(void) 
{ 
	//--------------------------------------------------------------------
	//    Declare and initialize variables.
	
	CHAR szSource[100]; 
	CHAR szDestination[100]; 
	CHAR szPassword[100]; 
	
	printf("Decrypt a file. \n\n");
	printf("Enter the name of the file to be decrypted: ");
	scanf("%s",szSource);
	printf("Enter the name of the output file: ");
	scanf("%s",szDestination);
	printf("Enter the password:");
	scanf("%s",szPassword);
	
	if(!DecryptFile(szSource, szDestination, szPassword))
	{
		printf("\nError decrypting file. \n"); 
	}
	else
	{ 
		printf("\nDecryption of file %s succeeded. \n", szSource);
		printf("The decrypted file is %s .\n",szDestination);
	}
} // End of main

//--------------------------------------------------------------------
//    Define the function Decryptfile.

static BOOL DecryptFile(
						PCHAR szSource, 
						PCHAR szDestination, 
						PCHAR szPassword) 
{ 
	//--------------------------------------------------------------------
	//   Declare and initialize local variables.
	
	FILE *hSource; 
	FILE *hDestination; 
	
	HCRYPTPROV hCryptProv; 
	HCRYPTKEY hKey; 
	HCRYPTHASH hHash; 
	
	PBYTE pbBuffer; 
	DWORD dwBlockLen; 
	DWORD dwBufferLen; 
	DWORD dwCount; 
	
	BOOL status = FALSE; 
	
	//--------------------------------------------------------------------
	// Open source file. 
	if(!(hSource = fopen(szSource,"rb"))) 
	{
		HandleError("Error opening ciphertext file!");
	}
	//--------------------------------------------------------------------
	// Open destination file. 
	
	if(!(hDestination = fopen(szDestination,"wb")))
	{
		HandleError("Error opening plaintext file!");
	} 
	//--------------------------------------------------------------------
	// Get a handle to the default provider. 
	if(!CryptAcquireContext(
		&hCryptProv, 
		NULL, 
		NULL, 
		PROV_RSA_FULL, 
		0))
	{
		HandleError("Error during CryptAcquireContext!"); 
	}
	
	//--------------------------------------------------------------------
	// Decrypt the file with a session key derived from a password. 
	
	//--------------------------------------------------------------------
	// Create a hash object. 
	if(!CryptCreateHash(
		hCryptProv, 
		CALG_MD5, 
		0, 
		0, 
		&hHash))
	{
		HandleError("Error during CryptCreateHash!");
	}
	//--------------------------------------------------------------------
	// Hash in the password data. 
	
	if(!CryptHashData(
		hHash, 
		(BYTE *)szPassword, 
		strlen(szPassword), 
		0)) 
	{
		HandleError("Error during CryptHashData!"); 
	}
	//--------------------------------------------------------------------
	// Derive a session key from the hash object. 
	
	if(!CryptDeriveKey(
		hCryptProv, 
		ENCRYPT_ALGORITHM, 
		hHash, 
		KEYLENGTH, 
		&hKey))
	{ 
		HandleError("Error during CryptDeriveKey!"); 
	}
	//--------------------------------------------------------------------
	// Destroy the hash object. 
	
	CryptDestroyHash(hHash); 
	hHash = 0; 
	
	//--------------------------------------------------------------------
	//   The decryption key is now available, either having been imported
	//   from a BLOB read in from the source file or having been created 
	//   using the password. This point in the program is not reached if 
	//   the decryption key is not available.
	
	//--------------------------------------------------------------------
	// Determine the number of bytes to decrypt at a time. 
	// This must be a multiple of ENCRYPT_BLOCK_SIZE. 
	
	dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE; 
	dwBufferLen = dwBlockLen; 
	
	//--------------------------------------------------------------------
	// Allocate memory. 
	
	if(!(pbBuffer = (BYTE *)malloc(dwBufferLen)))
	{
		HandleError("Out of memory!\n"); 
	}
	//--------------------------------------------------------------------
	// Decrypt source file, and write to destination file. 
	
	do { 
		//--------------------------------------------------------------------
		// Read up to dwBlockLen bytes from source file. 
		
		dwCount = fread(
			pbBuffer, 
			1, 
			dwBlockLen, 
			hSource); 
		if(ferror(hSource))
		{
			HandleError("Error reading ciphertext!");
		}
		//--------------------------------------------------------------------
		// Decrypt data. 
		if(!CryptDecrypt(
			hKey, 
			0, 
			feof(hSource), 
			0, 
			pbBuffer, 
			&dwCount))
		{
			HandleError("Error during CryptDecrypt!"); 
		}
		//--------------------------------------------------------------------
		// Write data to destination file. 
		
		fwrite(
			pbBuffer, 
			1, 
			dwCount, 
			hDestination); 
		if(ferror(hDestination))
		{
			HandleError("Error writing plaintext!"); 
		}
	} while(!feof(hSource)); 
	status = TRUE; 
	
	//--------------------------------------------------------------------
	// Close files. 
	if(hSource) 
		fclose(hSource); 
	if(hDestination) 
		fclose(hDestination); 
	
	//--------------------------------------------------------------------
	// Free memory. 
	
	if(pbBuffer) 
		free(pbBuffer); 
	
	//--------------------------------------------------------------------
	// Destroy session key. 
	
	if(hKey) 
		CryptDestroyKey(hKey); 
	
	//--------------------------------------------------------------------
	// Destroy hash object. 
	if(hHash) 
		CryptDestroyHash(hHash); 
	
	//--------------------------------------------------------------------
	// Release provider handle. 
	
	if(hCryptProv) 
		CryptReleaseContext(hCryptProv, 0); 
	
	return status;
} // End of Decryptfile

//--------------------------------------------------------------------
//  This example uses the function HandleError, a simple error
//  handling function, to print an error message to the standard error 
//  (stderr) file and exit the program. 
//  For most applications, replace this function with one 
//  that does more extensive error reporting.

void HandleError(char *s)
{
    fprintf(stderr,"An error occurred in running the program. \n");
    fprintf(stderr,"%s\n",s);
    fprintf(stderr, "Error number %x.\n", GetLastError());
    fprintf(stderr, "Program terminating. \n");
    exit(1);
} // End of HandleError

⌨️ 快捷键说明

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