public static BigInteger2[] DivideBy(BigInteger2 Numb1, BigInteger2 Numb2) { var numb1 = BigInteger2.GetBytesTwosCompliment(Numb1); var numb2 = BigInteger2.GetBytesTwosCompliment(Numb2); BigInteger n1 = new BigInteger(numb1); BigInteger n2 = new BigInteger(numb2); BigInteger quot; BigInteger result = BigInteger.DivRem(n1, n2, out quot); BigInteger2[] results = new BigInteger2[2]; var bitAry = BigInteger2.ReverseBigIntegerBitsToBitArray(result.ToByteArray()); var whole = new BigInteger2(bitAry.Length, 0); BigInteger2.CopyFromBitArrayFromIndex(bitAry, whole, 0); var bitAry2 = BigInteger2.ReverseBigIntegerBitsToBitArray(quot.ToByteArray()); var quotient = new BigInteger2(bitAry2.Length, 0); BigInteger2.CopyFromBitArrayFromIndex(bitAry2, quotient, 0); results[0] = BigInteger2.NegateZeros(whole); results[1] = BigInteger2.NegateZeros(quotient); return(results); }
public void setPrivateKey(byte[] key) { BitArray bitArray = new BitArray(key); d = new BigInteger2(bitArray.Length, 0); BigInteger2.CopyFromBitArrayFromIndex(bitArray, d, 0); d = BigInteger2.NegateZeros(d); }
public void setPublicKey(byte[] key) { BitArray bitArray = new BitArray(key); e = new BigInteger2(bitArray.Length, 0); BigInteger2.CopyFromBitArrayFromIndex(bitArray, e, 0); e = BigInteger2.NegateZeros(e); }
public void setModValue(byte[] value) { BitArray bitArray = new BitArray(value); Mod = new BigInteger2(bitArray.Length, 0); BigInteger2.CopyFromBitArrayFromIndex(bitArray, Mod, 0); Mod = BigInteger2.NegateZeros(Mod); n = Mod.bitlength.Length - 2; //MessageBox.Show(""+(Mod.bitlength.Length - 2)); }
public byte[] Decrypt(String str) { ArrayList resultSet = new ArrayList(); BigInteger2[] message = splitMessage(Convert.FromBase64String(str), n, "decrypt"); GC.Collect(); for (int i = 0; i < message.Length; i++) { //message[i] = BigInteger2.NegateZeros(message[i]); BigInteger2 unitResult = BigIntegerExtensions.modPower(message[i], d, Mod); unitResult = BigInteger2.NegateZeros(unitResult); BigInteger2 result = new BigInteger2(unitResult.bitlength.Length - 3, 0); for (int x = 3, xj = 2; x < unitResult.bitlength.Length; x++, xj++) { result.bitlength[xj] = unitResult.bitlength[x]; } unitResult = result; resultSet.Add(unitResult); GC.Collect(); } //Create Full BigInteger2 containing bits from resultSet BigIntegers int bitSize = 0; foreach (BigInteger2 b in resultSet) { bitSize += (b.bitlength.Length - 2); } //MessageBox.Show("Whole decrypted Text length: " + bitSize); BigInteger2 wholeMessage = new BigInteger2(bitSize, 0); //Fill wholeMessage int curIndex = 2; foreach (BigInteger2 b in resultSet) { for (int i = 2; i < b.bitlength.Length; i++, curIndex++) { wholeMessage.bitlength[curIndex] = b.bitlength[i]; } } byte[] res = DeInject(wholeMessage); GC.Collect(); // MessageBox.Show("Decryption Completed!!!"); return(res); }
public static BigInteger2 gcd(BigInteger2 Numb1, BigInteger2 Numb2) { var numb1 = BigInteger2.GetBytesTwosCompliment(Numb1); var numb2 = BigInteger2.GetBytesTwosCompliment(Numb2); BigInteger n1 = new BigInteger(numb1); BigInteger n2 = new BigInteger(numb2); var result = BigInteger.GreatestCommonDivisor(n1, n2); var bitAry = BigInteger2.ReverseBigIntegerBitsToBitArray(result.ToByteArray()); var whole = new BigInteger2(bitAry.Length, 0); BigInteger2.CopyFromBitArrayFromIndex(bitAry, whole, 0); return(BigInteger2.NegateZeros(whole)); }
public static BigInteger2 modPower(BigInteger2 current, BigInteger2 power, BigInteger2 mod) { var numb1 = BigInteger2.GetBytesTwosCompliment(current); var numb2 = BigInteger2.GetBytesTwosCompliment(power); var numb3 = BigInteger2.GetBytesTwosCompliment(mod); BigInteger n1 = new BigInteger(numb1); BigInteger n2 = new BigInteger(numb2); BigInteger n3 = new BigInteger(numb3); var result = BigInteger.ModPow(n1, n2, n3); var bitAry = BigInteger2.ReverseBigIntegerBitsToBitArray(result.ToByteArray()); var whole = new BigInteger2(bitAry.Length, 0); BigInteger2.CopyFromBitArrayFromIndex(bitAry, whole, 0); return(BigInteger2.NegateZeros(whole)); }
public byte[] Encrypt(String str) { BigInteger2[] message = this.splitMessage(Encoding.UTF8.GetBytes(str), this.n - 1, "encrypt"); mess = new BigInteger2[message.Length]; mess = message; //MessageBox.Show("Message Length: " + message.Length); //return data; ArrayList resultSet = new ArrayList(); GC.Collect(); //Encrypt each Message and populate resultSet for (int i = 0; i < message.Length; i++) { BigInteger2 unitResult = BigIntegerExtensions.modPower(message[i], e, Mod); unitResult = BigInteger2.NegateZeros(unitResult); if (i == 0 && unitResult.bitlength.Length - 2 < n) { //MessageBox.Show("Padding with 'a'"); int ind = n + 4 - unitResult.bitlength.Length; BigInteger2 tmp = new BigInteger2(n, 0); for (int xi = ind, xj = 2; xj < unitResult.bitlength.Length; xi++, xj++) { tmp.bitlength[xi] = unitResult.bitlength[xj]; } unitResult = tmp; //PAD Left with byte 'a' byte[] pad = ASCIIEncoding.ASCII.GetBytes("a"); BitArray padAry = new BitArray(pad); //PAD left of unitResult with padAry BigInteger2 padded = new BigInteger2(unitResult.bitlength.Length - 2 + padAry.Length, 0); int pi, pj; for (pi = 2, pj = 0; pj < padAry.Length; pi++, pj++) { padded.bitlength[pi] = padAry[pj]; } //Continue filling from unitResult for (int indi = pi, indj = 2; indi < padded.bitlength.Length; indi++, indj++) { padded.bitlength[indi] = unitResult.bitlength[indj]; } unitResult = padded; } if (i > 0 && unitResult.bitlength.Length - 2 < n) { //MessageBox.Show("Padding with zeros"); //pad result's leftside with zeros int ind = n + 4 - unitResult.bitlength.Length; BigInteger2 tmp = new BigInteger2(n, 0); for (int xi = ind, xj = 2; xj < unitResult.bitlength.Length; xi++, xj++) { tmp.bitlength[xi] = unitResult.bitlength[xj]; } unitResult = null; unitResult = tmp; } GC.Collect(); resultSet.Add(unitResult); } //Create Full BigInteger2 containing bits from resultSet BigIntegers int bitSize = 0; foreach (BigInteger2 b in resultSet) { bitSize += (b.bitlength.Length - 2); } BigInteger2 wholeMessage = new BigInteger2(bitSize, 0); //Fill wholeMessage int curIndex = 2; foreach (BigInteger2 b in resultSet) { for (int i = 2; i < b.bitlength.Length; i++, curIndex++) { wholeMessage.bitlength[curIndex] = b.bitlength[i]; } } byte[] result = BigInteger2.getKeyBytes(wholeMessage); GC.Collect(); // MessageBox.Show("Encryption Completed!!!"); return(result); }
public void generateNewKey() { var testMessage = "[Administrator] wrote: ==>I am fed up of taking tests where you sore highly yet fail to progress to the next rounds of interviews. Given to abnormalities stated as team fit or cultural fit." + System.Environment.NewLine + " How can you say such crap, when you've never seen me sitted in your teams!!. Please visit and support the secure cryptology aspects of the ethical world, without the likes of Secret Service tampering or peeking through your mail: https://martinlayooinc.co.uk is therefore open to do business with you." + System.Environment.NewLine + "So I have taken up my projects under my own company umbrella, and winning: https://www.martinlayooinc.co.uk/Home/Product?prodId=checkMutableUber" + System.Environment.NewLine + " If in doubt of the buy and immediate download procedures or unsure whether you are spending money on value, then check out free samples and engage in the buying process for 0.01 - a penny.How can you discredit that.https://www.martinlayooinc.co.uk/Home/Product?prodId=checkAES" + System.Environment.NewLine + " Feel free to test drive my apps.Yes, to survive I am running my own business as I am fed up of spurious claims from employers amidst interviewing processes viewable link below this"; var testMessage2 = "If in doubt of the buy and immediate download procedures or unsure whether you are spending money on value, then check out free samples and engage in the buying process for 0.01 - a penny.How can you discredit that.https://www.martinlayooinc.co.uk/Home/Product?prodId=checkAES"; MessageBox.Show("RSA Public & Private Key Pair For Signing Is About to Get Generated. Please Click Ok, and Wait...\nYou will be notified when Keys are generated to Continue Process."); DateTime startTime = DateTime.Now; var tasks = new List <Task>(); Task tp = new TaskFactory().StartNew(() => { p = BigIntegerExtensions.getProbablePrime(num2);//should be a prime number p = BigInteger2.NegateZeros(p); GC.Collect(); }); tasks.Add(tp); //MessageBox.Show("P Got:"); Task tq = new TaskFactory().StartNew(() => { q = BigIntegerExtensions.getProbablePrime(num1);//should be a prime number q = BigInteger2.NegateZeros(q); //MessageBox.Show("p and q determined!!"); GC.Collect(); }); tasks.Add(tq); Task.WaitAll(tasks.ToArray()); Task tm = new TaskFactory().StartNew(() => { while (true) { try { while (p.gcd(p, q) != BigInteger2.ONE()) { q = BigIntegerExtensions.getProbablePrime(num1);//should be a prime number q = BigInteger2.NegateZeros(q); //MessageBox.Show("p and q determined!!"); GC.Collect(); } Mod = p * q; //MessageBox.Show("p Is:"); //MessageBox.Show(p.ToString()); //MessageBox.Show("q Is:"); //MessageBox.Show(q.ToString()); Mod = BigInteger2.NegateZeros(Mod); //MessageBox.Show("Mod Is:"); //MessageBox.Show(Mod.ToString()); n = Mod.bitlength.Length - 2; GC.Collect(); BigInteger2 px = p - BigInteger2.ONE(); BigInteger2 qx = q - BigInteger2.ONE(); BigInteger2 f = px * qx; f = BigInteger2.NegateZeros(f); GC.Collect(); while (true) { e = BigIntegerExtensions.getProbablePrime(n - 1); e = BigInteger2.NegateZeros(e); d = e.inverseMod(e, f); if (d == new BigInteger2(1, 0)) { continue; } d = BigInteger2.NegateZeros(d); break; } //MessageBox.Show("Found decryption Key"); //MessageBox.Show("Encryption Key Got:"); // MessageBox.Show(e.ToString()); //MessageBox.Show("Decryption Key Got:"); //MessageBox.Show(d.ToString()); GC.Collect(); } catch (Exception ex) { MessageBox.Show("Exception:" + ex.Message); } try { var resultBytes = Encrypt(testMessage); var encryptedString = Convert.ToBase64String(resultBytes); var decryptBytes = Decrypt(encryptedString); var decryptedString = ASCIIEncoding.UTF8.GetString(decryptBytes); var isKeys = false; if (decryptedString.Equals(testMessage)) { isKeys = true; } else { isKeys = false; } if (!isKeys) { continue; } resultBytes = Encrypt(testMessage2); encryptedString = Convert.ToBase64String(resultBytes); decryptBytes = Decrypt(encryptedString); decryptedString = ASCIIEncoding.UTF8.GetString(decryptBytes); if (decryptedString.Equals(testMessage2)) { DateTime endTime = DateTime.Now; TimeSpan tspan = endTime - startTime; MessageBox.Show(String.Format("Keys Set!! Key generation Time elapsed=>{0} Hours: {1} Minutes: {2} Seconds", tspan.Hours, tspan.Minutes, tspan.Seconds)); break; } } catch (Exception e) { MessageBox.Show(e.Message + System.Environment.NewLine + e.StackTrace); } } }); Task.WaitAny(tm); }
public byte[] getPrivateKey() { d = BigInteger2.NegateZeros(d); return(BigInteger2.getKeyBytes(d)); }
public byte[] getPublicKey() { e = BigInteger2.NegateZeros(e); return(BigInteger2.getKeyBytes(e)); }
public static BigInteger2 inverseMod(BigInteger2 numbe, BigInteger2 modf) { //Check to see that gcd is 1 before proceeding: gcd(numbe,modf)=1 ArrayList factors2 = new ArrayList(); int count = 0; BigInteger2 NUM = (BigInteger2)numbe.Clone(); BigInteger2 MOD = (BigInteger2)modf.Clone(); bool foundInverse = true; while (true) { BigInteger2[] temp = BigIntegerExtensions.DivideBy(modf, numbe); Factors2 x = new Factors2(); x.Mod = modf; x.fMod = (BigInteger2)BigInteger2.ONE().Clone(); x.fnumb = BigInteger2.NegateZeros((BigInteger2)temp[0].Clone()); x.fnumb.bitlength[0] = true; if (count == 0) { factors2.Add(x); //added only in first instance } count++; //iteration number //Look up for previous remainder for factor substitution in x: if (count == 2) { Factors2 prevx = (Factors2)factors2[0]; //Check for Factors2 to incorporate!! Factors2 tmpResolve = new Factors2(); Factors2 resCurr = new Factors2(); resCurr.fnumb = BigInteger2.NegateZeros(BigIntegerExtensions.Multiply(x.fnumb, prevx.fnumb)); resCurr.fMod = BigInteger2.NegateZeros(BigIntegerExtensions.Multiply(x.fnumb, prevx.fMod)); tmpResolve.fnumb = x.fMod + resCurr.fnumb; tmpResolve.fMod = resCurr.fMod; factors2.Add(tmpResolve); } if (count > 2) { Factors2 prev2Back = (Factors2)factors2[factors2.Count - 2]; Factors2 prevBack = (Factors2)factors2[factors2.Count - 1]; Factors2 resCurr = new Factors2(); resCurr.fnumb = BigInteger2.NegateZeros(BigIntegerExtensions.Multiply(x.fnumb, prevBack.fnumb)); resCurr.fMod = BigInteger2.NegateZeros(BigIntegerExtensions.Multiply(x.fnumb, prevBack.fMod)); resCurr.fnumb = prev2Back.fnumb + resCurr.fnumb; resCurr.fMod = prev2Back.fMod + resCurr.fMod; factors2.Add(resCurr); } if (temp[1] == BigInteger2.ONE()) { break; } if (temp[1] == new BigInteger2(1, 0)) { foundInverse = false; break; } modf = (BigInteger2)numbe.Clone(); numbe = (BigInteger2)temp[1].Clone(); } if (!foundInverse) { return(new BigInteger2(1, 0)); } //Calculate inverse mod Factors2 result = (Factors2)factors2[count - 1]; BigInteger2 invres = result.fnumb; if (!invres.bitlength[0]) { return((BigIntegerExtensions.DivideBy(invres, MOD))[1]); //if invres is positive } else // invres is negative { invres.bitlength[0] = false; if (invres < MOD) { return(BigInteger2.NegateZeros(MOD - invres)); } else { BigInteger2 test = BigIntegerExtensions.DivideBy(invres, MOD)[0] + BigInteger2.ONE(); return(BigInteger2.NegateZeros(BigIntegerExtensions.Multiply(test, MOD) - MOD)); } } }