public static string GenerateToken(string privateKey, string jsonData) { var pubkey = ConvertKeyToXml(privateKey); using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { try { rsa.FromXmlString(pubkey); byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonData); using (SHA256 sha256 = SHA256.Create()) { byte[] hash = sha256.ComputeHash(jsonBytes); byte[] signatureBytes = rsa.SignHash(hash, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); ------>Here I got error string token = Convert.ToBase64String(signatureBytes); return token; } } catch (Exception ex) { Console.WriteLine($"Error while generating tokens: {ex.Message}"); throw; } } }
public static string ConvertKeyToXml(string publickey) { byte[] keyBytes = Convert.FromBase64String(publickey); RSAParameters rsaParameters = new RSAParameters(); rsaParameters.Modulus = GetBytesFromArray(keyBytes, 28, 128); rsaParameters.Exponent = GetBytesFromArray(keyBytes, 156, 3);
RSA rsa = RSA.Create(); rsa.ImportParameters(rsaParameters);
string publicKeyXml = rsa.ToXmlString(false); return publicKeyXml; }
But I got error : Keyset does not exist
I have private key and jsondata if any one tell the Error and send me the Best Solution