private static void TestSignature(string sourceFile, string signatureFile) { var rsa = new RsaCryptosystem(new PrimeNumberGenerator(new MillerRabinTest())); var keys = rsa.GenerateKeys(); var file = File.ReadAllBytes(sourceFile); var digitalSignature = new DigitalSignature(new Hasher()); File.WriteAllBytes(signatureFile, digitalSignature.Sign(file, keys.publicKey)); var signature = File.ReadAllBytes(signatureFile); var result = digitalSignature.CheckSign(file, signature, keys.privateKey); Console.WriteLine("check = {0}", result); }
private static void TestFileRsa(string sourceFile, string encryptedFile, string decryptedFile) { var rsa = new RsaCryptosystem(new PrimeNumberGenerator(new MillerRabinTest())); Stopwatch watch = new Stopwatch(); var message = File.ReadAllBytes(sourceFile); watch.Start(); var keys = rsa.GenerateKeys(); var publicKeySerializer = new XmlSerializer(typeof(CandPCI_4.RSA.PublicKey)); using (var file = File.OpenWrite(sourceFile + ".public")) publicKeySerializer.Serialize(file, keys.publicKey); var privateKeySerializer = new XmlSerializer(typeof(CandPCI_4.RSA.PrivateKey)); using (var file = File.OpenWrite(sourceFile + ".private")) privateKeySerializer.Serialize(file, keys.privateKey); var privateComponentsSerializer = new XmlSerializer(typeof(CandPCI_4.RSA.PrivateComponents)); using (var file = File.OpenWrite(sourceFile + ".components")) privateComponentsSerializer.Serialize(file, keys.privateComponents); watch.Stop(); Console.WriteLine("Keys generation time = {0}", watch.ElapsedMilliseconds); watch.Reset(); //var d = keys.privateKey.d; //var e = keys.publicKey.e; //keys.privateKey.d = e; //keys.publicKey.e = d; watch.Start(); var encrypted = rsa.Encrypt(message, keys.publicKey); watch.Stop(); File.WriteAllBytes(encryptedFile, encrypted); Console.WriteLine("Encryption time = {0}", watch.ElapsedMilliseconds); encrypted = File.ReadAllBytes(encryptedFile); watch.Reset(); watch.Start(); //var decrypted = rsa.Decrypt(encrypted, keys.privateKey, keys.publicKey); var decrypted = rsa.Decrypt(encrypted, keys.privateKey); watch.Stop(); Console.WriteLine("Decryption time = {0}", watch.ElapsedMilliseconds); File.WriteAllBytes(decryptedFile, decrypted); }