是的,例子我也试过的,但是我没有找到一个加密字串的例子,我根据加密文件的例子做的加密字串的程序在解密的时候不对,我想有可能是Key的问题,我把源程序贴上,帮我看看吧
EncryptMessage(LPTSTR szSource,LPTSTR &szDestination, LPTSTR szKey)
{
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
//-----------------------------------------------
//Get the handle to the default provider. 取得一个默认Provider的句柄
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
return false;
}
//--------------------------------------------------------------------
// Create a hash object.
if(!CryptCreateHash(hCryptProv,CALG_MD5, 0, 0, &hHash))
{
return false;
}
//--------------------------------------------------------------------
// Hash the password.
if(!CryptHashData(hHash, (BYTE *)szKey, (DWORD)strlen(szKey), 0))
{
return false;
}
//--------------------------------------------------------------------
// Derive a session key from the hash object.
if(!CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey))
{
return false;
}
//--------------------------------------------------------------------
// Destroy hash object.
if(hHash!= NULL)
{
if(!(CryptDestroyHash(hHash)))
return false;
hHash = 0;
}
//--------------------------------------------------------------------
// The session key is now ready. If it is not a key derived from a
// password, the session key encrypted with the encrypter's private
// key has been returned
// Encrypt data.
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
//--------------------------------------------------------------------
// Determine the block size. If a block cipher is used,
// it must have room for an extra block.
dwBufferLen = dwBlockLen;
//--------------------------------------------------------------------
// Allocate memory.
if(!(pbBuffer = (BYTE *)malloc(dwBufferLen)))
{
return false;
}
strcpy((char*)pbBuffer,szSource);
dwCount = (DWORD)strlen(szSource);
if(!CryptEncrypt(hKey, 0, true, 0, pbBuffer, &dwCount, dwBufferLen))
{
return false;
}
szDestination = (LPTSTR)pbBuffer;
if(hKey != NULL)
{
if(!(CryptDestroyKey(hKey)))
return false;
}
//--------------------------------------------------------------------
// Release the provider handle.
if(hCryptProv !=NULL)
{
if(!(CryptReleaseContext(hCryptProv, 0)))
return false;
}
return true;
}
DecryptMessage(LPTSTR szSource,LPTSTR &szDestination, LPTSTR szKey)
{
//--------------------------------------------------------------------
// Declare and initialize local variables.
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
//--------------------------------------------------------------------
// Get a handle to the default provider.
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
return false;
}
//--------------------------------------------------------------------
// Decrypt the file with a session key derived from a password.
//--------------------------------------------------------------------
// Create a hash object.
if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))
{
return false;
}
//--------------------------------------------------------------------
// Hash in the password data.
if(!CryptHashData(hHash, (BYTE *)szKey, (DWORD)strlen(szKey), 0))
{
return false;
}
//--------------------------------------------------------------------
// Derive a session key from the hash object.
if(!CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey))
{
return false;
}
//--------------------------------------------------------------------
// Destroy the hash object.
if(!(CryptDestroyHash(hHash)))
return false;
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;
static int MessageLength = 0; ///////////////////主要修正处
EncryptMessage(LPTSTR szSource,LPTSTR &szDestination, LPTSTR szKey)
{
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
//-----------------------------------------------
//Get the handle to the default provider. 取得一个默认Provider的句柄
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
return false;
}
//--------------------------------------------------------------------
// Create a hash object.
if(!CryptCreateHash(hCryptProv,CALG_MD5, 0, 0, &hHash))
{
return false;
}
//--------------------------------------------------------------------
// Hash the password.
if(!CryptHashData(hHash, (BYTE *)szKey, (DWORD)strlen(szKey), 0))
{
return false;
}
//--------------------------------------------------------------------
// Derive a session key from the hash object.
if(!CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey))
{
return false;
}
//--------------------------------------------------------------------
// Destroy hash object.
if(hHash!= NULL)
{
if(!(CryptDestroyHash(hHash)))
return false;
hHash = 0;
}
//--------------------------------------------------------------------
// The session key is now ready. If it is not a key derived from a
// password, the session key encrypted with the encrypter's private
// key has been returned
// Encrypt data.
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
//--------------------------------------------------------------------
// Determine the block size. If a block cipher is used,
// it must have room for an extra block.
dwBufferLen = dwBlockLen;
//--------------------------------------------------------------------
// Allocate memory.
if(!(pbBuffer = (BYTE *)malloc(dwBufferLen)))
{
return false;
}
strcpy((char*)pbBuffer,szSource);
dwCount = (DWORD)strlen(szSource);
if(!CryptEncrypt(hKey, 0, true, 0, pbBuffer, &dwCount, dwBufferLen))
{
return false;
}
szDestination = (LPTSTR)pbBuffer;
MessageLength = dwCount; ///////////////////主要修正处
//--------------------------------------------------------
if(hKey != NULL)
{
if(!(CryptDestroyKey(hKey)))
return false;
}
//--------------------------------------------------------------------
// Release the provider handle.
if(hCryptProv !=NULL)
{
if(!(CryptReleaseContext(hCryptProv, 0)))
return false;
}
return true;
}
DecryptMessage(LPTSTR szSource,LPTSTR &szDestination, LPTSTR szKey)
{
//--------------------------------------------------------------------
// Declare and initialize local variables.
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
//--------------------------------------------------------------------
// Get a handle to the default provider.
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
return false;
}
//--------------------------------------------------------------------
// Decrypt the file with a session key derived from a password.
//--------------------------------------------------------------------
// Create a hash object.
if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))
{
return false;
}
//--------------------------------------------------------------------
// Hash in the password data.
if(!CryptHashData(hHash, (BYTE *)szKey, (DWORD)strlen(szKey), 0))
{
return false;
}
//--------------------------------------------------------------------
// Derive a session key from the hash object.
if(!CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey))
{
return false;
}
//--------------------------------------------------------------------
// Destroy the hash object.
if(!(CryptDestroyHash(hHash)))
return false;
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;
if(hCryptProv!=NULL)
{
if(!(CryptReleaseContext(hCryptProv, 0)))
return false;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
LPTSTR szSource = "This is a test.This is a test.This is a test.This is a test.This is a test.This is a test.";
LPTSTR szDestination = NULL;LPTSTR szRes = NULL;
LPTSTR szKey = "My own password for this test.";
EncryptMessage(szSource, szDestination, szKey);
DecryptMessage(szDestination, szRes, szKey);