Ejemplo n.º 1
0
        public static string DecryptPgpData(string inputData)
        {
            string output;

            using (Stream inputStream = IoHelper.GetStream(inputData))
            {
                using (Stream keyIn = File.OpenRead(PrivateKeyOnlyPath))
                {
                    output = DecryptPgpData(inputStream, keyIn, Password);
                }
            }
            return(output);
        }
Ejemplo n.º 2
0
        // Note: I was able to extract the private key into xml format .Net expecs with this
        public static string GetPrivateKeyXml(string inputData)
        {
            Stream           inputStream = IoHelper.GetStream(inputData);
            PgpObjectFactory pgpFactory  = new PgpObjectFactory(PgpUtilities.GetDecoderStream(inputStream));
            PgpObject        pgp         = null;

            if (pgpFactory != null)
            {
                pgp = pgpFactory.NextPgpObject();
            }

            PgpEncryptedDataList encryptedData = null;

            if (pgp is PgpEncryptedDataList)
            {
                encryptedData = (PgpEncryptedDataList)pgp;
            }
            else
            {
                encryptedData = (PgpEncryptedDataList)pgpFactory.NextPgpObject();
            }

            Stream privateKeyStream = File.OpenRead(PrivateKeyOnlyPath);

            // find secret key
            PgpSecretKeyRingBundle pgpKeyRing = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream));
            PgpPrivateKey          privateKey = null;

            foreach (PgpPublicKeyEncryptedData pked in encryptedData.GetEncryptedDataObjects())
            {
                privateKey = FindSecretKey(pgpKeyRing, pked.KeyId, Password.ToCharArray());
                if (privateKey != null)
                {
                    //pubKeyData = pked;
                    break;
                }
            }

            // get xml:
            RsaPrivateCrtKeyParameters rpckp = ((RsaPrivateCrtKeyParameters)privateKey.Key);
            RSAParameters dotNetParams       = DotNetUtilities.ToRSAParameters(rpckp);
            RSA           rsa = RSA.Create();

            rsa.ImportParameters(dotNetParams);
            string xmlPrivate = rsa.ToXmlString(true);

            return(xmlPrivate);
        }