summary refs log tree commit diff
path: root/crypto/test/src/openpgp/examples/ByteArrayHandler.cs
blob: 676db8766f1cd8c3ebe4f34953afafb80f6438e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections;
using System.IO;
using System.Text;

using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.Bcpg.OpenPgp.Examples
{
    /**
    * Simple routine to encrypt and decrypt using a passphrase.
    * This service routine provides the basic PGP services between
    * byte arrays.
    *
    * Note: this code plays no attention to -Console in the file name
    * the specification of "_CONSOLE" in the filename.
    * It also expects that a single pass phrase will have been used.
    *
    */
    public sealed class ByteArrayHandler
    {
        private ByteArrayHandler()
        {
        }

        /**
        * decrypt the passed in message stream
        *
        * @param encrypted  The message to be decrypted.
        * @param passPhrase Pass phrase (key)
        *
        * @return Clear text as a byte array.  I18N considerations are
        *         not handled by this routine
        * @exception IOException
        * @exception PgpException
        */
        public static byte[] Decrypt(
            byte[] encrypted,
            char[] passPhrase)
        {
            Stream inputStream = new MemoryStream(encrypted);

            inputStream = PgpUtilities.GetDecoderStream(inputStream);

            PgpObjectFactory pgpF = new PgpObjectFactory(inputStream);
            PgpEncryptedDataList enc = null;
            PgpObject o = pgpF.NextPgpObject();

			//
            // the first object might be a PGP marker packet.
            //
            if (o is PgpEncryptedDataList)
            {
                enc = (PgpEncryptedDataList) o;
            }
            else
            {
                enc = (PgpEncryptedDataList) pgpF.NextPgpObject();
            }

            PgpPbeEncryptedData pbe = (PgpPbeEncryptedData)enc[0];

            Stream clear = pbe.GetDataStream(passPhrase);

            PgpObjectFactory pgpFact = new PgpObjectFactory(clear);

            PgpCompressedData cData = (PgpCompressedData) pgpFact.NextPgpObject();

            pgpFact = new PgpObjectFactory(cData.GetDataStream());

            PgpLiteralData ld = (PgpLiteralData) pgpFact.NextPgpObject();

            Stream unc = ld.GetInputStream();

			return Streams.ReadAll(unc);
        }

        /**
        * Simple PGP encryptor between byte[].
        *
        * @param clearData  The test to be encrypted
        * @param passPhrase The pass phrase (key).  This method assumes that the
        *                   key is a simple pass phrase, and does not yet support
        *                   RSA or more sophisiticated keying.
        * @param fileName   File name. This is used in the Literal Data Packet (tag 11)
        *                   which is really inly important if the data is to be
        *                   related to a file to be recovered later.  Because this
        *                   routine does not know the source of the information, the
        *                   caller can set something here for file name use that
        *                   will be carried.  If this routine is being used to
        *                   encrypt SOAP MIME bodies, for example, use the file name from the
        *                   MIME type, if applicable. Or anything else appropriate.
        *
        * @param armor
        *
        * @return encrypted data.
        * @exception IOException
        * @exception PgpException
        */
        public static byte[] Encrypt(
            byte[]						clearData,
            char[]						passPhrase,
            string						fileName,
            SymmetricKeyAlgorithmTag	algorithm,
            bool						armor)
        {
            if (fileName == null)
            {
                fileName = PgpLiteralData.Console;
            }

			byte[] compressedData = Compress(clearData, fileName, CompressionAlgorithmTag.Zip);

			MemoryStream bOut = new MemoryStream();

			Stream output = bOut;
			if (armor)
			{
				output = new ArmoredOutputStream(output);
			}

			PgpEncryptedDataGenerator encGen = new PgpEncryptedDataGenerator(algorithm, new SecureRandom());
            encGen.AddMethod(passPhrase);

			Stream encOut = encGen.Open(output, compressedData.Length);

			encOut.Write(compressedData, 0, compressedData.Length);
			encOut.Close();
			
			if (armor)
			{
				output.Close();
			}

			return bOut.ToArray();
        }

		private static byte[] Compress(byte[] clearData, string fileName, CompressionAlgorithmTag algorithm)
		{
            MemoryStream bOut = new MemoryStream();

            PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm);
            Stream cos = comData.Open(bOut); // open it with the final destination
            PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator();

            // we want to Generate compressed data. This might be a user option later,
            // in which case we would pass in bOut.
            Stream pOut = lData.Open(
				cos,					// the compressed output stream
                PgpLiteralData.Binary,
                fileName,				// "filename" to store
                clearData.Length,		// length of clear data
                DateTime.UtcNow			// current time
            );

			pOut.Write(clearData, 0, clearData.Length);
			pOut.Close();

			comData.Close();

			return bOut.ToArray();
		}

		private static string GetAsciiString(byte[] bs)
		{
			return Encoding.ASCII.GetString(bs, 0, bs.Length);
		}

        public static void Main(
			string[] args)
        {
            string passPhrase = "Dick Beck";
            char[] passArray = passPhrase.ToCharArray();

            byte[] original = Encoding.ASCII.GetBytes("Hello world");
            Console.WriteLine("Starting PGP test");
            byte[] encrypted = Encrypt(original, passArray, "iway", SymmetricKeyAlgorithmTag.Cast5, true);

            Console.WriteLine("\nencrypted data = '"+Hex.ToHexString(encrypted)+"'");
            byte[] decrypted= Decrypt(encrypted,passArray);

            Console.WriteLine("\ndecrypted data = '"+GetAsciiString(decrypted)+"'");

            encrypted = Encrypt(original, passArray, "iway", SymmetricKeyAlgorithmTag.Aes256, false);

            Console.WriteLine("\nencrypted data = '"+Hex.ToHexString(encrypted)+"'");
            decrypted= Decrypt(encrypted, passArray);

            Console.WriteLine("\ndecrypted data = '"+GetAsciiString(decrypted)+"'");
        }
    }
}