public static byte[] EncryptPgp(byte[] input, byte[] publicKey)
        {
            using (MemoryStream publicKeyStream = new MemoryStream(publicKey))
                using (MemoryStream outputStream = new MemoryStream())
                    using (MemoryStream encryptedBytes = new MemoryStream())
                    {
                        using (Stream s = new PgpLiteralDataGenerator().Open(outputStream, PgpLiteralData.Binary, PgpLiteralDataGenerator.Console, input.Length, DateTime.Now))
                            using (Stream inputStream = new MemoryStream(input))
                            {
                                s.Write(input, 0, input.Length);
                            }

                        PgpPublicKey pubKey = ReadPublicKey(publicKeyStream);

                        PgpEncryptedDataGenerator dataGenerator = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Aes256, true, new SecureRandom());

                        dataGenerator.AddMethod(pubKey);

                        byte[] output = outputStream.ToArray();

                        using (Stream dgenStream = dataGenerator.Open(encryptedBytes, output.Length)) {
                            dgenStream.Write(output, 0, output.Length);
                        }

                        dataGenerator.Close();

                        return(encryptedBytes.ToArray());
                    }
        }
示例#2
0
        internal static byte[] Compress(string input, CompressionAlgorithmTag algorithmTag)
        {
            byte[] inputData = Encoding.Default.GetBytes(input);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                PgpCompressedDataGenerator compressedDataGenerator = new PgpCompressedDataGenerator(algorithmTag);

                using (Stream compressedStream = compressedDataGenerator.Open(memoryStream))
                    using (Stream outputStream = new PgpLiteralDataGenerator().Open(compressedStream, PgpLiteralData.Binary, PgpLiteralData.Console, inputData.Length, DateTime.Now))
                    {
                        outputStream.Write(inputData, 0, inputData.Length);
                    }
                compressedDataGenerator.Close();

                return(memoryStream.ToArray());
            }
        }