Created
January 9, 2019 13:18
-
-
Save Traderain/e5c8197170d47226e84817768c9fbf9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Security.Principal; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace RSA | |
{ | |
class Program | |
{ | |
public static Random r = new Random(); | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Generating primes...."); | |
int p = GeneratePrime(); | |
int q = GeneratePrime(); | |
Console.WriteLine("P is " + p + " and Q is " + q); | |
int n = p * q; | |
Console.WriteLine("N is " + n); | |
int fi_n = (p-1)*(q-1); | |
Console.WriteLine("Fi n is " + fi_n); | |
int e = GetCoPrime(fi_n); | |
Console.WriteLine("Encryption key is " + e); | |
int d = 0; | |
Console.WriteLine("Generating decription key..."); | |
for (;;) | |
{ | |
d = r.Next(fi_n); | |
if ((d * e) % phi(n) == 1) | |
break; | |
} | |
Console.WriteLine("Decription key is: " + d); | |
Console.WriteLine("Please type some text to encrypt!"); | |
var encrypt = Console.ReadLine(); | |
var encrypted = Encrypt(encrypt, n, e); | |
var dec = Decrypt(encrypted, n, d); | |
Console.WriteLine("Decrypted text:" + dec); | |
Console.ReadKey(); | |
} | |
public static void cwt(string text) | |
{ | |
foreach (char t in text) | |
{ | |
Console.Write(t + "(" + (byte)t + "),"); | |
} | |
Console.Write("\n"); | |
} | |
public static List<int> Encrypt(string str, int n,int e) | |
{ | |
var bytes = Encoding.UTF8.GetBytes(str); | |
List<int> ret = new List<int>(); | |
for (int i = 0; i < bytes.Length; i++) | |
{ | |
ret.Add(PowMod(bytes[i],e, n)); | |
} | |
Console.WriteLine("Encrypted!"); | |
return ret; | |
} | |
public static string Decrypt(List<int> dec,int n, int d) | |
{ | |
List<int> ret = new List<int>(); | |
for (int i = 0; i < dec.Count; i++) | |
{ | |
ret.Add(PowMod(dec[i],d, n)); | |
} | |
return Encoding.UTF8.GetString(ret.Select(x => (byte)(x)).ToArray()); | |
} | |
public static int IntPow(int x, int pow) | |
{ | |
int ret = 1; | |
while ( pow != 0 ) | |
{ | |
if ( (pow & 1) == 1 ) | |
ret *= x; | |
x *= x; | |
pow >>= 1; | |
} | |
return ret; | |
} | |
public static int PowMod(int num, int pow,int mod) | |
{ | |
if (mod == 1) return 0; | |
var ret = 1; | |
num = num % mod; | |
while (pow > 0) | |
{ | |
if (pow % 2 == 1) | |
{ | |
ret = (ret * num) % mod; | |
} | |
pow = pow >> 1; | |
num = (num * num) % mod; | |
} | |
return ret; | |
} | |
public static int GetCoPrime(int range) | |
{ | |
for (;;) | |
{ | |
int num = r.Next(range); | |
if (gcd(num, range) == 1) | |
return num; | |
} | |
} | |
public static int phi( int n) | |
{ | |
int result = 1; | |
for (int i = 2; i < n; i++) | |
if (gcd(i, n) == 1) | |
result++; | |
return result; | |
} | |
public static int GeneratePrime() | |
{ | |
for (;;) | |
{ | |
int num = r.Next(100); | |
if (isPrime(num)) | |
return num; | |
} | |
} | |
public static int gcd(int a, int b) | |
{ | |
// Everything divides 0 | |
if (a == 0 || b == 0) | |
return 0; | |
// base case | |
if (a == b) | |
return a; | |
// a is greater | |
if (a > b) | |
return gcd(a - b, b); | |
return gcd(a, b - a); | |
} | |
public static bool isPrime(int number) | |
{ | |
if (number == 1) return false; | |
if (number == 2) return true; | |
var limit = Math.Ceiling(Math.Sqrt(number)); //hoisting the loop limit | |
for (int i = 2; i <= limit; ++i) { | |
if (number % i == 0) return false; | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment