0% found this document useful (0 votes)
16 views4 pages

Using Using Using Using Namespace Class: "Enter P" "P " "Enter Q" "Q "

This document contains code for an RSA encryption/decryption program in C#. It defines a class called rsa that contains methods for encryption (enc()) and decryption (dec()). The enc() method takes in plaintext, performs RSA encryption using user-input values for p, q, and e, and outputs the ciphertext. The dec() method takes in ciphertext, performs RSA decryption using d, and outputs the original plaintext. The Main method prompts the user to choose encryption or decryption and calls the corresponding rsa method.

Uploaded by

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

Using Using Using Using Namespace Class: "Enter P" "P " "Enter Q" "Q "

This document contains code for an RSA encryption/decryption program in C#. It defines a class called rsa that contains methods for encryption (enc()) and decryption (dec()). The enc() method takes in plaintext, performs RSA encryption using user-input values for p, q, and e, and outputs the ciphertext. The dec() method takes in ciphertext, performs RSA decryption using d, and outputs the original plaintext. The Main method prompts the user to choose encryption or decryption and calls the corresponding rsa method.

Uploaded by

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

using System;

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

namespace RSA
{
class rsa
{
//p=11 q=3
public double d=1, p, q, n, phi, e = 3;
public int i = 0,k=0;
public string plain;
double [] plain1 = new double[25];

int[] cipher2 = new int[25];

public void enc()


{

Console.WriteLine("enter p");
p = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("p=");
Console.WriteLine(p);
Console.WriteLine("enter q");
q = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("q=");
Console.WriteLine(q);
n = p * q;
Console.WriteLine("n=p*q");
Console.WriteLine(n);
phi = (p - 1) * (q - 1);
Console.WriteLine("phi=(p-1)*(q-1)");
Console.WriteLine(phi);
Console.WriteLine("e=");
Console.WriteLine(e);
/*
d = (Math.Pow(e, -1)) % phi;* */
while (((d * e) % n) != 1)
{
d++;
Console.WriteLine("d=");
Console.WriteLine(d);
}

int g = 0;
while (g != 1)
{

i = 0;

Console.WriteLine("enter plaintext");
plain = Console.ReadLine();
foreach (char c in plain)
{
plain1[i] = (c - 97);
i++;
}
double[] cipher1 = new double[i];

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


{
cipher1[j] = Math.Pow(plain1[j], e) % n ;
Console.Write(Convert .ToChar (Convert .ToInt16 (cipher1[j]+97)));
} Console.WriteLine();
}
}
public void dec()
{
Console.WriteLine("enter p");
p = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("p=");
Console.WriteLine(p);
Console.WriteLine("enter q");
q = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("q=");
Console.WriteLine(q);
n = p * q;
Console.WriteLine("n=p*q");
Console.WriteLine(n);
phi = (p - 1) * (q - 1);
Console.WriteLine("phi=(p-1)*(q-1)");
Console.WriteLine(phi);
Console.WriteLine("e=");
Console.WriteLine(e);
/*
d = (Math.Pow(e, -1)) % phi;* */
Console.WriteLine("d=");
Console.WriteLine(d);

int g = 0;
while (g != 1)
{

k = 0;
string cipher;
Console.WriteLine("enter ciphertext");
cipher = Console.ReadLine();
foreach (char c in cipher)
{
cipher2[k] = (c - 97);
k++;
}
double[] plain2 = new double[k];
for (int j = 0; j < k; j++)
{
plain2[j] = (Math.Pow(cipher2[j], d)) % n;
Console.Write(Convert.ToChar(Convert.ToInt16(plain2[j] + 97)));
} Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("1=encryption");
Console.WriteLine("2=decryption");
int s;
s =Convert .ToInt16 ( Console.ReadLine());

rsa t = new rsa();


if (s == 1)
{
t.enc();
}
else if (s == 2)
{
t.dec();
}

}
}
}

You might also like