我有,可到www.v-galaxy.com上有疏菜兄的文章或www.vckbase.com,代码如下。
加密:
CString CHrtKey::EncryptGroupName(CString GroupName)
{
if(GroupName.IsEmpty())
return GroupName;
CString szData;
szData=GroupName;
szData.TrimRight();
return szData;
HCRYPTPROV hProv=0;
HCRYPTKEY hKey=0;
HCRYPTHASH hHash=0;
try
{
if(!CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,0))
{
if(GetLastError()==NTE_BAD_KEYSET)
{
if(CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET))
{
if(!CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,0))
AfxThrowUserException();
}
else
AfxThrowUserException();
}
else
AfxThrowUserException();
} if(!CryptCreateHash(hProv,CALG_MD5,0,0,&hHash))
AfxThrowUserException();
if(!CryptHashData(hHash,(BYTE*)_T("HTOM"),4,0))
AfxThrowUserException();
if(!CryptDeriveKey(hProv,CALG_RC4,hHash,CRYPT_EXPORTABLE,&hKey))
AfxThrowUserException();
if(!CryptDestroyHash(hHash))
AfxThrowUserException();
DWORD nLength;
nLength=szData.GetLength()+1;
if(!CryptEncrypt(hKey,0,TRUE,0,(BYTE*)szData.GetBufferSetLength(nLength),&nLength,nLength))
AfxThrowUserException();
szData.ReleaseBuffer();
}
catch(CUserException *e)
{
LPVOID szErrorText;
CString szErrorNum;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,NULL,
::GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&szErrorText,
0,NULL);
szErrorNum.Format("错误码:%d\n错误信息:",::GetLastError());
szErrorNum+=(LPCTSTR)szErrorText;
::LocalFree(szErrorText);
AfxMessageBox(szErrorNum,MB_OK|MB_ICONSTOP);
AfxThrowUserException();
}
return szData;
}
解密:
CString CHrtKey::DecryptGroupName(CString GroupName)
{
if(GroupName.IsEmpty())
return GroupName;
CString szData;
szData=GroupName;
szData.TrimRight();
return szData;
HCRYPTPROV hProv=0;
HCRYPTKEY hKey=0;
HCRYPTHASH hHash=0;
try
{
if(!CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,0))
{
if(GetLastError()==NTE_BAD_KEYSET)
{
if(CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET))
{
if(!CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,0))
AfxThrowUserException();
}
else
AfxThrowUserException();
}
else
AfxThrowUserException();
} if(!CryptCreateHash(hProv,CALG_MD5,0,0,&hHash))
AfxThrowUserException();
if(!CryptHashData(hHash,(BYTE*)_T("HTOM"),4,0))
AfxThrowUserException();
if(!CryptDeriveKey(hProv,CALG_RC4,hHash,CRYPT_EXPORTABLE,&hKey))
AfxThrowUserException();
if(!CryptDestroyHash(hHash))
AfxThrowUserException();
DWORD nLength;
nLength=szData.GetLength()+1;
if(!CryptDecrypt(hKey,0,TRUE,0,(BYTE *)szData.GetBufferSetLength(nLength),&nLength))
AfxThrowUserException();
szData.ReleaseBuffer();
}
catch(CUserException *e)
{
LPVOID szErrorText;
CString szErrorNum;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,NULL,
::GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&szErrorText,
0,NULL);
szErrorNum.Format("错误码:%d\n错误信息:",::GetLastError());
szErrorNum+=(LPCTSTR)szErrorText;
::LocalFree(szErrorText);
AfxMessageBox(szErrorNum,MB_OK|MB_ICONSTOP);
AfxThrowUserException();
}
return szData;
}