//calculate inverse matrix public void inverseMatrix() { getValues(); EuclidExtended ee = new EuclidExtended(long.Parse(detFunc().ToString()), long.Parse(moduo.ToString())); EuclidExtendedSolution result = ee.calculate(); float pom1 = float.Parse(label13.Text); float pom2 = float.Parse(label12.Text); float pom3 = float.Parse(label11.Text); float pom4 = float.Parse(label10.Text); a11I = mod(result.X * pom1, moduo); a12I = mod(result.X * pom2, moduo); a21I = mod(result.X * pom3, moduo); a22I = mod(result.X * pom4, moduo); a11LBL.Text = a11I.ToString(); a12LBL.Text = a12I.ToString(); a21LBL.Text = a21I.ToString(); a22LBL.Text = a22I.ToString(); }
private void button1_Click(object sender, EventArgs e) { getValues(); EuclidExtended ee = new EuclidExtended(long.Parse(detFunc().ToString()), long.Parse(moduo.ToString())); EuclidExtendedSolution result = ee.calculate(); if (result.D == 1) { detLBL.Text = detFunc().ToString(); adjA(); inverseMatrix(); } else { MessageBox.Show("Invalid key! It's impossible to find an inverse matrix!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } }
static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; Random rnd = new Random(); for (int k = 0; k < 1; k++) { Console.WriteLine("Please enter length of prime number 3 - 12"); int lenght = int.Parse(Console.ReadLine()); /*rnd.Next(3, 13);*/ BigInteger p1 = GeneratePrimeNumber(lenght); BigInteger p2 = GeneratePrimeNumber(lenght); Console.WriteLine($"p1= {p1.ToString().Length} {Environment.NewLine} p2= {p2.ToString().Length}"); BigInteger n = BigInteger.Multiply(p1, p2); BigInteger phiN = BigInteger.Multiply((p1 - 1), (p2 - 1)); Console.WriteLine($"Phi: {phiN} -> length {phiN.ToString().Length}"); BigInteger e = GeneratePublicExponentE(lenght, phiN); Console.WriteLine($"e= {e}"); EuclidExtended ee = new EuclidExtended(e, phiN); EuclidExtendedSolution result = ee.calculate(); BigInteger d = result.X < 0 ? BigInteger.Subtract(phiN, BigInteger.Abs(result.X)) : result.X; Console.WriteLine($"d={d}"); if (e > n) { Console.WriteLine("Sorry try again. n must must be biggest from e"); } string messages = Console.ReadLine(); /*"CRYPTED";*/ byte[] messagesInBytes = Encoding.UTF8.GetBytes(messages); BigInteger[] cryptedMessages = new BigInteger[messages.Length]; foreach (var item in messagesInBytes) { if (item > n) { Console.WriteLine("Messages is broken. m must be smaller than n "); return; } } Console.Write($"Crypted messages: "); for (int i = 0; i < messagesInBytes.Length; i++) { cryptedMessages[i] = BigInteger.ModPow(messagesInBytes[i], e, n); Console.Write($"{(char)((cryptedMessages[i]) % char.MaxValue)} "); } BigInteger[] encryptedMessages = new BigInteger[messages.Length]; Console.WriteLine(); Console.Write($"Encrypted messages: "); for (int i = 0; i < cryptedMessages.Length; i++) { encryptedMessages[i] = BigInteger.ModPow(cryptedMessages[i], d, n); Console.Write($"{(char)(encryptedMessages[i])}"); } Console.WriteLine(); } Console.ReadKey(); }