0

I am pretty new to Java. I have a flat file where I have stored my password encrypted using GPG utility from Unix. Now, I want to read the file and get the password into a string before connecting to a system. I have read the post Getting GPG Decryption To Work In Java (Bouncy Castle) but I cant use the program as it gives me error to add @ before PGPSecretKey and to remove tokens etc. Basically I dont know where to put the java methods into my Project.

Below is my code:

package jcpx.oracle.apps.gpg;

import org.bouncycastle.apache.*;
import org.bouncycastle.apache.bzip2.*;
import org.bouncycastle.bcpg.*;
import org.bouncycastle.bcpg.attr.*;
import org.bouncycastle.bcpg.sig.*;
import org.bouncycastle.openpgp.*;

private PGPSecretKey readSecretKey(InputStream in) throws IOException, PGPException {
in = PGPUtil.getDecoderStream(in);
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in,
        new BcKeyFingerprintCalculator());

PGPSecretKey key = null;
for (Iterator<PGPSecretKeyRing> it = pgpSec.getKeyRings(); key == null && it.hasNext();) {
    PGPSecretKeyRing kRing = it.next();

    for (Iterator<PGPSecretKey> it2 = kRing.getSecretKeys(); key == null
            && it2.hasNext();) {
        PGPSecretKey k = it2.next();
        if ((keyId == null) && k.isSigningKey()) {
            key = k;
        }
        if ((keyId != null)
                && (Long.valueOf(keyId, 16).longValue() == (k.getKeyID() & MASK))) {
            key = k;
        }
    }
}

if (key == null) {
    throw new IllegalArgumentException("Can't find encryption key"
            + (keyId != null ? " '" + keyId + "' " : " ") + "in key ring.");
}

return key;
}
Community
  • 1
  • 1
Prasanna
  • 23
  • 3
  • Please make sure to post the _exact_ error messages in your problem description. Often, providing a small, but complete (and runnable) example helps to understand the issue. – Jens Erat Oct 24 '16 at 07:34

1 Answers1

1

Debugging your code is difficult without adequate context (e.g. errors, input files).

In case your question is still open, see this answer here: https://stackoverflow.com/a/42652115/7550201

Community
  • 1
  • 1
Jens
  • 570
  • 3
  • 11