加密高手入

shkspr 2003-08-19 11:55:43
HCRYPTPROV hProv = NULL;
HCRYPTKEY hKey = NULL;
HCRYPTHASH hHash = NULL;

PBYTE pbBuffer = NULL;
DWORD dwBufferLen = 0;
DWORD dwCount = 0;

BOOL status = FALSE;

// Get handle to the default provider.
if( !CryptAcquireContext( &hProv, NULL, NULL, PROV_RSA_FULL, 0 ) )
goto done;

// Encrypt the file with a session key derived from a password.

// Create a hash object.
if( !CryptCreateHash( hProv, CALG_MD5, 0, 0, &hHash ) )
goto done;

// Hash in the password data.
if( !CryptHashData( hHash, (const unsigned char *) pszHashKey, strlen( pszHashKey ), 0 ) )
goto done;

// Derive a session key from the hash object.
if( !CryptDeriveKey( hProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey ) )
goto done;

// Destroy the hash object.
CryptDestroyHash( hHash );
hHash = 0;

dwBufferLen = strlen( pszIn );
dwCount = dwBufferLen;

// Allocate memory.
if( ( pbBuffer = (PBYTE) malloc( dwBufferLen ) ) == NULL )
goto done;

// Copy challenge into Buffer
strncpy( (char *) pbBuffer, pszIn, dwBufferLen );

// Encrypt data
if( !CryptEncrypt( hKey, 0, FALSE, 0, pbBuffer, &dwCount, dwBufferLen ) )
goto done;


上面这一段很通用的程序里,加密的结果会不会和机器和操作系统有关?
我怎么改称和机器和操作系统无关的。
...全文
54 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shkspr 2003-08-19
  • 打赏
  • 举报
回复
我用这段程序加密了一段数据,在本机上解开以后是正确的
在另外一台机器(不同系统)上解开以后是错误的

下面是解码的程序,高手帮我分析一下

HCRYPTPROV hProv = NULL;
HCRYPTKEY hKey = NULL;
HCRYPTHASH hHash = NULL;
PBYTE pbBuffer = NULL;
int nCount = 0;
BOOL bRetValue = FALSE;
CString strLineOut;

// Get handle to the default provider.
if( !CryptAcquireContext( &hProv, NULL, NULL,
PROV_RSA_FULL, CRYPT_MACHINE_KEYSET ) )
{
// Create default key container.
if( !CryptAcquireContext( &hProv, NULL, NULL,
PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET ) )
{
goto Done;
}
}

// Encrypt the file with a session key derived from a password.

// Create a hash object.
if( !CryptCreateHash( hProv, CALG_MD5, 0, 0, &hHash ) )
{
goto Done;
}

// Hash in the password data.
if( !CryptHashData( hHash, (const unsigned char *) pszHashKey,
strlen( pszHashKey ), 0 ) )
{
goto Done;
}

// Derive a session key from the hash object.
if( !CryptDeriveKey( hProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey ) )
{
goto Done;
}

// Destroy the hash object.
CryptDestroyHash( hHash );
hHash = NULL;

nCount = nBufLen;

// Allocate memory.
if( ( pbBuffer = (PBYTE) malloc( nBufLen ) ) == NULL )
{
goto Done;
}

// Copy challenge into Buffer
memcpy( pbBuffer, pbIn, nBufLen );

// Encrypt data
if( !CryptDecrypt( hKey, 0, FALSE, 0, pbBuffer, (DWORD *) &nCount ) )
{
goto Done;
}

// Copy Response out of Buffer
strncpy( pszOut, (const char *) pbBuffer, nBufLen );
pszOut[nBufLen] = 0;

bRetValue = TRUE;
kingzai 2003-08-19
  • 打赏
  • 举报
回复
这只是一段普通的HASH加密算法,加密结果跟操作系统没什么关系,编译的时候把WinVer的版本定高一些就可以通过。
findmyself 2003-08-19
  • 打赏
  • 举报
回复
up
bager 2003-08-19
  • 打赏
  • 举报
回复
加密的结果不会和系统相关(我曾在Windows2000 P&&S,Window2003 Standard Edit RTM上测试过,可能不够全面),相关性应该只体现在加密相关函数( Encryption/Decryption Functions)对系统版本的配置要求上,如下:
Windows NT/2000 or later: Requires Windows NT 4.0 or later.
Windows 95/98/Me: Requires Windows 95 OSR2 or later (or Windows 95 with Internet Explorer 3.02 or later).

帮你UP

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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