1212import org .bouncycastle .openpgp .PGPPublicKeyEncryptedData ;
1313import org .bouncycastle .openpgp .PGPSecretKey ;
1414import org .bouncycastle .openpgp .PGPSecretKeyRing ;
15+ import org .bouncycastle .openpgp .PGPSessionKey ;
16+ import org .bouncycastle .openpgp .PGPSessionKeyEncryptedData ;
1517import org .bouncycastle .openpgp .bc .BcPGPObjectFactory ;
1618import org .bouncycastle .openpgp .operator .PublicKeyDataDecryptorFactory ;
19+ import org .bouncycastle .openpgp .operator .SessionKeyDataDecryptorFactory ;
1720import org .bouncycastle .openpgp .operator .bc .BcPublicKeyDataDecryptorFactory ;
21+ import org .bouncycastle .openpgp .operator .bc .BcSessionKeyDataDecryptorFactory ;
1822import org .bouncycastle .util .io .Streams ;
1923
2024import java .io .ByteArrayInputStream ;
@@ -36,7 +40,8 @@ public void performTest()
3640 throws Exception
3741 {
3842 decryptMessageEncryptedUsingPKESKv6 ();
39- encryptDecryptMessageUsingV6GopenpgpTestKey ();
43+ decryptMessageUsingV6GopenpgpTestKey ();
44+ decryptMessageUsingSessionKey ();
4045 }
4146
4247 private void decryptMessageEncryptedUsingPKESKv6 ()
@@ -99,7 +104,7 @@ private void decryptMessageEncryptedUsingPKESKv6()
99104 Streams .readAll (lit .getDataStream ()));
100105 }
101106
102- private void encryptDecryptMessageUsingV6GopenpgpTestKey ()
107+ private void decryptMessageUsingV6GopenpgpTestKey ()
103108 throws IOException , PGPException
104109 {
105110 // Ed448/X448 test key
@@ -165,6 +170,38 @@ private void encryptDecryptMessageUsingV6GopenpgpTestKey()
165170 Streams .readAll (lit .getDataStream ()));
166171 }
167172
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+
168205 public static void main (String [] args )
169206 {
170207 runTest (new PGPv6MessageDecryptionTest ());
0 commit comments