feat: add the option to save keys to a folder

* user runs keygen
* selects a folder that will home the keys
* keys are generated and saved to a predefined location
* set all possible fields and methods as private
This commit is contained in:
citizen-VM 2021-01-11 03:36:34 +01:00
parent da04346f26
commit 65b3376d94
Signed by: wanderer
GPG Key ID: 6391444A736EEE7E
2 changed files with 64 additions and 33 deletions

View File

@ -389,7 +389,31 @@ namespace KRY_0x04
internal void genprimes_handler(TextBox threadsbox, Label threads_warning)
{
rsapls.genprimes_handler(threadsbox, threads_warning);
MessageBox.Show("in the next step, you will be choosing path for saving the key pair", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
string path = "";
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.Description = "pick a location for your new keypair";
dlg.ShowNewFolderButton = true;
DialogResult dr = dlg.ShowDialog();
if (dr == DialogResult.OK)
{
try
{
path = dlg.SelectedPath;
}
catch (Exception excep)
{
MessageBox.Show("error: " + excep.ToString(), "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else if (dr == DialogResult.Cancel)
{
MessageBox.Show("error: you have to choose a path", "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
rsapls.genprimes_handler(threadsbox, threads_warning, path);
}
}
}

71
Rsa.cs
View File

@ -1,37 +1,34 @@
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Text.RegularExpressions;
using System.IO;
namespace KRY_0x04
{
class Rsa
{
protected bool encr = true;
protected int enc = 1;
RandomNumberGenerator rng = new RNGCryptoServiceProvider();
static int block_size = ((Convert.ToInt32(key_size.b1024) * 2) / 8) - 1;
static int b1024 = Convert.ToInt32(key_size.b1024); //512b primes
static int b2048 = Convert.ToInt32(key_size.b2048);
static int b3072 = Convert.ToInt32(key_size.b3072);
static int b4096 = Convert.ToInt32(key_size.b4096);
static int randbytesize = 0;
static BigInteger P = 0;
static BigInteger Q = 0;
static BigInteger E = 0;
static BigInteger D = 0;
static BigInteger N = 0;
static bool isprime_p, isprime_q;
static int candidates_tried;
static DateTime gp_start, gp_finish;
static int numThreads = 3;
int custom_threads = 0;
private RandomNumberGenerator rng = new RNGCryptoServiceProvider();
private static int block_size = ((Convert.ToInt32(key_size.b1024) * 2) / 8) - 1;
private static int b1024 = Convert.ToInt32(key_size.b1024); //512b primes
private static int b2048 = Convert.ToInt32(key_size.b2048);
private static int b3072 = Convert.ToInt32(key_size.b3072);
private static int b4096 = Convert.ToInt32(key_size.b4096);
private static int randbytesize = 0;
private static BigInteger P = 0;
private static BigInteger Q = 0;
private static BigInteger E = 0;
private static BigInteger D = 0;
private static BigInteger N = 0;
private static bool isprime_p, isprime_q;
private static int candidates_tried;
private static DateTime gp_start, gp_finish;
private static int numThreads = 3;
private int custom_threads = 0;
enum key_size : uint
{
@ -95,14 +92,14 @@ namespace KRY_0x04
}
}
static int mr_min_checks(int bits)
private static int mr_min_checks(int bits)
{
if (bits > 2048)
return 128;
return 64;
}
public static int[] low_primes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
private static int[] low_primes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269,
271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367,
@ -113,7 +110,7 @@ namespace KRY_0x04
787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883,
887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};
bool is_huge_prime(BigInteger source, int mr_min_rounds)
private bool is_huge_prime(BigInteger source, int mr_min_rounds)
{
for (int i = 0; i < low_primes.Length; i++)
{
@ -169,7 +166,7 @@ namespace KRY_0x04
return true;
}
BigInteger mmi(BigInteger a, BigInteger nn)
private BigInteger mmi(BigInteger a, BigInteger nn)
{
BigInteger i = nn, v = BigInteger.Zero, d = BigInteger.One;
while (a > BigInteger.Zero)
@ -187,8 +184,13 @@ namespace KRY_0x04
}
internal void genprimes_handler(TextBox threadsbox, Label threads_warning)
internal void genprimes_handler(TextBox threadsbox, Label threads_warning, string path)
{
string privkeypath = Path.Combine(path, "privkey-" + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".pri");
string pubkeypath = Path.Combine(path, "pubkey-" + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".pub");
string privkey = "";
string pubkey = "";
ManualResetEvent resetEvent = new ManualResetEvent(false);
int toProcess = 0;
tb_check(threadsbox, threads_warning);
@ -239,12 +241,9 @@ namespace KRY_0x04
Console.WriteLine($"[*] p:\n{P}");
Console.WriteLine($"[*] q:\n{Q}\n");
//textBox1.Text = Convert.ToString(P);
//textBox2.Text = Convert.ToString(Q);
N = P * Q;
Console.WriteLine($"[*] n:\n{N}\n");
//textBox3.Text = Convert.ToString(N);
Console.WriteLine("[*] eulerFunction = (p - 1) * (q - 1)");
BigInteger eulerFunction = (P - 1) * (Q - 1);
@ -263,12 +262,10 @@ namespace KRY_0x04
Console.WriteLine($"[*] gcd(e, eulerFunction) = {BigInteger.GreatestCommonDivisor(E, eulerFunction)}");
Console.WriteLine($"[*] e:\n{E}\n");
//textBox4.Text = Convert.ToString(E);
Console.WriteLine($"d = e exp -1");
D = mmi(E, eulerFunction);
Console.WriteLine($"[*] d:\n{D}\n");
//textBox5.Text = Convert.ToString(D);
Console.WriteLine("[*] check inverse: e * d mod eulerFunction = 1");
Console.WriteLine($"{E} * {D} mod {eulerFunction} = {(E * D) % eulerFunction}\n");
@ -285,9 +282,19 @@ namespace KRY_0x04
Console.WriteLine($"[*] generating primes took: {gp_finish - gp_start}");
Console.WriteLine("\n");
privkey = string.Join(" ", N, D);
pubkey = string.Join(" ", N, E);
write_keys(privkey, privkeypath, pubkey, pubkeypath);
MessageBox.Show("got primes y'all", "info");
}
private void write_keys(string privkey, string privkeypath, string pubkey, string pubkeypath)
{
File.WriteAllText(privkeypath, privkey);
File.WriteAllText(pubkeypath, pubkey);
}
private int threads_valid(string input)
{
int returnme = 0;
@ -332,7 +339,7 @@ namespace KRY_0x04
{
tb.BackColor = Color.PaleVioletRed;
}
void tb_valid_input(TextBox tb)
private void tb_valid_input(TextBox tb)
{
tb.BackColor = Color.White;
}