Example usage for org.bouncycastle.crypto CryptoException CryptoException

List of usage examples for org.bouncycastle.crypto CryptoException CryptoException

Introduction

In this page you can find the example usage for org.bouncycastle.crypto CryptoException CryptoException.

Prototype

public CryptoException(String message) 

Source Link

Document

create a CryptoException with the given message.

Usage

From source file:com.distrimind.util.crypto.P2PJPAKESecretMessageExchanger.java

License:Open Source License

@Override
protected void receiveData(int stepNumber, byte[] dataReceived) throws CryptoException {
    valid = false;/*from w  w w .  j a  v  a 2 s  .c om*/
    switch (stepNumber) {
    case 0:
        try (ByteArrayInputStream bais = new ByteArrayInputStream(dataReceived)) {
            try (DataInputStream ois = new DataInputStream(bais)) {
                JPAKERound1Payload r1;
                try {
                    short s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    byte[] tab = new byte[s];
                    ois.readFully(tab);
                    BigInteger gx1 = new BigInteger(tab);

                    s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    tab = new byte[s];
                    ois.readFully(tab);
                    BigInteger gx2 = new BigInteger(tab);

                    int size = ois.readInt();
                    BigInteger[] knowledgeProofForX1 = null;
                    if (size > 0) {
                        if (size > 100)
                            throw new CryptoException("illegal argument exception");
                        knowledgeProofForX1 = new BigInteger[size];
                        for (int i = 0; i < size; i++) {
                            s = ois.readShort();
                            if (s <= 0)
                                throw new IOException();
                            tab = new byte[s];
                            ois.readFully(tab);
                            knowledgeProofForX1[i] = new BigInteger(tab);
                        }
                    }
                    size = ois.readInt();
                    BigInteger[] knowledgeProofForX2 = null;
                    if (size > 0) {
                        if (size > 100)
                            throw new CryptoException("illegal argument exception");
                        knowledgeProofForX2 = new BigInteger[size];
                        for (int i = 0; i < size; i++) {
                            s = ois.readShort();
                            if (s <= 0)
                                throw new IOException();
                            tab = new byte[s];
                            ois.readFully(tab);
                            knowledgeProofForX2[i] = new BigInteger(tab);
                        }
                    }
                    s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    tab = new byte[s];
                    ois.readFully(tab);
                    String pid = new String(tab, Charset.forName("utf-8"));
                    if (knowledgeProofForX1 == null)
                        throw new IOException();
                    if (knowledgeProofForX2 == null)
                        throw new IOException();
                    r1 = new JPAKERound1Payload(pid, gx1, gx2, knowledgeProofForX1, knowledgeProofForX2);
                } catch (Exception e) {
                    valid = false;
                    throw new CryptoException("data received is not a valid instance of JPAKERound1Payload", e);
                }
                jpake.validateRound1PayloadReceived(r1);
            }
        } catch (Exception e) {
            valid = false;
            if (e instanceof CryptoException)
                throw (CryptoException) e;
            else
                throw new CryptoException("", e);
        }
        break;
    case 1:
        try (ByteArrayInputStream bais = new ByteArrayInputStream(dataReceived)) {
            try (DataInputStream ois = new DataInputStream(bais)) {
                JPAKERound2Payload r2;
                try {
                    short s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    byte[] tab = new byte[s];
                    ois.readFully(tab);
                    BigInteger A = new BigInteger(tab);

                    int size = ois.readInt();
                    BigInteger[] knowledgeProofForX2s = null;
                    if (size > 0) {
                        if (size > 100)
                            throw new CryptoException("illegal argument exception");
                        knowledgeProofForX2s = new BigInteger[size];
                        for (int i = 0; i < size; i++) {
                            s = ois.readShort();
                            if (s <= 0)
                                throw new IOException();
                            tab = new byte[s];
                            ois.readFully(tab);
                            knowledgeProofForX2s[i] = new BigInteger(tab);
                        }
                    }
                    s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    tab = new byte[s];
                    ois.readFully(tab);
                    String pid = new String(tab, Charset.forName("utf-8"));

                    if (knowledgeProofForX2s == null)
                        throw new IOException();

                    r2 = new JPAKERound2Payload(pid, A, knowledgeProofForX2s);
                } catch (Exception e) {
                    valid = false;
                    throw new CryptoException("data received is not a valid instance of JPAKERound2Payload", e);
                }
                jpake.validateRound2PayloadReceived(r2);
            }
        } catch (Exception e) {
            valid = false;
            if (e instanceof CryptoException)
                throw (CryptoException) e;
            else
                throw new CryptoException("", e);
        }
        break;
    case 2:
        try (ByteArrayInputStream bais = new ByteArrayInputStream(dataReceived)) {
            try (DataInputStream ois = new DataInputStream(bais)) {
                JPAKERound3Payload r3;
                try {
                    short s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    byte[] tab = new byte[s];
                    ois.readFully(tab);
                    BigInteger magTag = new BigInteger(tab);

                    s = ois.readShort();
                    if (s <= 0)
                        throw new IOException();
                    tab = new byte[s];
                    ois.readFully(tab);
                    String pid = new String(tab, Charset.forName("utf-8"));

                    r3 = new JPAKERound3Payload(pid, magTag);
                } catch (Exception e) {
                    valid = false;
                    throw new CryptoException("data received is not a valid instance of JPAKERound2Payload", e);
                }

                jpake.validateRound3PayloadReceived(r3, keyMaterial);
            }
        } catch (Exception e) {
            valid = false;
            if (e instanceof CryptoException)
                throw (CryptoException) e;
            else
                throw new CryptoException("", e);
        }
        break;
    default:
        throw new IllegalAccessError();

    }
    valid = true;
}

From source file:com.wlami.mibox.client.networking.encryption.AesChunkEncryption.java

License:Open Source License

@Override
public DataChunk decryptChunk(MChunk mChunk, byte[] encryptedChunkData) {
    log.debug("Starting decryption");
    try {//from   w w  w  .j a v  a2  s  . co m
        byte[] decryptedChunkData = AesEncryption.decrypt(encryptedChunkData, mChunk.getDecryptedChunkHash(),
                mChunk.getPosition());
        String decryptedHash = HashUtil.calculateSha256(decryptedChunkData);
        log.debug("Calculate decrypted hash: [{}]", decryptedHash);
        if (!decryptedHash.equals(mChunk.getDecryptedChunkHash())) {
            throw new CryptoException("Hash of decrypted data does not match");
        }
        return new DataChunk(false, decryptedHash, decryptedChunkData);
    } catch (CryptoException e) {
        throw new CryptoRuntimeException(e);
    }
}

From source file:org.candlepin.util.X509CRLStreamWriter.java

License:Open Source License

protected static Digest createDigest(AlgorithmIdentifier digAlg) throws CryptoException {
    Digest dig;//from ww  w  .  j a  v  a 2 s . c  o m

    if (digAlg.getAlgorithm().equals(OIWObjectIdentifiers.idSHA1)) {
        dig = new SHA1Digest();
    } else if (digAlg.getAlgorithm().equals(NISTObjectIdentifiers.id_sha224)) {
        dig = new SHA224Digest();
    } else if (digAlg.getAlgorithm().equals(NISTObjectIdentifiers.id_sha256)) {
        dig = new SHA256Digest();
    } else if (digAlg.getAlgorithm().equals(NISTObjectIdentifiers.id_sha384)) {
        dig = new SHA384Digest();
    } else if (digAlg.getAlgorithm().equals(NISTObjectIdentifiers.id_sha512)) {
        dig = new SHA384Digest();
    } else if (digAlg.getAlgorithm().equals(PKCSObjectIdentifiers.md5)) {
        dig = new MD5Digest();
    } else if (digAlg.getAlgorithm().equals(PKCSObjectIdentifiers.md4)) {
        dig = new MD4Digest();
    } else {
        throw new CryptoException("Cannot recognize digest.");
    }

    return dig;
}