SHA1withRSA加签名和验签名

利用私钥加签名:

//contentForSign为需加标签的字符串

public String signWhithsha1withrsa(string contentForSign)

{

string result="";

try

{

RSACryptoServiceProvider    tMerchantKey=GetPrivateKey();//获取私钥

SHA1Managed tHash=new SHA1Managed();

//将传递需要加签的字符串进行base64操作

byte[] base64=Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes(contentForSign)));

byte[] tHashedData=tHash.ComputeHash(base64);

//对其进行加签名

byte[] tSigned=tMerchantKey.SignHash(tHashedData,"SHA1");

result=Convert.ToBase64String(tSigned);

return result;

}

catch(Exception e)

{

logger.Info("加签失败!",e);

}}

 

//读取cer并验证公钥签名

//requestBody为json报文数据

//signature为加签标识

public bool read_cer_and_verify_sign(string requestBody,string signature)

{

bool result=false;

try

{

byte[] orgin=Encoding.UTF8.GetBytes((requestBody));//json报文数据获得字节数据

byte[] singedBase64=Convert.FromBase64String((signature));

//读取证书

RSACryptoServiceProvider tMerchantKey=GetPublicKey();//获取公钥

result=tMerchantKey.VerifyData(orgin,"SHA1",singedBase64);

return result;

}

catch(Exception e)

{

logger.Info("验签失败!",e);

}

}

posted @ 2019-03-12 14:47  那一抹的温柔  阅读(3813)  评论(1编辑  收藏  举报