原文url: http://www.bigsea.com.cn/archives/821/
zt自己动手用工行U盾加密自己私有文件
No Comments | 安全技术 | by bigsea | 4788 Views. | 2008, November 26, 5:59 PM 本帖子原来是fleshwound的投稿文章,被root转入论坛。
作者:ecceccecc
URL:http://www.smatrix.org
网上的文件加密 软件多如牛毛,文件加密的核心是对对称密钥的保护。网上基于口令的文件加密很弱的,自己动手用工行u盾 可以做一个文件加密程序,绝对的安全。AES大家goole上一找就行了。对称密钥核心部分用下面的代码就可以了。
#include <windows.h>
#include <tchar.h>
#include <wincrypt.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
HCRYPTPROV hProv;
HCRYPTKEY hKey;
BYTE *pbKeyBlob = NULL;
DWORD dwBlobLen;
BYTE *pbData = NULL;
DWORD cbData;
DWORD i;
//LPTSTR MSCSP=TEXT("Logoxin CSP v1.0");
//LPSTR MSACSP="Logoxin CSP v1.0";
LPTSTR MSCSP=TEXT("SafeSign CSP Version 1.0");
LPSTR MSACSP="SafeSign CSP Version 1.0";
BYTE pbBuffer[128];
BYTE *pbConst= (BYTE *)"The data that is to be Encrypted.";
DWORD dwBufferLen = (DWORD)strlen((char *)pbConst)+1;
memcpy(pbBuffer,pbConst,dwBufferLen);
if(!CryptAcquireContext(&hProv, NULL, MSCSP, PROV_RSA_FULL, 0))
printf("Error %x CryptAcquireContext!n", GetLastError());
if(!CryptGetProvParam(hProv, PP_ENUMCONTAINERS, pbData, &cbData, CRYPT_FIRST))
printf("Error %x CryptGetProvParam!n", GetLastError());
pbData = new BYTE[cbData];
//Get the Container Name
if(!CryptGetProvParam(hProv, PP_ENUMCONTAINERS, pbData, &cbData, CRYPT_FIRST))
printf("Error %x CryptGetProvParam!n", GetLastError());
//ReleaseContext
if(!CryptReleaseContext(hProv,0))
printf("Error %x during ReleaseContext!n", GetLastError());
//Get the Container
if(!CryptAcquireContextA(&hProv, (LPCSTR)pbData, MSACSP, PROV_RSA_FULL, 0))
printf("Error %x CryptAcquireContext!n", GetLastError());
if(!CryptGetUserKey(hProv,AT_KEYEXCHANGE,&hKey))
printf("Error %x CryptGetUserKey!n", GetLastError());
// Determine the size of the key BLOB and allocate memory.
if(!CryptExportKey(hKey, 0, PUBLICKEYBLOB , 0, NULL, &dwBlobLen))
printf("Error %x computing BLOB length!n", GetLastError());
if((pbKeyBlob = (BYTE *)malloc(dwBlobLen)) == NULL)
printf("Out of memory!n");
// Export the key into a simple key BLOB.
if(!CryptExportKey(hKey, 0, PUBLICKEYBLOB , 0, pbKeyBlob, &dwBlobLen))
printf("Error %x during CryptExportKey!n", GetLastError());
//Encrypt PlainText
if(!CryptEncrypt(hKey,NULL,TRUE,0,pbBuffer,&dwBufferLen,128))
printf("Error %x during CryptEncrypt!n", GetLastError());
printf("the Ciper isn");
for(i=0;i<dwBufferLen;i++)
printf("%02X",pbBuffer);
printf("n");
//Decrypt CipherText
if(!CryptDecrypt(hKey,NULL,TRUE,0,pbBuffer,&dwBufferLen))
printf("Error %x during CryptDecrypt!n", GetLastError());
printf("the Plain isn");
for(i=0;i<dwBufferLen;i++)
printf("%c",pbBuffer);
printf("n");
if(hKey)
{
if(!CryptDestroyKey(hKey))
printf("Error %x during CryptDestoryKey!n", GetLastError());
}
//ReleaseContext
if(!CryptReleaseContext(hProv,0))
printf("Error %x during ReleaseContext!n", GetLastError());
return 1;
}
(你可以随意转载,但请保留原作者信息,有任何关于密码学和USBKEY,网银的问题可以来交流!
by ecceccecc)