initial commit
This commit is contained in:
77
Signing/Program.cs
Normal file
77
Signing/Program.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
|
||||
|
||||
|
||||
// ---- Hashing example
|
||||
var data = "My Passwort";
|
||||
|
||||
HashAlgorithm sha = SHA256.Create();
|
||||
byte[] result = sha.ComputeHash(Encoding.ASCII.GetBytes(data));
|
||||
|
||||
foreach(var byt in result)
|
||||
{
|
||||
Console.Write($"{byt:X}");
|
||||
}
|
||||
// ----
|
||||
|
||||
// ---- Signing
|
||||
|
||||
/*
|
||||
Verifying a digital signature is the opposite of signing data.
|
||||
Verifying a signature will tell you if the signed data has changed
|
||||
or not. When a digital signature is verified, the signature is decrypted
|
||||
using the public key to produce the original hash value. The data that was
|
||||
signed is hashed. If the two hash values match, then the signature has been
|
||||
verified. To do this, write a program.
|
||||
|
||||
|
||||
publicKey kann verschlüssen. Nur der privat key kann aber Entschlüssen.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// https://learn.microsoft.com/en-us/dotnet/standard/security/cryptographic-signatures
|
||||
using SHA256 alg = SHA256.Create();
|
||||
|
||||
byte[] data = Encoding.ASCII.GetBytes("Hello, from the .NET Docs!");
|
||||
byte[] hash = alg.ComputeHash(data);
|
||||
RSAParameters sharedParameters;
|
||||
byte[] signedHash;
|
||||
|
||||
// Generate signature
|
||||
using (RSA rsa = RSA.Create())
|
||||
{
|
||||
sharedParameters = rsa.ExportParameters(false); //Public key
|
||||
|
||||
RSAPKCS1SignatureFormatter rsaFormatter = new(rsa);
|
||||
rsaFormatter.SetHashAlgorithm(nameof(SHA256));
|
||||
|
||||
signedHash = rsaFormatter.CreateSignature(hash);
|
||||
}
|
||||
|
||||
// Verify signature
|
||||
using (RSA rsa = RSA.Create())
|
||||
{
|
||||
rsa.ImportParameters(sharedParameters); //Public key
|
||||
|
||||
RSAPKCS1SignatureDeformatter rsaDeformatter = new(rsa);
|
||||
rsaDeformatter.SetHashAlgorithm(nameof(SHA256));
|
||||
|
||||
if (rsaDeformatter.VerifySignature(hash, signedHash))
|
||||
{
|
||||
Console.WriteLine("The signature is valid.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("The signature is not valid.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ----
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user