July 2012
M T W T F S S
« Jun   Oct »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories

Meta


« | Main | »

C#.NET snack: sign data with RSA

By Bart | July 10, 2012

Create keys:

var csp = new RSACryptoServiceProvider(keyStrength);
var privateKey = csp.ExportCspBlob(true);
var publicKey = csp.ExportCspBlob(false);

Sign data:
var csp = new RSACryptoServiceProvider();
csp.Clear();
csp.ImportCspBlob(File.ReadAllBytes(privateKeyFile));
var sig = csp.SignData(data, new SHA1CryptoServiceProvider());

Verify data:

csp.Clear();
csp.ImportCspBlob(File.ReadAllBytes(publicKeyFile));
if (csp.VerifyData(data, new SHA1CryptoServiceProvider(), sig)) {
    Console.WriteLine("Data is OK");
} else {
    Console.WriteLine("Data is not OK");
}

Topics: Geekstuff, Snack | Geen reacties »