Last active
December 29, 2015 13:57
-
-
Save ikkentim/9893914 to your computer and use it in GitHub Desktop.
RSA bruteforcer
This file contains 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.Numerics; | |
namespace BruteForceRSA | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var processing = new Dictionary<PublicKey, BigInteger> | |
{ | |
//Key > Decrypt value pairs | |
[new PublicKey(267, 5)] = 5, | |
[new PublicKey(287, 41)] = 41, | |
[new PublicKey(473, 23)] = 23, | |
[new PublicKey(511, 5)] = 5, | |
[new PublicKey(623, 7)] = 7, | |
[new PublicKey(851, 13)] = 13, | |
[new PublicKey(1037, 43)] = 43, | |
[new PublicKey(1079, 23)] = 23, | |
[new PublicKey(1081, 43)] = 43, | |
[new PublicKey(1157, 7)] = 7, | |
[new PublicKey(1333, 41)] = 41, | |
[new PublicKey(1643, 7)] = 7, | |
[new PublicKey(1679, 89)] = 89, | |
[new PublicKey(1679, 5)] = 5, | |
[new PublicKey(1909, 7)] = 7, | |
[new PublicKey(2491, 7)] = 7, | |
[new PublicKey(2813, 5)] = 5, | |
[new PublicKey(2867, 11)] = 11, | |
[new PublicKey(3053, 163)] = 163, | |
[new PublicKey(3337, 11)] = 11, | |
[new PublicKey(3403, 13)] = 13, | |
[new PublicKey(3431, 29)] = 29, | |
[new PublicKey(6497, 7)] = 7 | |
}; | |
foreach (var process in processing) | |
{ | |
var privateKey = process.Key.GetPrivateKey(); | |
Console.WriteLine($"{process.Key} => {privateKey} ; " + | |
$"encrypted: {process.Value} => decrypted: {privateKey?.Decrypt(process.Value)}"); | |
} | |
} | |
#region Key structs | |
public struct PrivateKey | |
{ | |
public PrivateKey(BigInteger prime1, BigInteger prime2, BigInteger keyValue) : this() | |
{ | |
Prime1 = prime1; | |
Prime2 = prime2; | |
KeyValue = keyValue; | |
} | |
public BigInteger Prime1 { get; set; } | |
public BigInteger Prime2 { get; set; } | |
public BigInteger KeyValue { get; set; } | |
public BigInteger Modulus => Prime1*Prime2; | |
public BigInteger Decrypt(BigInteger c) | |
{ | |
return BigInteger.ModPow(c, KeyValue, Modulus); | |
} | |
public PublicKey GetPublicKey() | |
{ | |
BigInteger phi = (Prime1 - 1)*(Prime2 - 1); | |
BigInteger publicKeyValue; | |
for (BigInteger f = 0;; f += 2) | |
{ | |
BigInteger tmp = f*phi + 1; | |
if (tmp%KeyValue != 0) continue; | |
publicKeyValue = tmp/KeyValue; | |
break; | |
} | |
return new PublicKey {Modulus = Modulus, KeyValue = publicKeyValue}; | |
} | |
public override string ToString() | |
{ | |
return $"PrivateKey({Modulus}, {KeyValue}, (Primes: {Prime1}, {Prime2}))"; | |
} | |
} | |
public struct PublicKey | |
{ | |
public PublicKey(BigInteger modulus, BigInteger keyValue) | |
: this() | |
{ | |
Modulus = modulus; | |
KeyValue = keyValue; | |
} | |
public BigInteger KeyValue { get; set; } | |
public BigInteger Modulus { get; set; } | |
private static IEnumerable<BigInteger> CalculatePrimes(BigInteger min, BigInteger max) | |
{ | |
for (var test = min; test <= max; test++) | |
if (((Func<BigInteger, bool>) (n => | |
{ | |
if (n.IsEven) return false; | |
for (BigInteger t = 3; t < n / 2; t += 2) | |
if (n%t == 0) | |
return false; | |
return true; | |
}))(test)) | |
yield return test; | |
} | |
public PrivateKey? GetPrivateKey(BigInteger minimumPrime) | |
{ | |
var primes = CalculatePrimes(minimumPrime, Modulus/minimumPrime).ToList(); | |
BigInteger | |
prime1, | |
prime2 = 0; | |
var tmpThis = this; | |
if ( | |
(prime1 = | |
primes.FirstOrDefault(p => (prime2 = primes.FirstOrDefault(q => p*q == tmpThis.Modulus)) != 0)) == | |
0) | |
return null; | |
var phi = (prime1 - 1)*(prime2 - 1); | |
BigInteger privateKeyValue; | |
for (BigInteger f = 0;; f += 2) | |
{ | |
var tmp = f*phi + 1; | |
if (tmp%KeyValue != 0) continue; | |
privateKeyValue = tmp/KeyValue; | |
break; | |
} | |
return new PrivateKey {KeyValue = privateKeyValue, Prime1 = prime1, Prime2 = prime2}; | |
} | |
public PrivateKey? GetPrivateKey() | |
{ | |
return GetPrivateKey(2); | |
} | |
public BigInteger Encrypt(BigInteger n) | |
{ | |
return BigInteger.ModPow(n, KeyValue, Modulus); | |
} | |
public override string ToString() | |
{ | |
return $"PublicKey({Modulus}, {KeyValue})"; | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment