using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
public static void Main()
Console.WriteLine($"SHA1withRSA:{Sign("Test WS", "67AA44E9BD7A73D006EF8C9A929FD00FE38AE417747DCC348AAB135D55B640E431BCB41EE24946E40BA60D2CD7C21B03A8D2E61493267518FBE31CAFD5601E67", "CBF78385216C61EFDA09FAD16D17AE8A7B09B2AF5BC6A81C2AAFC7456090F8CB11545153DA7FF5D0B73FB1531ADC6D745F1C7B0BE57D8CD791AC82816E03A22D", "SHA1withRSA")}");
Console.WriteLine($"SHA1withRSAandMGF1:{Sign("Test WS", "67AA44E9BD7A73D006EF8C9A929FD00FE38AE417747DCC348AAB135D55B640E431BCB41EE24946E40BA60D2CD7C21B03A8D2E61493267518FBE31CAFD5601E67", "CBF78385216C61EFDA09FAD16D17AE8A7B09B2AF5BC6A81C2AAFC7456090F8CB11545153DA7FF5D0B73FB1531ADC6D745F1C7B0BE57D8CD791AC82816E03A22D", "SHA1withRSAandMGF1")}");
Console.WriteLine($"SHA1withRSA/ISO9796-2:{Sign("Test WS", "67AA44E9BD7A73D006EF8C9A929FD00FE38AE417747DCC348AAB135D55B640E431BCB41EE24946E40BA60D2CD7C21B03A8D2E61493267518FBE31CAFD5601E67", "CBF78385216C61EFDA09FAD16D17AE8A7B09B2AF5BC6A81C2AAFC7456090F8CB11545153DA7FF5D0B73FB1531ADC6D745F1C7B0BE57D8CD791AC82816E03A22D", "SHA1withRSA/ISO9796-2")}");
private static RsaKeyParameters MakeKey(String modulusHexString, String exponentHexString, bool isPrivateKey)
var modulus = new Org.BouncyCastle.Math.BigInteger(modulusHexString, 16);
var exponent = new Org.BouncyCastle.Math.BigInteger(exponentHexString, 16);
return new RsaKeyParameters(isPrivateKey, modulus, exponent);
public static String Sign(String data, String privateModulusHexString, String privateExponentHexString, string algorithm)
RsaKeyParameters key = MakeKey(privateModulusHexString, privateExponentHexString, true);
ISigner sig = SignerUtilities.GetSigner(algorithm);
var bytes = Encoding.ASCII.GetBytes(data);
sig.BlockUpdate(bytes, 0, bytes.Length);
byte[] signature = sig.GenerateSignature();
return ByteArrayToString(signature);
public static string ByteArrayToString(byte[] ba)
StringBuilder hex = new StringBuilder(ba.Length * 2);
hex.AppendFormat("{0:X2}", b);