终于使用microsoft enhanced cryptographic provider v1.0 实现了公钥加密和解密,但safesign csp version 1.0不能,不知为什么,有高手能解答吗?
另外dwbuflen 怎样计算才好,我取100,出编号234错,便取了200。
以下程序在vs2003下调试成功
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
void handleerror(char *s);
#define sa_sign_prov "safesign csp version 1.0"
int _tmain(int argc, _tchar* argv[])
{
hcryptprov hcryptprov;
hcryptkey hxchgkey; //交换密钥
byte pbdata[1000];
dword cbdata;
printf("a cryptographic provider will acquired. \n");
char ch;
//--------------------------------------------------------------------
//访问CSP
if(cryptacquirecontext(
&hcryptprov,
null, //null表示使用默认密钥容器,默认密钥容器名为用户登陆名
ms_enhanced_prov ,//sa_sign_prov ,
//sa_sign_prov ,
prov_rsa_full,
0))
{
printf("a cryptographic provider has been acquired. \n");
}
else
{
printf("error in get cryptographic provider h. \n");
}
//得到CSP参数
cbdata = 10000;
if(cryptgetprovparam(
hcryptprov,
pp_name,
pbdata,
&cbdata,
crypt_first ))
{
printf("cryptgetprovparam succeeded.\n");
printf("provider name: %s\n", pbdata);
}
else
{
printf("error reading csp name. \n");
//exit(1);
}
//得到用户的密钥对
if(cryptgetuserkey(
hcryptprov,
at_keyexchange,
&hxchgkey))
{
printf("the user exchange key pair has been retrieved. \n");
bool bresult;
dword dwsecretlen;
char bsecret[100]="i love you"; //改了
dwsecretlen=strlen((char *) bsecret)+1;
//使用公钥加密
if(!cryptencrypt(
hxchgkey, // 之前获得的密鈅对象
0, // 不散列数据
true, // 最后的还是缓冲的数据
0, // 必须置0
(byte*)bsecret, // 数据缓冲区
&dwsecretlen, // 数据尺寸
200) // 数据块尺寸
)
{
printf("error during cryptencrypt. \n");
handleerror("error during cryptencrypt. \n");
}
else
{
printf("the cryptencrypt sucess. \n");
printf("encrypt text:%s\n",bsecret);
}
//---------------------------------------------------------
//使用私钥解密
if(!cryptdecrypt(hxchgkey,0,true,0,(byte*)bsecret,&dwsecretlen))//改了
{
handleerror("error during decrypt. \n");
}
else
{ printf("cryptdecrypt sucess");