Beispiel #1
0
        public static string ToXMLDocument(ANS1PrivateKey key)
        {
            StringBuilder result = new StringBuilder();

            WriteXMLDocument(result, key);
            return(result.ToString());
        }
Beispiel #2
0
 public static void WritePQDocument(StringBuilder stringBuilder, ANS1PrivateKey key)
 {
     stringBuilder.AppendLine($"<RSAKeyValue>");
     stringBuilder.AppendLine($"  <P>{key.P}</P>");
     stringBuilder.AppendLine($"  <Q>{key.Q}</Q>");
     stringBuilder.AppendLine($"</RSAKeyValue>");
     stringBuilder.Append(Environment.NewLine);
 }
        public static void AssertValidRSAPrivateKey(ANS1PrivateKey key)
        {
            BigInteger pMinusOne = (key.P - 1);
            BigInteger qMinusOne = (key.Q - 1);
            BigInteger phi       = BigInteger.Multiply(pMinusOne, qMinusOne);

            Contract.Assert(key.Modulus == (key.P * key.Q), "Modulus ≠ P*Q");
            Contract.Assert(key.DP == (key.D % pMinusOne), "DP ≢ D (mod P-1)");
            Contract.Assert(key.DQ == (key.D % qMinusOne), "DQ ≢ D (mod Q-1)");
            Contract.Assert(1 == ((key.D * key.Exponent) % phi), "D*Exponent ≢ 1 (mod phi(N))"); // (d) (e) mod phi(n) = 1
            Contract.Assert(1 == ((key.InverseQ * key.Q) % key.P), "Q*Q¯¹ ≢ 1 (mod P)");         //(InverseQ)(q) = 1 mod p
        }
Beispiel #4
0
        public static RSAParameters ToRSAParameters(ANS1PrivateKey key)
        {
            RSAParameters result = new RSAParameters();

            result.Modulus  = EncodingUtility.AsBytes(key.Modulus);
            result.Exponent = EncodingUtility.AsBytes(key.Exponent);
            result.P        = EncodingUtility.AsBytes(key.P);
            result.Q        = EncodingUtility.AsBytes(key.Q);
            result.DP       = EncodingUtility.AsBytes(key.DP);
            result.DQ       = EncodingUtility.AsBytes(key.DQ);
            result.D        = EncodingUtility.AsBytes(key.D);
            result.InverseQ = EncodingUtility.AsBytes(key.InverseQ);
            return(result);
        }
Beispiel #5
0
 public static void WriteXMLDocument(StringBuilder stringBuilder, ANS1PrivateKey key)
 {
     stringBuilder.AppendLine($"<RSAKeyValue>");
     stringBuilder.AppendLine($"  <Modulus>{key.Modulus}</Modulus>");
     stringBuilder.AppendLine($"  <Exponent>{key.Exponent}</Exponent>");
     stringBuilder.AppendLine($"  <P>{key.P}</P>");
     stringBuilder.AppendLine($"  <Q>{key.Q}</Q>");
     stringBuilder.AppendLine($"  <DP>{key.DP}</DP>");
     stringBuilder.AppendLine($"  <DQ>{key.DQ}</DQ>");
     stringBuilder.AppendLine($"  <InverseQ>{key.InverseQ}</InverseQ>");
     stringBuilder.AppendLine($"  <D>{key.D}</D>");
     stringBuilder.AppendLine($"</RSAKeyValue>");
     stringBuilder.Append(Environment.NewLine);
 }
        public void Begin()
        {
            if (CancelToken.IsCancellationRequested)
            {
                return;
            }

            FilePaths = SearchExtensions.SelectMany(searchPattern => Directory.EnumerateFiles(SearchDirectory, searchPattern, SearchOption.TopDirectoryOnly));
            //FilePaths = Directory.EnumerateFiles(SearchDirectory, SearchExtension, SearchOption.TopDirectoryOnly);

            if (FilePaths == null)
            {
                throw new Exception("No files to process!");
            }

            byte[]         bytes   = new byte[] { };
            ANS1PrivateKey ans1Key = null;
            StringBuilder  output  = new StringBuilder();

            List <string> filesToDelete = new List <string>();

            IEnumerable <string> pathBatch = FilePaths.Take(BatchSize);

            while (pathBatch.Any() && !CancelToken.IsCancellationRequested)
            {
                foreach (string file in pathBatch)
                {
                    bytes = GetEncodedBytes(file);

                    if (bytes == null)
                    {
                        continue;
                    }
                    else if (bytes == EmptyByteArray)
                    {
                        if (DeleteFiles)
                        {
                            filesToDelete.Add(file);
                        }
                    }

                    using (ans1Key = new ANS1PrivateKey(bytes))
                    {
                        ans1Key.ParseBuffer();

                        EncodingUtility.AssertValidRSAPrivateKey(ans1Key);

                        if (ExportOnlyPQ)
                        {
                            PrivateKeySerializer.WritePQDocument(output, ans1Key);
                        }
                        else
                        {
                            PrivateKeySerializer.WriteXMLDocument(output, ans1Key);
                        }

                        bytes = null;

                        if (DeleteFiles)
                        {
                            filesToDelete.Add(file);
                        }
                    }
                }

                File.AppendAllText(OutputFilename, output.ToString());
                output.Clear();
                bytes = null;

                if (DeleteFiles && filesToDelete.Any())
                {
                    foreach (string file in filesToDelete)
                    {
                        File.Delete(file);
                    }
                    filesToDelete.Clear();
                }

                FilePaths = FilePaths.Skip(BatchSize);
                pathBatch = FilePaths.Take(BatchSize);
            }
        }