Created
July 1, 2017 18:03
-
-
Save mimura1133/0fc3a43df72043053be4fea43dc7e731 to your computer and use it in GitHub Desktop.
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.Globalization; | |
using System.IO; | |
using System.Linq; | |
using System.Numerics; | |
using System.Security.Cryptography; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace decrypt | |
{ | |
class Program | |
{ | |
private static BigInteger[] Extended_GCD(BigInteger A, BigInteger B) | |
{ | |
if (A < B) //if A less than B, switch them | |
{ | |
var temp = A; | |
A = B; | |
B = temp; | |
} | |
BigInteger r = B; | |
BigInteger x0 = 1, y0 = 0; | |
BigInteger x1 = 0, y1 = 1; | |
BigInteger x = 0, y = 0; | |
while (r > 1) | |
{ | |
r = A%B; | |
var q = A/B; | |
x = x0 - q*x1; | |
y = y0 - q*y1; | |
x0 = x1; | |
y0 = y1; | |
x1 = x; | |
y1 = y; | |
A = B; | |
B = r; | |
} | |
return new BigInteger[] {r, x, y}; | |
} | |
private static BigInteger[] KeyExtract(string first,string second) | |
{ | |
var c1 = | |
new X509Certificate2(X509Certificate.CreateFromCertFile(first)).PublicKey.Key as | |
RSACryptoServiceProvider; | |
var c2 = | |
new X509Certificate2(X509Certificate.CreateFromCertFile(second)).PublicKey.Key as | |
RSACryptoServiceProvider; | |
var cred1Mod = new BigInteger(c1.ExportParameters(false).Modulus.Reverse().Concat(new byte[] {0}).ToArray()); | |
var cred2Mod = new BigInteger(c2.ExportParameters(false).Modulus.Reverse().Concat(new byte[] {0}).ToArray()); | |
var surplus = BigInteger.One; | |
var i = BigInteger.Max(cred1Mod, cred2Mod); | |
var j = BigInteger.Min(cred1Mod, cred2Mod); | |
while (surplus != 0) | |
{ | |
surplus = i%j; | |
i = j; | |
j = surplus; | |
} | |
return new BigInteger[] {i, (cred1Mod/i), (cred2Mod/i)}; | |
} | |
private static RSAParameters makeParameters(BigInteger p, BigInteger q) | |
{ | |
var e = 65537; | |
if (p < q) | |
{ | |
var r = q; | |
q = p; | |
p = r; | |
} | |
BigInteger u; | |
var d = (Extended_GCD(e, (p - 1) * (q - 1))[2] + (p - 1) * (q - 1)) % ((p - 1) * (q - 1)); | |
var iq = (Extended_GCD(q, p)[2] + p) % p; | |
if ((iq * q) % p != 1) | |
throw new Exception("FAILED!"); | |
RSAParameters param; | |
var tmp = new RSACryptoServiceProvider(2048).ExportParameters(true); | |
param.P = p.ToByteArray().Take(tmp.P.Length).Reverse().ToArray(); | |
param.Q = q.ToByteArray().Take(tmp.Q.Length).Reverse().ToArray(); | |
param.D = d.ToByteArray().Take(tmp.D.Length).Reverse().ToArray(); | |
param.DP = (d % (p - 1)).ToByteArray().Take(tmp.DP.Length).Reverse().ToArray(); | |
param.DQ = (d % (q - 1)).ToByteArray().Take(tmp.DQ.Length).Reverse().ToArray(); | |
param.InverseQ = iq.ToByteArray().Take(tmp.InverseQ.Length).Reverse().ToArray(); | |
param.Exponent = new BigInteger(e).ToByteArray().Take(tmp.Exponent.Length).Reverse().ToArray(); | |
param.Modulus = (p * q).ToByteArray().Take(tmp.Modulus.Length).Reverse().ToArray(); | |
new RSACryptoServiceProvider().ImportParameters(param); | |
return param; | |
} | |
private static void Main(string[] args) | |
{ | |
var keys = KeyExtract("01.der", "02.der"); | |
using (var w = new StreamWriter(File.Create("01.pem"))) | |
{ | |
PEM.ExportPrivateKey(makeParameters(keys[0],keys[1]), w); | |
w.Flush(); | |
w.Close(); | |
} | |
using (var w = new StreamWriter(File.Create("02.pem"))) | |
{ | |
PEM.ExportPrivateKey(makeParameters(keys[0], keys[2]), w); | |
w.Flush(); | |
w.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment