I.Ren |
|
| Soluzione in c#: CODICE using System;
namespace funzioni { class funzioni /* Funzioni ausiliarie */ { public static bool Primo(int numero) { int pos = (numero + 1) / 2; while (pos > 1) { if (numero % pos == 0 && pos != numero && pos != 1) return false; pos--; } return true; } public static int[] Genera_Due_Primi(int min,int max) { int[] ret = new int[2]; Random ran_class = new Random(); int ran1 = ran_class.Next(min,max); while (Primo(ran1) != true) { ran1 = ran_class.Next(min,max); } int ran2 = ran_class.Next(min,max); while (Primo(ran2) != true && ran2 != ran1) { ran2 = ran_class.Next(min,max); } ret[0] = ran1; ret[1] = ran2; return ret; } public static int Genera_coprimo(int piupiccolodi) { Random ran_class = new Random(); int ran = ran_class.Next(piupiccolodi-1); while (Primo(ran) != true) { ran = ran_class.Next(piupiccolodi-1); } return ran; } public static int Potenza(int base_pot,int esponente) { int mol=0; mol = base_pot*base_pot; for (int i=0;i<esponente-2;i++) { mol = mol * base_pot; } return mol; } } }
namespace rsa { class Program { public static void Main(string[] args) { /* Inizializzazione dell'algoritmo */ int[] gen; int p,q,n,e,d; gen = funzioni.funzioni.Genera_Due_Primi(1,1000); p = gen[0]; q = gen[1]; n = p*q; e = funzioni.funzioni.Genera_coprimo((p-1)*(q-1)); d = (1/e) % ((p-1)-(q-1)); /* Applicazione dell'algoritmo */ int val = 2; int cript = (funzioni.funzioni.Potenza(val,d))%n; int decript = (funzioni.funzioni.Potenza(cript,e))%n; Console.WriteLine(cript); Console.WriteLine(decript); Console.ReadLine(); } } } Edited by MHack - 22/3/2011, 19:16
|
| |