通过CryptAPI调用Microsoft RAS服务程序中的RC4流编码
虽然有编程接口,也真不好懂,我看了半天才明白了一点.void CFileEncIIDlg::OnButtonEnc() 
{
// TODO: Add your control notification handler code here
CString strPassword;
strPassword=GetPassword();
if(strPassword!="")
{
SetState("初始化...");
HCRYPTPROV hCsp;     //加密服务程序(CSP)句柄
HCRYPTHASH hPwdHash; //生成密钥的散列句柄
HCRYPTKEY hPwdKey;   //密钥句柄
if(!CryptAcquireContext(&hCsp,NULL,NULL,PROV_RSA_FULL,0))

if(GetLastError()==NTE_BAD_KEYSET)
{
if(CryptAcquireContext(&hCsp,NULL,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET))
{
if(!CryptAcquireContext(&hCsp,NULL,NULL,PROV_RSA_FULL,0))
{
MessageBox("建立新密钥库后,连接CSP错误.");
return;
}
}
else
{
MessageBox("建立新密钥库错误.");
return;
}
}
else
{
MessageBox("连接CSP错误(非无密钥库).");
return;
}
}
if(!CryptCreateHash(hCsp,CALG_SHA,0,0,&hPwdHash))
{
MessageBox("建立散列错误");
return;
}
if(!CryptHashData(hPwdHash,(unsigned char *)strPassword.GetBuffer(strPassword.GetLength()),strPassword.GetLength(),NULL))
{
MessageBox("由口令生成散列错误");
return;
}
if(!CryptDeriveKey(hCsp,CALG_RC4,hPwdHash,CRYPT_EXPORTABLE,&hPwdKey))
{
MessageBox("由口令派生密钥错误");
return;
}
//////////////从文件中读出数据/////////////
SetState("读数据...");
CString strFilename;//取得文件名
m_Edit.GetWindowText(strFilename);CFile LogFile(strFilename, //打开文件
CFile::modeReadWrite | CFile::shareDenyNone);
CString strBuf; 
DWORD dwDataLength=LogFile.GetLength();
LogFile.Read(strBuf.GetBufferSetLength(dwDataLength),
dwDataLength); //读出文件内容//////////////////读结束///////////////////
SetState("加密中...");
if(!CryptEncrypt(hPwdKey,0,TRUE,0,(unsigned char *)strBuf.GetBuffer(dwDataLength),&dwDataLength,dwDataLength))
{
MessageBox("进行加密错误");
return;
}
//////////////////把加密后的数据写入文件////////////
SetState("写数据...");
LogFile.Seek(NULL, CFile::begin); //文件指针移动到文件头
LogFile.Write(strBuf.GetBuffer(strBuf.GetLength()), 
strBuf.GetLength()); 
LogFile.Close(); //关闭文件
//////////////////写结束////////////////////////////
SetState("完成");

}void CFileEncIIDlg::OnButtonUnenc() 
{
// TODO: Add your control notification handler code here
CString strPassword;
strPassword=GetPassword();
if(strPassword!="")
{
SetState("初始化...");
HCRYPTPROV hCsp; //加密服务程序(CSP)句柄
HCRYPTHASH hPwdHash; //生成密钥的散列句柄
HCRYPTKEY hPwdKey; //密钥句柄if(!CryptAcquireContext(&hCsp,NULL,NULL,PROV_RSA_FULL,0))

if(GetLastError()==NTE_BAD_KEYSET)
{
if(CryptAcquireContext(&hCsp,NULL,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET))
{
if(!CryptAcquireContext(&hCsp,NULL,NULL,PROV_RSA_FULL,0))
{
MessageBox("建立新密钥库后,连接CSP错误.");
return;
}
}
else
{
MessageBox("建立新密钥库错误.");
return;
}
}
else
{
MessageBox("连接CSP错误(非无密钥库).");
return;
}
}
if(!CryptCreateHash(hCsp,CALG_SHA,0,0,&hPwdHash))
{
MessageBox("建立散列错误");
return;
}
if(!CryptHashData(hPwdHash,(unsigned char *)strPassword.GetBuffer(strPassword.GetLength()),strPassword.GetLength(),NULL))
{
MessageBox("由口令生成散列错误");
return;
}
if(!CryptDeriveKey(hCsp,CALG_RC4,hPwdHash,CRYPT_EXPORTABLE,&hPwdKey))
{
MessageBox("由口令派生密钥错误");
return;
}
//////////////从文件中读出数据/////////////
SetState("读数据...");
CString strFilename;//取得文件名
m_Edit.GetWindowText(strFilename);CFile LogFile(strFilename, //打开文件
CFile::modeReadWrite | CFile::shareDenyNone);
CString strBuf; 
DWORD dwDataLength=LogFile.GetLength();
LogFile.Read(strBuf.GetBufferSetLength(dwDataLength),
dwDataLength); //读出文件内容//////////////////读结束///////////////////
SetState("加密中...");
if(!CryptDecrypt(hPwdKey,0,TRUE,0,(unsigned char *)strBuf.GetBuffer(dwDataLength),&dwDataLength))
{
MessageBox("进行解密错误");
return;
}
//////////////////把加密后的数据写入文件////////////
SetState("写数据...");
LogFile.Seek(NULL, CFile::begin); //文件指针移动到文件头
LogFile.Write(strBuf.GetBuffer(strBuf.GetLength()), 
strBuf.GetLength()); 
LogFile.Close(); //关闭文件
//////////////////写结束///////////////////////////
SetState("完成");

}