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

Rsa Algorithm: Saad Bin Naeem BCS01113074 Muhammad Bilal Aslam BCS01113028 Usman Basharat BCS01113045

This document describes the RSA encryption algorithm and provides code samples for implementing it in C#. It includes sections for key generation, encryption/decryption functions, and variables/function implementation. The key generation section shows how to generate the public and private keys by selecting two prime numbers p and q, and then computing the modulus n and encryption/decryption exponents. The encryption/decryption functions use the .NET RSACryptoServiceProvider to encrypt and decrypt byte arrays. The code sample encrypts and decrypts plaintext and displays the results.

Uploaded by

saadbinnaeemch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Rsa Algorithm: Saad Bin Naeem BCS01113074 Muhammad Bilal Aslam BCS01113028 Usman Basharat BCS01113045

This document describes the RSA encryption algorithm and provides code samples for implementing it in C#. It includes sections for key generation, encryption/decryption functions, and variables/function implementation. The key generation section shows how to generate the public and private keys by selecting two prime numbers p and q, and then computing the modulus n and encryption/decryption exponents. The encryption/decryption functions use the .NET RSACryptoServiceProvider to encrypt and decrypt byte arrays. The code sample encrypts and decrypts plaintext and displays the results.

Uploaded by

saadbinnaeemch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

RSA ALGORITHM

Saad Bin Naeem BCS01113074


Muhammad Bilal Aslam BCS01113028
Usman Basharat BCS01113045


MAY 28, 2014
SUBMITTED TO: SIR WASEEM








CODE:
Key generator:
namespace cryptographer
{
public partial class Form1 : Form
{
int n = 0;
int s = 0, d = 0;
public Form1()
{
InitializeComponent();

}

public void btnprime_Click(object sender, EventArgs e)
{
int p=0, q = 0,z=0;

p = int.Parse(ptext.Text);
ptext.Text = ptext.Text;
q = int.Parse(qtext.Text);
if ((p != 0) && (q != 0) && (p != 1) && (q != 1))
{
for (int i = 2; i <= p/2; i++)
{
if (p % i == 0)
{
MessageBox.Show("enter prime number correctly");
break;
}
}
for (int i = 2; i <= q/2; i++)
{
if (p % i == 0)
{
MessageBox.Show("enter prime number correctly");
break;
}
}
n = p * q;
z = (p - 1) * (q - 1);
ntext.Text=n.ToString();

}
else
{
MessageBox.Show("Enter prime numbers correctly");

}
}

public void genratebtn_Click(object sender, EventArgs e)
{


s = int.Parse(etext.Text);
d = int.Parse(dtext.Text);
if (n % s == 0)
{
MessageBox.Show("enter any other value for e which is co prime with n");

if ((d * s) % n != 1)
{
MessageBox.Show("Enter any other value for d");
}

}
Encryption/Decryption:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace RSAEncryption
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{

}

#region-----Encryptionand Decryption Function-----
static public byte[] Encryption(byte[] Data, RSAParameters RSAKey, bool
DoOAEPPadding)
{
try
{
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAKey);
encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
}
return encryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);

return null;
}

}

static public byte[] Decryption(byte[] Data, RSAParameters RSAKey, bool
DoOAEPPadding)
{
try
{
byte[] decryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAKey);
decryptedData = RSA.Decrypt(Data, DoOAEPPadding);
}
return decryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.ToString());

return null;
}

}
#endregion

#region--variables area
UnicodeEncoding ByteConverter = new UnicodeEncoding();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
byte[] plaintext;
byte[] encryptedtext;
#endregion

#region-- Function Implemantation
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
byte[] decryptedtex = Decryption(encryptedtext, RSA.ExportParameters(true),
false);
txtdecrypt.Text = ByteConverter.GetString(decryptedtex);
}
#endregion

private void button1_Click_1(object sender, EventArgs e)
{
plaintext = ByteConverter.GetBytes(txtplain.Text);
encryptedtext = Encryption(plaintext, RSA.ExportParameters(false), false);
txtencrypt.Text = ByteConverter.GetString(encryptedtext);


}


}
}

You might also like