SHA1withRSA加簽名和驗簽名

利用私鑰加簽名:json

//contentForSign爲需加標籤的字符串ide

public String signWhithsha1withrsa(string contentForSign)字符串

{string

string result="";it

tryio

{base64

RSACryptoServiceProvider    tMerchantKey=GetPrivateKey();//獲取私鑰coding

SHA1Managed tHash=new SHA1Managed();rsa

//將傳遞須要加簽的字符串進行base64操做request

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);

}

}

相關文章
相關標籤/搜索