原创:星绽紫辉 转载请注明出处,3Q Very Much! http://www.cppblog.com/rawdata 我写的MD5和RC4加密缓冲区函数,其原始代码借鉴 www.microsoft.com 网站上的文章。
这种加密函数是引用Windows内部维护的CSP数据库的,是机器相关的,换句话说,你只有在同一台机器上才能对它进行加密解密。加密前,你给一个私钥字符串,就可以加密和解密缓冲区了。也可以稍加修改用来加密解密文件。
其实,里面的步骤是死的,至于为什么这么做,这是这些API编写者对API应用者要求的必须遵循的调用规则,如果你想获得更为详尽的信息,可以在微软的网站上找到有关CSP和Encryt/Decrypt API的资料。
下面贴出完整的源代码: (呵呵,顺便说一句,我是支持开源的。这对于加速软件技术研究是有帮助的,我期待着高智能机器人时代的快速来临,我希望有健全完整的共享知识库,我也希望发达健全的软件系统能有一个质的飞跃。)
http://www.cppblog.com/rawdata
1 2 #pragma once 3 4 #include <tchar.h> 5 #include <stdio.h> 6 #include <windows.h> 7 #include <wincrypt.h> 8 9 #pragma comment (lib, "advapi32") 10 11 #define KEYLENGTH 0x00800000 12 #define ENCRYPT_ALGORITHM CALG_RC4 13 #define ENCRYPT_BLOCK_SIZE 8 14 15 16 //--------------------------------------------------------------------------------------- 17 18 #pragma pack(push,1) 19 struct DEFKEY 20  { 21 HCRYPTPROV hCryptProv; 22 HCRYPTKEY hKey; 23 HCRYPTHASH hHash; 24 DEFKEY() 25 { 26 hCryptProv = NULL; 27 hKey = NULL; 28 hHash = NULL; 29 } 30 }; 31 #pragma pack(pop) 32 33 //-------------------------------------------------------------------------------------- 34 35 //初始化Key 36 void InitEncrypt(DEFKEY& initkey,BYTE* pbyPassword,DWORD dwPwdLen) 37  { 38 //请求CSP 39 if(!CryptAcquireContext( &initkey.hCryptProv, NULL,MS_ENHANCED_PROV,PROV_RSA_FULL,0)) 40 return; 41 42 //创建散列 43 if(!CryptCreateHash(initkey.hCryptProv,CALG_MD5,0,0,&initkey.hHash)) 44 return; 45 46 //生成密钥数据 47 if(!CryptHashData(initkey.hHash,pbyPassword,dwPwdLen,0)) 48 return; 49 50 //生成密钥(密钥长度,密钥算法) 51 if(!CryptDeriveKey(initkey.hCryptProv,ENCRYPT_ALGORITHM,initkey.hHash,KEYLENGTH,&initkey.hKey)) 52 return; 53 54 } 55 56 57 //------------------------------------------------------------------------------------------------ 58 void InitDecrypt(DEFKEY& initkey,BYTE* pbyPassword,DWORD dwPwdLen) 59  { 60 //是否是加密尾部块 61 bool fReturn = false; 62 63 //请求CSP 64 if(!CryptAcquireContext(&initkey.hCryptProv,NULL,MS_ENHANCED_PROV,PROV_RSA_FULL,0)) 65 return; 66 67 //创建散列表 68 if(!CryptCreateHash(initkey.hCryptProv,CALG_MD5,0,0,&initkey.hHash)) 69 return; 70 71 //创建解密数据 72 if(!CryptHashData(initkey.hHash,(BYTE *)pbyPassword,dwPwdLen,0)) 73 return; 74 75 //生成解密数据 76 if(!CryptDeriveKey(initkey.hCryptProv,ENCRYPT_ALGORITHM, 77 initkey.hHash,KEYLENGTH,&initkey.hKey)) 78 return; 79 } 80 81 //----------------------------------------------------------------------------------- 82 void ReleaseCrypt(DEFKEY& initkey) 83  { 84 if(initkey.hHash) 85 { 86 CryptDestroyHash(initkey.hHash); 87 initkey.hHash = NULL; 88 } 89 90 if(initkey.hKey) 91 CryptDestroyKey(initkey.hKey); 92 93 if(initkey.hCryptProv) 94 CryptReleaseContext(initkey.hCryptProv, 0); 95 } 96 97 //------------------------------------------------------------------------------------- 98 99 100 bool EncryptBuffer(BYTE* pbyInputBuf, DWORD dwInputLen, 101 DWORD dwBufLen,HCRYPTKEY hKey,BOOL bEnd); 102 103 104 bool DecryptBuffer(BYTE* pbyInputBuf, DWORD dwInputLen, 105 DWORD dwBufLen,HCRYPTKEY hKey,BOOL bEnd); 106 107 108 //--------------------------------------------------------------------------------------- 109 bool EncryptBuffer(BYTE* pbyInputBuf, DWORD dwInputLen, 110 DWORD dwBufLen,HCRYPTKEY hKey,BOOL bEnd) 111  { 112 DWORD dwCount = dwInputLen; 113 CryptDecrypt(hKey,0,bEnd,0,pbyInputBuf,&dwCount); 114 return 1; 115 } 116 117 118 119 120 bool DecryptBuffer(BYTE* pbyInputBuf, DWORD dwInputLen, 121 DWORD dwBufLen,HCRYPTKEY hKey,BOOL bEnd) 122  { 123 DWORD dwCount = dwInputLen; 124 CryptDecrypt(hKey,0,bEnd,0,pbyInputBuf,&dwCount); 125 return 1; 126 }
http://www.cppblog.com/rawdata 如果代码有什么谬误或者有更好的解决方案,请留言或者EmailToMe: xiaolu69soft@yahoo.com.cn. 让我们一起努力, 让软件技术和电子机械技术的高能时代快快来临吧!!! ^_~ 2008-12-30 星绽紫辉
|