The problem must be in the string you are sending, it must be badly formatted, try to do the following, first format your string following the documentation of the city hall, it looks more or less as below, and beware, IISretido has to be with S or N and not 0 or 1, pay attention to the documentation for type of Rps, etc.:
char pad = '0';
string strDeAssinatura = tpRPS.ChaveRPS.InscricaoPrestador.ToString().PadLeft(8, pad) +
tpRPS.ChaveRPS.SerieRPS.PadRight(5, ' ') +
tpRPS.ChaveRPS.NumeroRPS.ToString().PadLeft(12, pad) +
tpRPS.DataEmissao.ToString("yyyyMMdd").PadLeft(8, pad) +
tpRPS.TributacaoRPS.ToString() +
tpRPS.StatusRPS.ToString() +
iisRetido +
tpRPS.ValorServicos.ToString("N2", _enUS).Replace(".", "").ToString().PadLeft(15, pad) +
tpRPS.ValorDeducoes.ToString("N2", _enUS).Replace(".", "").ToString().PadLeft(15, pad) +
tpRPS.CodigoServico.ToString().PadLeft(5, pad) +
tomadorTipo +
tpRPS.CPFCNPJTomador.Item.PadLeft(14, pad);
After signing only this string, you can use the function below for this:
Then play the value in tpRPS.Assinatura and you're done.
Good luck.
private byte[] Assinatura(string strDeAssinatura)
{
KeyInfo keyInfo = new KeyInfo();
KeyInfoX509Data keyInfoData = new KeyInfoX509Data(_cert);
RSACryptoServiceProvider rsaprovider = (RSACryptoServiceProvider)_cert.PublicKey.Key;
keyInfo.AddClause(keyInfoData);
RSACryptoServiceProvider rsaKey = _cert.PrivateKey as RSACryptoServiceProvider;
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(rsaKey);
RSAFormatter.SetHashAlgorithm("SHA1");
SHA1Managed SHhash = new SHA1Managed();
byte[] SignedHashValue = RSAFormatter.CreateSignature(SHhash.ComputeHash(new ASCIIEncoding().GetBytes(strDeAssinatura)));
return SignedHashValue;
}