OpenSSL_add_all_algorithms();
EVP_PKEY *pPriKey;
BIO *p_Bio_File;
char * usr="root";
p_Bio_File = BIO_new_file("990724.pem", "r");
if(!p_Bio_File)
{
cout<<"file error"<<endl;
return 0;
}
pPriKey = PEM_read_bio_PrivateKey(p_Bio_File, NULL, NULL,NULL);
if (NULL == pPriKey)
{
cout<<"get key error"<<endl;
return 0;
}
//fclose(pFile);
BIO_free(p_Bio_File);
char *mPackStr="{\"phonecode\":\"000000000000000\",\"mark\":\"1\"}";
int mPackLen =strlen(mPackStr);
EVP_MD_CTX mdctx;
EVP_MD_CTX_init(&mdctx);
if(!EVP_SignInit_ex(&mdctx,EVP_sha1(),NULL)) //签名初始化,设置摘要算法
{
printf("err\n");
EVP_PKEY_free(pPriKey);
return 0;
}
if(!EVP_SignUpdate(&mdctx,mPackStr,mPackLen)) //计算签名(摘要)Update
{
printf("err\n");
EVP_PKEY_free(pPriKey);
return 0;
}
unsigned int mSignLen = EVP_PKEY_size(pPriKey);
unsigned char * pSignBuf = (unsigned char*)calloc(mSignLen+1, sizeof(char));
if(!EVP_SignFinal(&mdctx,pSignBuf,&mSignLen,pPriKey)) //签名输出
{
printf("err\n");
EVP_PKEY_free(pPriKey);
return 0;
}
char * b=base64_encode((char *)pSignBuf);
EVP_MD_CTX_cleanup(&mdctx);
cout<<"签名结果:"<<endl;
cout<<b<<endl;
和java
System.out.println("签名前的内容是"+singed);
byte[] sig=null;
try {
Signature dsa = Signature.getInstance("SHA1withRSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
//Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding",new org.bouncycastle.jce.provider.BouncyCastleProvider());
dsa.initSign(key);
dsa.update(singed.getBytes());
sig = dsa.sign();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SignatureException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String s=Base64EnAndDeCode.encode(sig);
System.out.println("签名后"+s);
什么问题导致签名结果不同呢?搞了好几天了。。囧。。就是结果不同。。
------解决思路----------------------
怎么解决阿?我遇到问题了。
------解决思路----------------------
接分来了,哈哈