예제 #1
0
        private string ExportPrivateKey()         //TextWriter outputStream)
        {
            StringBuilder pemPrivateKey = new StringBuilder();

            if (CryptoServiceProvider.PublicOnly)
            {
                throw new ArgumentException("CSP does not contain a private key", "csp");
            }
            var parameters = CryptoServiceProvider.ExportParameters(true);

            using (var stream = new MemoryStream()) {
                var writer = new BinaryWriter(stream);
                writer.Write((byte)0x30);                  // SEQUENCE
                using (var innerStream = new MemoryStream()) {
                    var innerWriter = new BinaryWriter(innerStream);
                    EncodeIntegerBigEndian(innerWriter, new byte[] { 0x00 });                   // Version
                    EncodeIntegerBigEndian(innerWriter, parameters.Modulus);
                    EncodeIntegerBigEndian(innerWriter, parameters.Exponent);
                    EncodeIntegerBigEndian(innerWriter, parameters.D);
                    EncodeIntegerBigEndian(innerWriter, parameters.P);
                    EncodeIntegerBigEndian(innerWriter, parameters.Q);
                    EncodeIntegerBigEndian(innerWriter, parameters.DP);
                    EncodeIntegerBigEndian(innerWriter, parameters.DQ);
                    EncodeIntegerBigEndian(innerWriter, parameters.InverseQ);
                    var length = (int)innerStream.Length;
                    EncodeLength(writer, length);
                    writer.Write(innerStream.GetBuffer(), 0, length);
                }

                var base64 = Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length).ToCharArray();
                //            outputStream.WriteLine("-----BEGIN RSA PRIVATE KEY-----");
                pemPrivateKey.AppendLine("-----BEGIN RSA PRIVATE KEY-----");
                // Output as Base64 with lines chopped at 64 characters
                for (var i = 0; i < base64.Length; i += 64)
                {
                    //                  outputStream.WriteLine(base64, i, Math.Min(64, base64.Length - i));
                    pemPrivateKey.AppendLine(new String(base64, i, Math.Min(64, base64.Length - i)));
                }
                //            outputStream.WriteLine("-----END RSA PRIVATE KEY-----");
                pemPrivateKey.AppendLine("-----END RSA PRIVATE KEY-----");
            }
            return(pemPrivateKey.ToString());
        }
예제 #2
0
        private String ExportPublicKeyToPemFormat(TextWriter outputStream)
        {
            var parameters = CryptoServiceProvider.ExportParameters(false);

            using (var stream = new MemoryStream()) {
                var writer = new BinaryWriter(stream);
                writer.Write((byte)0x30);                  // SEQUENCE
                using (var innerStream = new MemoryStream()) {
                    var innerWriter = new BinaryWriter(innerStream);
                    EncodeIntegerBigEndian(innerWriter, new byte[] { 0x00 });                   // Version
                    EncodeIntegerBigEndian(innerWriter, parameters.Modulus);
                    EncodeIntegerBigEndian(innerWriter, parameters.Exponent);

                    //All Parameter Must Have Value so Set Other Parameter Value Whit Invalid Data  (for keeping Key Structure  use "parameters.Exponent" value for invalid data)
                    EncodeIntegerBigEndian(innerWriter, parameters.Exponent);                     // instead of parameters.D
                    EncodeIntegerBigEndian(innerWriter, parameters.Exponent);                     // instead of parameters.P
                    EncodeIntegerBigEndian(innerWriter, parameters.Exponent);                     // instead of parameters.Q
                    EncodeIntegerBigEndian(innerWriter, parameters.Exponent);                     // instead of parameters.DP
                    EncodeIntegerBigEndian(innerWriter, parameters.Exponent);                     // instead of parameters.DQ
                    EncodeIntegerBigEndian(innerWriter, parameters.Exponent);                     // instead of parameters.InverseQ

                    var length = (int)innerStream.Length;
                    EncodeLength(writer, length);
                    writer.Write(innerStream.GetBuffer(), 0, length);
                }

                var base64 = Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length).ToCharArray();
                outputStream.WriteLine("-----BEGIN PUBLIC KEY-----");
                // Output as Base64 with lines chopped at 64 characters
                for (var i = 0; i < base64.Length; i += 64)
                {
                    outputStream.WriteLine(base64, i, Math.Min(64, base64.Length - i));
                }
                outputStream.WriteLine("-----END PUBLIC KEY-----");

                return(outputStream.ToString());
            }
        }