12
12
import org .bouncycastle .openpgp .PGPPublicKeyEncryptedData ;
13
13
import org .bouncycastle .openpgp .PGPSecretKey ;
14
14
import org .bouncycastle .openpgp .PGPSecretKeyRing ;
15
+ import org .bouncycastle .openpgp .PGPSessionKey ;
16
+ import org .bouncycastle .openpgp .PGPSessionKeyEncryptedData ;
15
17
import org .bouncycastle .openpgp .bc .BcPGPObjectFactory ;
16
18
import org .bouncycastle .openpgp .operator .PublicKeyDataDecryptorFactory ;
19
+ import org .bouncycastle .openpgp .operator .SessionKeyDataDecryptorFactory ;
17
20
import org .bouncycastle .openpgp .operator .bc .BcPublicKeyDataDecryptorFactory ;
21
+ import org .bouncycastle .openpgp .operator .bc .BcSessionKeyDataDecryptorFactory ;
18
22
import org .bouncycastle .util .io .Streams ;
19
23
20
24
import java .io .ByteArrayInputStream ;
@@ -36,7 +40,8 @@ public void performTest()
36
40
throws Exception
37
41
{
38
42
decryptMessageEncryptedUsingPKESKv6 ();
39
- encryptDecryptMessageUsingV6GopenpgpTestKey ();
43
+ decryptMessageUsingV6GopenpgpTestKey ();
44
+ decryptMessageUsingSessionKey ();
40
45
}
41
46
42
47
private void decryptMessageEncryptedUsingPKESKv6 ()
@@ -99,7 +104,7 @@ private void decryptMessageEncryptedUsingPKESKv6()
99
104
Streams .readAll (lit .getDataStream ()));
100
105
}
101
106
102
- private void encryptDecryptMessageUsingV6GopenpgpTestKey ()
107
+ private void decryptMessageUsingV6GopenpgpTestKey ()
103
108
throws IOException , PGPException
104
109
{
105
110
// Ed448/X448 test key
@@ -165,6 +170,38 @@ private void encryptDecryptMessageUsingV6GopenpgpTestKey()
165
170
Streams .readAll (lit .getDataStream ()));
166
171
}
167
172
173
+ private void decryptMessageUsingSessionKey ()
174
+ throws IOException , PGPException
175
+ {
176
+ // created using gosop 430bb02923c123e39815814f6b97a6d501bdde6a
177
+ // ./gosop encrypt --profile=rfc9580 cert.asc < msg.plain > msg.asc
178
+ String MSG = "-----BEGIN PGP MESSAGE-----\n " +
179
+ "\n " +
180
+ "wYUGIQaz5Iy7+n5O1bg87Cy2PfSolKK6L8cwIPLJnEeZFjMu2xoAfSM/MwQpXahy\n " +
181
+ "Od1pknhDyw3X5EgxQG0EffQCMpaKsNtqvVGYBJ5chuAcV/8gayReP/g6RREGeyj4\n " +
182
+ "Vc2dgJ67/KwaP0Z7k7vExHs79U24DsrU088QbYhk/XLvJHWlXXj90loCCQMMIvmD\n " +
183
+ "KS5f5WYbntB4N+FspsbQ7GN6taOrAqUtEuKWKzrlhZdtg9qGG4RLCvX1vfL0u6NV\n " +
184
+ "Yzk9fGVgty73B8pmyYdefLdWt87ljwr8wGGX/Dl8PSBIE3w=\n " +
185
+ "-----END PGP MESSAGE-----\n " ;
186
+ String SESSION_KEY = "9:47343387303C170873252051978966871EE2EA0F68D975F061AF022B78B165C1" ;
187
+
188
+ ByteArrayInputStream bIn = new ByteArrayInputStream (MSG .getBytes (StandardCharsets .UTF_8 ));
189
+ ArmoredInputStream aIn = new ArmoredInputStream (bIn );
190
+ BCPGInputStream pIn = new BCPGInputStream (aIn );
191
+ PGPObjectFactory objFac = new BcPGPObjectFactory (pIn );
192
+ PGPEncryptedDataList encList = (PGPEncryptedDataList ) objFac .nextObject ();
193
+ PGPSessionKeyEncryptedData encData = encList .extractSessionKeyEncryptedData ();
194
+ SessionKeyDataDecryptorFactory decryptor = new BcSessionKeyDataDecryptorFactory (
195
+ PGPSessionKey .fromAsciiRepresentation (SESSION_KEY ));
196
+
197
+ InputStream decrypted = encData .getDataStream (decryptor );
198
+ PGPObjectFactory decFac = new BcPGPObjectFactory (decrypted );
199
+ PGPLiteralData lit = (PGPLiteralData ) decFac .nextObject ();
200
+ isEncodingEqual ("Message plaintext mismatch" ,
201
+ "Hello, World!\n " .getBytes (StandardCharsets .UTF_8 ),
202
+ Streams .readAll (lit .getDataStream ()));
203
+ }
204
+
168
205
public static void main (String [] args )
169
206
{
170
207
runTest (new PGPv6MessageDecryptionTest ());
0 commit comments