Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
PGP decryption
Message
De
01/12/2014 11:33:40
 
 
À
01/12/2014 10:02:55
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
VB 9.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01611506
Message ID:
01611617
Vues:
25
>>>Hi
>>>
>>>has anyone got a link to or an example of doing PGP decryption in c# Visual Studio.
>>>
>>>I had a quick search but nothing obvious jumped out at me
>>>
>>>Thanks
>>
>>The BouncyCastle library should work. Here is an example: http://stackoverflow.com/questions/10999542/bouncy-castle-c-sharp-pgp-decryption-example
>
>Hi Rob when I try and use the Bouncy castle example
> I'm getting an error "Can't find signing key in key ring." any ideas whats wrong there ?
>
>The user of the ftp site has supplied me with both private and public keys and passphrase but it doesn't seem to work. ?

Try this example:
public static string DecryptFile(Stream inputStream, Stream outputStream, PgpPrivateKey privateKey)
        {
            string retVal = string.Empty;
            try
            {
                using (Stream decoderStream = PgpUtilities.GetDecoderStream(inputStream))
                {
                    PgpObjectFactory pgpObjectFactory = new PgpObjectFactory(decoderStream);
                    PgpObject pgpObject = pgpObjectFactory.NextPgpObject();

                    //
                    // the first object might be a PGP marker packet.
                    //
                    PgpEncryptedDataList encryptedDataList = pgpObject as PgpEncryptedDataList;
                    if (encryptedDataList == null)
                    {
                        encryptedDataList = (PgpEncryptedDataList)pgpObjectFactory.NextPgpObject();
                    }

                    if (encryptedDataList.IsEmpty)
                        retVal = "Message does not contain encrypted data.";
                    else
                    {
                        PgpPublicKeyEncryptedData encryptedData = (PgpPublicKeyEncryptedData)encryptedDataList[0];

                        using (Stream clear = encryptedData.GetDataStream(privateKey))
                        {
                            PgpObjectFactory pgpFact = new PgpObjectFactory(clear);

                            //
                            // if we're trying to read a file generated by someone other than us
                            // the data might not be compressed, so we check the return type from
                            // the factory and behave accordingly.
                            //
                            pgpObject = pgpFact.NextPgpObject();
                            if (pgpObject is PgpCompressedData)
                            {
                                PgpCompressedData compressedData = (PgpCompressedData)pgpObject;
                                pgpFact = new PgpObjectFactory(compressedData.GetDataStream());
                                pgpObject = pgpFact.NextPgpObject();
                            }

                            PgpLiteralData literalData = (PgpLiteralData)pgpObject;
                            using (Stream literalDataStream = literalData.GetInputStream())
                            {
                                Streams.PipeAll(literalDataStream, outputStream);
                            }

                            if (encryptedData.IsIntegrityProtected())
                            {
                                if (!encryptedData.Verify())
                                {
                                    retVal = "Message could not be verified.";
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                retVal = ex.Message;
            }

            return retVal;
        }

public static PgpPrivateKey GetPrivateKey(Stream secretKeyStream, char[] passPhrase)
        {
            PgpPrivateKey retVal = null;
            PgpSecretKey secretKey = GetSecretKey(secretKeyStream);
            if (secretKey != null)
                retVal = secretKey.ExtractPrivateKey(passPhrase);

            return retVal;
        }
public static PgpSecretKey GetSecretKey(Stream secretKeyStream)
        {
            PgpSecretKey retVal = null;

            using (Stream decodedStream = PgpUtilities.GetDecoderStream(secretKeyStream))
            {
                PgpSecretKeyRingBundle keyRingBundle = new PgpSecretKeyRingBundle(decodedStream);

                var rings = keyRingBundle.GetKeyRings();
                var curKeyRing = rings.GetEnumerator();
                while (curKeyRing.MoveNext())
                {
                    PgpSecretKeyRing secretKeyRing = curKeyRing.Current as PgpSecretKeyRing;

                    if (secretKeyRing != null)
                    {
                        retVal = secretKeyRing.GetSecretKey();
                        break;
                    }
                }
            }

            return retVal;
        }
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform