11package com .github .renovatebot ;
22
33import java .io .ByteArrayInputStream ;
4- import java .io .ByteArrayOutputStream ;
54import java .io .IOException ;
65import java .nio .charset .StandardCharsets ;
76
87import org .bouncycastle .bcpg .ArmoredInputStream ;
9- import org .bouncycastle .openpgp .PGPCompressedData ;
10- import org .bouncycastle .openpgp .PGPEncryptedDataList ;
118import org .bouncycastle .openpgp .PGPException ;
12- import org .bouncycastle .openpgp .PGPLiteralData ;
13- import org .bouncycastle .openpgp .PGPObjectFactory ;
14- import org .bouncycastle .openpgp .PGPPrivateKey ;
15- import org .bouncycastle .openpgp .PGPPublicKeyEncryptedData ;
16- import org .bouncycastle .openpgp .PGPSecretKeyRingCollection ;
17- import org .bouncycastle .openpgp .operator .bc .BcKeyFingerprintCalculator ;
18- import org .bouncycastle .openpgp .operator .bc .BcPublicKeyDataDecryptorFactory ;
19- import org .bouncycastle .util .io .Streams ;
209import org .teavm .jso .JSExport ;
2110
2211public final class Main {
@@ -28,61 +17,11 @@ private Main() {
2817 @ JSExport
2918 public static String decrypt (String key , String msg ) throws IOException , PGPException {
3019 final var builder = ArmoredInputStream .builder ().setIgnoreCRC (true );
31- final var input = builder .build (new ByteArrayInputStream (msg .getBytes (StandardCharsets .UTF_8 )));
32- final var pgpFactory = new PGPObjectFactory (input , new BcKeyFingerprintCalculator ());
33-
34- var firstObject = pgpFactory .nextObject ();
35- if (!(firstObject instanceof PGPEncryptedDataList )) {
36- firstObject = pgpFactory .nextObject ();
37- }
38-
39- final var keyStream = builder .build (new ByteArrayInputStream (key .getBytes (StandardCharsets .UTF_8 )));
40- final var keyRing = new PGPSecretKeyRingCollection (
41- keyStream ,
42- new BcKeyFingerprintCalculator ());
43-
44- PGPPrivateKey keyToUse = null ;
45- PGPPublicKeyEncryptedData encryptedData = null ;
46- final var encObjects = ((PGPEncryptedDataList ) firstObject ).getEncryptedDataObjects ();
47-
48- while (keyToUse == null && encObjects .hasNext ()) {
49- encryptedData = (PGPPublicKeyEncryptedData ) encObjects .next ();
50- final var k = keyRing .getSecretKey (encryptedData .getKeyIdentifier ().getKeyId ());
51- if (k != null ) {
52- keyToUse = k .extractPrivateKey (null );
53- break ;
54- }
55- }
56-
57- if (keyToUse == null ) {
58- throw new PGPException ("Cannot find secret key for message." );
59- }
60-
61- final var clearText = encryptedData .getDataStream (new BcPublicKeyDataDecryptorFactory (keyToUse ));
62- var message = new PGPObjectFactory (clearText , new BcKeyFingerprintCalculator ()).nextObject ();
63- String result = null ;
64-
65- if (message instanceof PGPCompressedData data ) {
66- message = new PGPObjectFactory (data .getDataStream (), new BcKeyFingerprintCalculator ()).nextObject ();
67- }
68-
69- if (message instanceof PGPLiteralData literalData ) {
70- final var outputStream = new ByteArrayOutputStream ();
71- Streams .pipeAll (literalData .getInputStream (), outputStream );
72- result = outputStream .toString (StandardCharsets .UTF_8 );
73- outputStream .close ();
74- } else {
75- throw new PGPException ("Message is not encoded correctly." );
76- }
77-
78- if (encryptedData .isIntegrityProtected () && !encryptedData .verify ()) {
79- throw new PGPException ("Message failed integrity check!" );
80- }
20+ final var bytes = msg .getBytes (StandardCharsets .UTF_8 );
21+ final var input = builder .build (new ByteArrayInputStream (bytes ));
8122
8223 input .close ();
83- keyStream .close ();
84- clearText .close ();
8524
86- return result ;
25+ return "test -> " + bytes . length ;
8726 }
8827}
0 commit comments