简体   繁体   中英

Bouncycastle PGP encryption error Illegal Key Size

I'm currently writing an encrypted messaging service in java, and I'm using the bouncycastle PGP library. I have written a test program that generates a key pair, and encrypts/decrypts a message. This was working for a while, but it recently stopped in the decrypt stage, giving me an InvalidKeyException.

I've done some research and downloaded the JCE .jar files and imported them into my project (through Eclipse project -> properties -> add external JARs). I saw that for windows users, they should be put into a specific folder in the java library, but i couldn't find a similar one on my Mac. I tried looking through the usr/library folder but couldn't find anything of use.

Has anyone solved this issue on Mac?

EDIT: here's some code from my main test function

// decrypt
byte[] decrypted = PGPEncryptDecrypt.decrypt(encFromFile, secKey, pass.toCharArray());

Here's my decrypt method( this was not written by me, but I made a PGPEncryptDecrypt class to hold related static methods, and it worked for me)

public static byte[] decrypt(byte[] encrypted, InputStream keyIn, char[] password)
        throws IOException, PGPException, NoSuchProviderException {
    InputStream in = new ByteArrayInputStream(encrypted);

    in = PGPUtil.getDecoderStream(in);

    PGPObjectFactory pgpF = new PGPObjectFactory(in);
    PGPEncryptedDataList enc = null;
    Object o = pgpF.nextObject();

    //
    // the first object might be a PGP marker packet.
    //
    if (o instanceof PGPEncryptedDataList) {
        enc = (PGPEncryptedDataList) o;
    } else {
        enc = (PGPEncryptedDataList) pgpF.nextObject();
    }



    //
    // find the secret key
    //
    Iterator it = enc.getEncryptedDataObjects();
    PGPPrivateKey sKey = null;
    PGPPublicKeyEncryptedData pbe = null;
    PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
            PGPUtil.getDecoderStream(keyIn));

    while (sKey == null && it.hasNext()) {
        pbe = (PGPPublicKeyEncryptedData) it.next();

        sKey = findSecretKey(pgpSec, pbe.getKeyID(), password);
    }

    if (sKey == null) {
        throw new IllegalArgumentException(
                "secret key for message not found.");
    }

    InputStream clear = pbe.getDataStream(sKey, "BC");



    PGPObjectFactory pgpFact = new PGPObjectFactory(clear);

    PGPCompressedData cData = (PGPCompressedData) pgpFact.nextObject();

    pgpFact = new PGPObjectFactory(cData.getDataStream());

    PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject();

    InputStream unc = ld.getInputStream();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int ch;

    while ((ch = unc.read()) >= 0) {
        out.write(ch);

    }

    byte[] returnBytes = out.toByteArray();
    out.close();
    return returnBytes;
}

The error points to the findSecretKey (in PGPEncryptDecrypt class) method, which is as follows

public static PGPPrivateKey findSecretKey(
        PGPSecretKeyRingCollection pgpSec, long keyID, char[] pass)
        throws PGPException, NoSuchProviderException {
    PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID);

    if (pgpSecKey == null) {
        return null;
    }

    return pgpSecKey.extractPrivateKey(pass, "BC");
}

These functions all worked perfectly when i first implemented them, but they stopped working.

For anyone else looking, i found the answer to this after digging around a little.

what i did was open terminal, enter root library (as sudo), found the appropriate java library, and did a manual copy from my downloads folder into the appropriate java security folder

path was Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/jre/lib/security

then in there i did two cp filename commands to copy the appropriate file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM