Advertisement
Alx09

Untitled

Apr 13th, 2022
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApp3
  10. {
  11.     class Program
  12.     {
  13.        
  14.         static void Main(string[] args)
  15.         {
  16.             DSACryptoServiceProvider myDSA = new DSACryptoServiceProvider(1024);
  17.             byte[] signature1;
  18.             byte[] signature2;
  19.             Console.WriteLine("-------- I. --------");
  20.            
  21.            
  22.             Console.WriteLine("Cale Fiser: ");
  23.             string caleFiser = Console.ReadLine();
  24.             signature1 = myDSA.SignData(System.Text.Encoding.ASCII.GetBytes(caleFiser));
  25.             bool verified;
  26.             verified = myDSA.VerifyData(System.Text.Encoding.ASCII.GetBytes(caleFiser),  signature1);
  27.             Console.WriteLine(Encoding.Default.GetString(signature1));
  28.             if (verified)
  29.             {
  30.                     StreamWriter scrie = new StreamWriter("sig.txt");
  31.                     scrie.WriteLine(Encoding.ASCII.GetString(signature1));
  32.                     scrie.Close();
  33.             }
  34.             Console.WriteLine("-------- II. --------");
  35.             Console.WriteLine("Cale Fiser: ");
  36.              caleFiser = Console.ReadLine();
  37.             StreamReader read = new StreamReader("sig.txt");
  38.             signature1 = myDSA.SignData(System.Text.Encoding.ASCII.GetBytes(caleFiser));
  39.             char[] sir = read.ReadToEnd().ToCharArray();
  40.             signature2 = myDSA.SignData(Encoding.ASCII.GetBytes(sir, 0, sir.Length));
  41.             read.Close();//afisam semnatura pe ecran transformand variabila sig din byte in string pt a putea vedea pe consola semnatura
  42.             int n1, n2;
  43.             n1 = signature1.Length;
  44.             n2 = signature2.Length;
  45.             Console.WriteLine(n1);
  46.             Console.WriteLine(n2);
  47.             bool itSame = true;
  48.          
  49.                 for (int i = 0; i < n1; i++)
  50.                     if (signature1[i].CompareTo(signature2[i + 1]) == 0)
  51.                     {
  52.                         itSame = false;
  53.                         Console.WriteLine( "Semnaturi diferite ");
  54.                         break;
  55.                     }
  56.                 if (itSame)
  57.                 {
  58.                 Console.WriteLine( "Semnaturi identice");
  59.                 }
  60.            
  61.            
  62.             Console.WriteLine("-------- III. --------");
  63.             System.Diagnostics.Stopwatch swatch = new System.Diagnostics.Stopwatch();
  64.             SHA256Managed myHash = new SHA256Managed();
  65.             int count = 100;
  66.             byte[] signature;
  67.             swatch.Reset();
  68.             swatch.Start();
  69.             for (int i = 0; i < count; i++)
  70.             {
  71.                 signature = myDSA.SignData(signature1);
  72.             }
  73.             swatch.Stop();
  74.            
  75.             Console.WriteLine("Timpul semnarii: " + ((double)swatch.ElapsedMilliseconds / count) + " ms");
  76.  
  77.             Console.Read();  //scriem asta pt a mentine consola pe ecran, in caz contrar nu ramane pe ecran
  78.         }
  79.     }
  80. }
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement