1+ using System ;
2+ using System . Security . Cryptography ;
3+ using System . Text ;
4+
5+ namespace QuantumExamples . Cryptography
6+ {
7+ public class SignatureExample
8+ {
9+ public static void RunExample ( )
10+ {
11+ const string originalMessage = "This is a message to sign!" ;
12+
13+ // Demonstrate ECDSA signing and verification
14+ DemonstrateECDSAExample ( originalMessage ) ;
15+
16+ // Demonstrate RSA signing and verification
17+ DemonstrateRSAExample ( originalMessage ) ;
18+
19+ // Demonstrate RSA with formatters
20+ DemonstrateRSAFormatterExample ( originalMessage ) ;
21+ }
22+
23+ private static void DemonstrateECDSAExample ( string message )
24+ {
25+ Console . WriteLine ( "=== ECDSA Example ===" ) ;
26+
27+ // Create ECDSA instance with P-256 curve
28+ var nistP256 = ECCurve . NamedCurves . nistP256 ;
29+ using var ecdsa = ECDsa . Create ( ECCurve . NamedCurves . nistP256 ) ;
30+
31+ // Message to sign
32+ var messageBytes = Encoding . UTF8 . GetBytes ( message ) ;
33+
34+ Console . WriteLine ( $ "Original message: { message } ") ;
35+
36+ // Sign the message
37+ var signature = ecdsa . SignData ( messageBytes , HashAlgorithmName . SHA256 ) ;
38+
39+ Console . WriteLine ( $ "Signature: { Convert . ToBase64String ( signature ) } ") ;
40+
41+ // Verify the signature
42+ var isValid = ecdsa . VerifyData ( messageBytes , signature , HashAlgorithmName . SHA256 ) ;
43+ Console . WriteLine ( $ "Signature valid: { isValid } ") ;
44+
45+ // Export public key for verification by others
46+ var publicKey = ecdsa . ExportParameters ( false ) ;
47+ Console . WriteLine ( $ "Public key X: { Convert . ToBase64String ( publicKey . Q . X ) } ") ;
48+ Console . WriteLine ( $ "Public key Y: { Convert . ToBase64String ( publicKey . Q . Y ) } ") ;
49+
50+ // Demonstrate verification with tampered data
51+ var tamperedMessage = Encoding . UTF8 . GetBytes ( "Hello, ECDSA Modified!" ) ;
52+ var isValidTampered = ecdsa . VerifyData ( tamperedMessage , signature , HashAlgorithmName . SHA256 ) ;
53+ Console . WriteLine ( $ "Tampered signature valid: { isValidTampered } ") ;
54+
55+ // Test with different instance
56+ using var ecdsaNew = ECDsa . Create ( ) ;
57+ byte [ ] newMessageBytes = Encoding . UTF8 . GetBytes ( "Hello, ECDSA!" ) ;
58+ var newSignature = ecdsaNew . SignData ( newMessageBytes , HashAlgorithmName . SHA256 ) ;
59+
60+ // Verify the signature
61+ var isNewValid = ecdsaNew . VerifyData ( newMessageBytes , newSignature , HashAlgorithmName . SHA256 ) ;
62+ Console . WriteLine ( $ "New signature valid: { isNewValid } ") ;
63+
64+ var parameters = ecdsaNew . ExportParameters ( false ) ;
65+
66+ var ecdsaFromParams = ECDsa . Create ( parameters ) ;
67+ var signatureFromParams = ecdsaFromParams . SignData ( newMessageBytes , HashAlgorithmName . SHA256 ) ;
68+ var isValidFromParams = ecdsaFromParams . VerifyData ( newMessageBytes , signatureFromParams , HashAlgorithmName . SHA256 ) ;
69+ Console . WriteLine ( $ "Signature valid with parameters: { isValidFromParams } ") ;
70+ }
71+
72+ private static void DemonstrateRSAExample ( string message )
73+ {
74+ Console . WriteLine ( "=== RSA Example ===" ) ;
75+
76+ using RSA rsa = RSA . Create ( ) ;
77+ byte [ ] data = Encoding . UTF8 . GetBytes ( message ) ;
78+ byte [ ] sig = rsa . SignData ( data , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
79+ bool isValid = rsa . VerifyData ( data , sig , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
80+ Console . WriteLine ( $ "Signature valid: { isValid } ") ;
81+
82+ // Create with parameters
83+ RSAParameters parameters = rsa . ExportParameters ( true ) ;
84+ using RSA rsaWithParams = RSA . Create ( parameters ) ;
85+ byte [ ] sigWithParams = rsaWithParams . SignData ( data , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
86+ bool isValidWithParams = rsaWithParams . VerifyData ( data , sigWithParams , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
87+ Console . WriteLine ( $ "Signature valid with parameters: { isValidWithParams } ") ;
88+
89+ // Create with specific key size
90+ using RSA rsaWithKeySize = RSA . Create ( 2048 ) ;
91+ byte [ ] sigWithKeySize = rsaWithKeySize . SignData ( data , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
92+ bool isValidWithKeySize = rsaWithKeySize . VerifyData ( data , sigWithKeySize , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
93+ Console . WriteLine ( $ "Signature valid with key size: { isValidWithKeySize } ") ;
94+ }
95+
96+ private static void DemonstrateRSAFormatterExample ( string message )
97+ {
98+ Console . WriteLine ( "=== RSA Formatter Example ===" ) ;
99+
100+ using SHA256 alg = SHA256 . Create ( ) ;
101+
102+ byte [ ] data = Encoding . UTF8 . GetBytes ( message ) ;
103+ byte [ ] hash = alg . ComputeHash ( data ) ;
104+
105+ RSAParameters sharedParameters ;
106+ byte [ ] signedHash ;
107+
108+ // Generate signature
109+ using ( RSA rsa = RSA . Create ( ) )
110+ {
111+ sharedParameters = rsa . ExportParameters ( false ) ;
112+
113+ RSAPKCS1SignatureFormatter rsaFormatter = new ( rsa ) ;
114+ rsaFormatter . SetHashAlgorithm ( nameof ( SHA256 ) ) ;
115+
116+ signedHash = rsaFormatter . CreateSignature ( hash ) ;
117+ }
118+
119+ // Verify signature
120+ using ( RSA rsa = RSA . Create ( ) )
121+ {
122+ rsa . ImportParameters ( sharedParameters ) ;
123+
124+ RSAPKCS1SignatureDeformatter rsaDeformatter = new ( rsa ) ;
125+ rsaDeformatter . SetHashAlgorithm ( nameof ( SHA256 ) ) ;
126+
127+ if ( rsaDeformatter . VerifySignature ( hash , signedHash ) )
128+ {
129+ Console . WriteLine ( "The signature is valid." ) ;
130+ }
131+ else
132+ {
133+ Console . WriteLine ( "The signature is not valid." ) ;
134+ }
135+ }
136+ }
137+ }
138+ }
0 commit comments