Skip to content

Instantly share code, notes, and snippets.

@mohnish82
Last active November 21, 2023 20:10
Show Gist options
  • Save mohnish82/7fd44b0f48d2d3f6ba66 to your computer and use it in GitHub Desktop.
Save mohnish82/7fd44b0f48d2d3f6ba66 to your computer and use it in GitHub Desktop.
Digital signature verification - CSharp and Java
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace DigitalSigning
{
class Program
{
static X509Certificate2 certificate = null;
static void Main(string[] args)
{
try
{
string data = "Test data";
// SIGN
byte[] signature = Sign(data, "CN=client1");
string digitalSignatureStr = System.Convert.ToBase64String(signature);
Console.WriteLine("Digital sig: " + digitalSignatureStr);
//VERIFY
bool validSig = Verify(data, digitalSignatureStr, "");
Console.WriteLine(validSig ? "Signature verified" : "Signature unverified!");
}
catch (Exception ex)
{
Console.WriteLine("EXCEPTION: " + ex.Message);
}
Console.ReadKey();
}
static byte[] Sign(string text, string certSubject)
{
X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
my.Open(OpenFlags.ReadOnly);
// Find certificate
RSACryptoServiceProvider csp = null;
foreach (X509Certificate2 cert in my.Certificates)
{
if (cert.Subject.Contains(certSubject))
{
certificate = cert;
csp = (RSACryptoServiceProvider)cert.PrivateKey;
}
}
if (csp == null)
throw new Exception("No valid cert was found");
// SIGN
RSACryptoServiceProvider rsaClear = new RSACryptoServiceProvider();
rsaClear.ImportParameters(csp.ExportParameters(true));
byte[] signature = rsaClear.SignData(Encoding.UTF8.GetBytes(text),
CryptoConfig.CreateFromName("SHA256"));
return signature;
}
static bool Verify(string text, string signature, string certPath)
{
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)certificate.PublicKey.Key;
return csp.VerifyData(Encoding.UTF8.GetBytes(text),
CryptoConfig.MapNameToOID("SHA256"),
System.Convert.FromBase64String(signature));
}
}
}
/**
* Tests digital signature verification
*
*/
public void testVerifySignature_MohnishSelfCert() throws Exception {
kynectServlet.setKynectSignatureAlias("");
String data = "Test data";
String signature = "AUyy1R9d4doirllFuzA6VFKBBNWC8/bYu9WeDvmycZB8bswRdoZvTcVAcw4/YO9XfzpVqJBLylLQHJLfNZP1vLYhGFJ9c7DRpMHsJ/LwX+PWZshb+51GciT6Acwa3VMeo2/OyxUOTSFCS8SqaQgmkIu8SogIPjRzzWKJAW0KA+A=";
boolean result = false;
try{
Certificate cert = keyStore.getCertificate("mcselfcert");
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initVerify(cert);
byte[] dataBytes = data.getBytes("UTF-8");
sig.update(dataBytes);
byte[] sigBytes = Base64.decodeBase64(signature.getBytes("UTF-8"));
result = sig.verify(sigBytes);
}catch(Exception e) {
e.printStackTrace();
}
assertTrue("Signatures don't match!", result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment