0% found this document useful (0 votes)
26 views

Cod Lab4 SI

This C# program measures the encryption, decryption, signing, and verification times for RSA cryptosystems with key sizes of 1024, 2048, 3072, and 4096 bits. It uses the RSACryptoServiceProvider and SHA256Managed classes from the System.Security.Cryptography namespace. The time_gen method performs the cryptographic operations for a given key size and plaintext, storing the elapsed times in an array. The main method calls time_gen in a loop based on the user's menu selection, and outputs the results.

Uploaded by

sergiu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Cod Lab4 SI

This C# program measures the encryption, decryption, signing, and verification times for RSA cryptosystems with key sizes of 1024, 2048, 3072, and 4096 bits. It uses the RSACryptoServiceProvider and SHA256Managed classes from the System.Security.Cryptography namespace. The time_gen method performs the cryptographic operations for a given key size and plaintext, storing the elapsed times in an array. The main method calls time_gen in a loop based on the user's menu selection, and outputs the results.

Uploaded by

sergiu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace ConsoleApplication21
{
class Program
{
public static double[] time_gen(int initialSize, byte[] plain) {
double[] results = new double[5];

RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider(1600);


System.Diagnostics.Stopwatch swatch = new
System.Diagnostics.Stopwatch();

int count = 100;


int size;

swatch.Start();

for (int i = 0; i < count; i++)


{
myrsa = new RSACryptoServiceProvider(initialSize);
size = myrsa.KeySize;
}
swatch.Stop();
Console.WriteLine("Encryption time at " + initialSize + " bits ... " +
(swatch.ElapsedTicks / (10 * count)).ToString() + " ms");
results[0] = (double)(swatch.ElapsedTicks / ((double)count));

byte[] ciphertext = myrsa.Encrypt(plain, true);


swatch.Reset();
swatch.Start();

for (int i = 0; i < count; i++)


{
ciphertext = myrsa.Encrypt(plain, true);
}
swatch.Stop();
Console.WriteLine("Encryption time at " + initialSize + " bits ... " +
(swatch.ElapsedTicks / (10 * count)).ToString() + " ms");
results[1] = (double)(swatch.ElapsedTicks / ((double)count));

swatch.Reset();
swatch.Start();
for (int i = 0; i < count; i++)
{
plain = myrsa.Decrypt(ciphertext, true);
}
swatch.Stop();
Console.WriteLine("Encryption time at " + initialSize + " bits ... " +
(swatch.ElapsedTicks / (10 * count)).ToString() + " ms");
results[2] = (double)(swatch.ElapsedTicks / ((double)count));

SHA256Managed myHash = new SHA256Managed();


byte[] signature = null;
swatch.Reset();
swatch.Start();
for (int i = 0; i < count; i++)
{
signature = myrsa.SignData(plain, myHash);
}
swatch.Stop();

results[3] = (double)(swatch.ElapsedTicks / (double)count);

swatch.Reset();
swatch.Start();
for (int i = 0; i < count; i++)
{
myrsa.VerifyData(plain, myHash, signature);
}
swatch.Stop();

results[4] = (double)(swatch.ElapsedTicks / (double)count);

return results;
}

static void Main(string[] args)


{
int optiune;
do
{
Console.WriteLine("Optiuni");
Console.WriteLine("1. 1024");
Console.WriteLine("2. 2048");
Console.WriteLine("3. 3072");
Console.WriteLine("4. 4096");
Console.WriteLine("0. EXIT");

try
{
optiune = Convert.ToInt16(Console.ReadLine());
}
catch (Exception err)
{
Console.WriteLine("Unknown option. Exiting...");
return;
}
double[] results = null;
switch (optiune)
{
case 1:
results = time_gen(1024,
Encoding.ASCII.GetBytes("myText"));
break;

case 2:
results = time_gen(2048,
Encoding.ASCII.GetBytes("myText"));
break;

case 3:
results = time_gen(3072,
Encoding.ASCII.GetBytes("myText"));
break;

case 4:
results = time_gen(4096,
Encoding.ASCII.GetBytes("myText"));
break;

case 0:
Console.WriteLine("Exiting...");
break;

default:
Console.WriteLine("Unknow Option");
break;
}
if (results != null)
{
Console.WriteLine("Timp generare: " + results[0].ToString());
Console.WriteLine("Timp Criptare: " + results[1].ToString());
Console.WriteLine("Timp Decriptare: " + results[2].ToString());
Console.WriteLine("Timp Semnare: " + results[3].ToString());
Console.WriteLine("Timp Verificare: " + results[4].ToString());
}
} while (optiune != 0);

}
}
}

You might also like