如何使用MD5 hash进行加密。

xhyxyr 2003-10-20 08:48:13
我需要用到MD5 hash进行加密,但不知道如何使用,帮助信息也只是MD5的一些定义而已,md5 hash 应该是一个函数吧,这个函数包含哪些参数。我是用vc++6.0进行编程,我该如何找到这个函数?
...全文
342 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
shines77 2003-10-21
  • 打赏
  • 举报
回复
mark
smch 2003-10-21
  • 打赏
  • 举报
回复
怎么发这来了。

MD5 hash不是一个函数。是一系列的函数。
wuxfBrave(升星了,休息休息) ( )
的已经比较清楚了。

有很多东西不是一个函数就可以完成的。
smch 2003-10-21
  • 打赏
  • 举报
回复
没人help me吗?
xhyxyr 2003-10-20
  • 打赏
  • 举报
回复
感谢二位的回复。我现在需要用到MD5 hash,所以想知道这个函数需要的参数以及返回值,还有需要包含的头文件等。
smch 2003-10-20
  • 打赏
  • 举报
回复
MD5 hash用于验证信息是否被修改,而不能用于加密。
wuxfBrave 2003-10-20
  • 打赏
  • 举报
回复
这是我写的一个对文件加密解密的函数
wuxfBrave 2003-10-20
  • 打赏
  • 举报
回复
BOOL CSystem::DecryptFile(LPCTSTR lpcszSource, LPCTSTR lpcszDest, LPCTSTR lpcszPasswd)
{
HCRYPTPROV hCryptProv = NULL;
HCRYPTKEY hKey = NULL;
HCRYPTHASH hHash = NULL;

CFileException e;
CFile source;
if (!source.Open(lpcszSource, CFile::modeRead|CFile::shareDenyWrite, &e))
{
ASSERT(FALSE);
return FALSE;
}

CFile dest;
if (!dest.Open(lpcszDest, CFile::modeCreate|CFile::modeWrite|CFile::shareExclusive, &e))
{
ASSERT(FALSE);
source.Close();
return FALSE;
}

if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
{
ASSERT(FALSE);
source.Close();
dest.Close();
return FALSE;
}


// Check for existence of a password.
DWORD dwFileLen = source.SeekToEnd();
source.SeekToBegin();
if (!lpcszPasswd)
{
// Decrypt the file with the saved session key.
// Read key blob length from source file, and allocate memory.
DWORD dwKeyBlobLen = 0;
PBYTE pbKeyBlob = NULL;
TRY
{
source.Read(&dwKeyBlobLen, sizeof(DWORD));
TRY
{
pbKeyBlob = new BYTE[dwKeyBlobLen];
}
CATCH(CMemoryException, me)
{
MessageBox(NULL, _T("不合法的文件"), _T("错误"), MB_OK | MB_APPLMODAL | MB_ICONWARNING);
return FALSE;
}
END_CATCH
source.Read(pbKeyBlob, dwKeyBlobLen);
}
CATCH(CFileException, e)
{
ASSERT(FALSE);
source.Close();
dest.Close();

if (pbKeyBlob)
delete[] pbKeyBlob;

CryptReleaseContext(hCryptProv, 0);

return FALSE;
}
END_CATCH

// Import key blob into CSP.
HCRYPTKEY hXchgKey = NULL;
if (!CryptImportKey(hCryptProv, pbKeyBlob, dwKeyBlobLen, hXchgKey, 0, &hKey))
{
DWORD dwErr = GetLastError();
ASSERT(FALSE);
source.Close();
dest.Close();

CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
delete[] pbKeyBlob;
}
else
{
// Decrypt the file with a session key derived from a password.
// Create a hash object.
// Hash in the password data.
if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash)
|| !CryptHashData(hHash, (LPBYTE)lpcszPasswd, _tcslen(lpcszPasswd), 0)
|| !CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey))
{
ASSERT(FALSE);
source.Close();
dest.Close();

CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
// 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.

DWORD dwCount;
DWORD dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
DWORD dwBufferLen = dwBlockLen;
PBYTE pbBuffer = new BYTE[dwBufferLen];
BOOL bSuccess = TRUE;

// Decrypt source file, and write to destination file.
do
{
// Read up to dwBlockLen bytes from source file.
TRY
{
dwCount = source.Read(pbBuffer, dwBlockLen);
}
CATCH(CFileException, e)
{
ASSERT(FALSE);
bSuccess = FALSE;
break;
}
END_CATCH

if(!CryptDecrypt(hKey, 0, source.GetPosition()==dwFileLen, 0, pbBuffer, &dwCount))
{
ASSERT(FALSE);
bSuccess = FALSE;
break;
}

TRY
{
dest.Write(pbBuffer, dwCount);
}
CATCH(CFileException, e)
{
ASSERT(FALSE);
bSuccess = FALSE;
break;
}
END_CATCH
}
while (source.GetPosition() != dwFileLen);

dest.Close();
source.Close();

if (pbBuffer)
delete[] pbBuffer;

CryptDestroyKey(hKey);

CryptReleaseContext(hCryptProv, 0);

return bSuccess;
}
wuxfBrave 2003-10-20
  • 打赏
  • 举报
回复
BOOL CSystem::EncryptFile(LPCTSTR lpcszSource, LPCTSTR lpcszDest, LPCTSTR lpcszPasswd)
{
HCRYPTPROV hCryptProv = NULL;
HCRYPTKEY hKey = NULL;
HCRYPTKEY hXchgKey = NULL;
HCRYPTHASH hHash = NULL;

CFileException e;
CFile source;
if (!source.Open(lpcszSource, CFile::modeRead|CFile::shareDenyWrite, &e))
{
ASSERT(FALSE);
return FALSE;
}

CFile dest;
if (!dest.Open(lpcszDest, CFile::modeCreate|CFile::modeWrite|CFile::shareExclusive, &e))
{
ASSERT(FALSE);
source.Close();
return FALSE;
}

if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
{
ASSERT(FALSE);
source.Close();
dest.Close();
return FALSE;
}

// Create the session key.
if (!lpcszPasswd)
{
// No password was passed.
// Encrypt the file with a random session key and write the key to a file.
// Create a random session key.

DWORD dwKeyBlobLen;
if (!CryptGenKey(hCryptProv, ENCRYPT_ALGORITHM, CRYPT_EXPORTABLE, &hKey)
|| !CryptGetUserKey(hCryptProv, AT_KEYEXCHANGE, &hXchgKey)
|| !CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, NULL, &dwKeyBlobLen))
{
ASSERT(FALSE);
source.Close();
dest.Close();

if (hKey)
CryptDestroyKey(hKey);

if (hXchgKey)
CryptDestroyKey(hXchgKey);

CryptReleaseContext(hCryptProv, 0);
return FALSE;
}

// Encrypt and export session key into a simple key blob.
PBYTE pbKeyBlob = new BYTE[dwKeyBlobLen];
if (!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, pbKeyBlob, &dwKeyBlobLen))
{
ASSERT(FALSE);
source.Close();
dest.Close();

delete[] pbKeyBlob;

if (hKey)
CryptDestroyKey(hKey);

if (hXchgKey)
CryptDestroyKey(hXchgKey);

CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
CryptDestroyKey(hXchgKey);
hXchgKey = NULL;

TRY
{
dest.Write(&dwKeyBlobLen, sizeof(DWORD));
dest.Write(pbKeyBlob, dwKeyBlobLen);
delete[] pbKeyBlob;
}
CATCH(CFileException, e)
{
ASSERT(FALSE);
source.Close();
dest.Close();

delete[] pbKeyBlob;

if (hKey)
CryptDestroyKey(hKey);

CryptReleaseContext(hCryptProv, 0);
return FALSE;
}
END_CATCH
}
else
{
// The file will be encrypted with a session key derived from a
// password.
// The session key will be recreated when the file is decrypted
// only if the password used to create the key is available.

// Create a hash object.

if (!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))
{
ASSERT(FALSE);
source.Close();
dest.Close();

CryptReleaseContext(hCryptProv, 0);
return FALSE;
}

// Hash the password.
if (!CryptHashData(hHash, (LPBYTE)lpcszPasswd, _tcslen(lpcszPasswd), 0))
{
ASSERT(FALSE);
source.Close();
dest.Close();

CryptDestroyHash(hHash);
CryptReleaseContext(hCryptProv, 0);
return FALSE;
}

// Derive a session key from the hash object.
if (!CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey))
{
ASSERT(FALSE);
source.Close();
dest.Close();

CryptDestroyHash(hHash);
CryptReleaseContext(hCryptProv, 0);
return FALSE;
}

CryptDestroyHash(hHash);
hHash = NULL;
}

// 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 written to the destination file.

// Determine number of bytes to encrypt at a time.
// This must be a multiple of ENCRYPT_BLOCK_SIZE.
// ENCRYPT_BLOCK_SIZE is set by a #define statement.

DWORD dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;

// Determine the block size. If a block cipher is used,
// it must have room for an extra block.

DWORD dwBufferLen = dwBlockLen;
if (ENCRYPT_BLOCK_SIZE > 1)
dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;

PBYTE pbBuffer = new BYTE[dwBufferLen];
DWORD dwCount = 0;

// In a do loop, encrypt the source file and write to the source file.
BOOL bSuccess = TRUE;
DWORD dwFileLength = source.GetLength();
TRACE("文件长度%d", dwFileLength);
source.SeekToBegin();
do
{
TRY
{
dwCount = source.Read(pbBuffer, dwBlockLen);
}
CATCH(CFileException, e)
{
ASSERT(FALSE);
bSuccess = FALSE;
break;
}
END_CATCH

if (!CryptEncrypt(hKey, 0, source.GetPosition()==dwFileLength, 0, pbBuffer, &dwCount, dwBufferLen))
{
ASSERT(FALSE);
bSuccess = FALSE;
break;
}

TRY
{
dest.Write(pbBuffer, dwCount);
}
CATCH(CFileException, e)
{
ASSERT(FALSE);
bSuccess = FALSE;
break;
}
END_CATCH
}
while(source.GetPosition() != dwFileLength);

source.Close();
dest.Close();

if (pbBuffer)
delete[] pbBuffer;

if (hKey)
CryptDestroyKey(hKey);

if(hCryptProv)
CryptReleaseContext(hCryptProv, 0);

return bSuccess;
}
catyou 2003-10-20
  • 打赏
  • 举报
回复
上网找吧,vckbase上有源码下载

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧