Search in sources :

Example 51 with InvalidKeyException

use of java.security.InvalidKeyException in project JamsMusicPlayer by psaravan.

the class Security method verify.

/**
     * Verifies that the signature from the server matches the computed
     * signature on the data.  Returns true if the data is correctly signed.
     *
     * @param publicKey public key associated with the developer account
     * @param signedData signed data from server
     * @param signature server signature
     * @return true if the data and signature match
     */
public static boolean verify(PublicKey publicKey, String signedData, String signature) {
    Signature sig;
    try {
        sig = Signature.getInstance(SIGNATURE_ALGORITHM);
        sig.initVerify(publicKey);
        sig.update(signedData.getBytes());
        if (!sig.verify(Base64.decode(signature))) {
            Log.e(TAG, "Signature verification failed.");
            return false;
        }
        return true;
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "NoSuchAlgorithmException.");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "Invalid key specification.");
    } catch (SignatureException e) {
        Log.e(TAG, "Signature exception.");
    } catch (Base64DecoderException e) {
        Log.e(TAG, "Base64 decoding failed.");
    }
    return false;
}
Also used : Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 52 with InvalidKeyException

use of java.security.InvalidKeyException in project ratpack by ratpack.

the class DefaultCrypto method decrypt.

@Override
public ByteBuf decrypt(ByteBuf message, ByteBufAllocator allocator) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, ShortBufferException, IllegalBlockSizeException {
    Cipher cipher = Cipher.getInstance(algorithm);
    if (isInitializationVectorRequired) {
        int ivByteLength = message.readByte();
        byte[] iv = new byte[ivByteLength];
        message.readBytes(iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));
    } else {
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    }
    int messageLength = message.readableBytes();
    ByteBuf decMessage = allocator.buffer(cipher.getOutputSize(messageLength));
    ByteBuf byteBuf = message.readBytes(messageLength);
    try {
        int count = cipher.doFinal(byteBuf.nioBuffer(), decMessage.nioBuffer(0, messageLength));
        if (!hasPadding) {
            for (int i = count - 1; i >= 0; i--) {
                if (decMessage.getByte(i) == 0x00) {
                    count--;
                } else {
                    break;
                }
            }
        }
        decMessage.writerIndex(count);
        return decMessage;
    } catch (Exception e) {
        decMessage.release();
        throw e;
    } finally {
        byteBuf.release();
    }
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec) ByteBuf(io.netty.buffer.ByteBuf) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException)

Example 53 with InvalidKeyException

use of java.security.InvalidKeyException in project robovm by robovm.

the class BaseMac method engineInit.

protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key == null) {
        throw new InvalidKeyException("key is null");
    }
    if (key instanceof BCPBEKey) {
        BCPBEKey k = (BCPBEKey) key;
        if (k.getParam() != null) {
            param = k.getParam();
        } else if (params instanceof PBEParameterSpec) {
            param = PBE.Util.makePBEMacParameters(k, params);
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    macEngine.init(param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidKeyException(java.security.InvalidKeyException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 54 with InvalidKeyException

use of java.security.InvalidKeyException in project robovm by robovm.

the class BaseStreamCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AlgorithmParameterSpec paramSpec = null;
    if (params != null) {
        for (int i = 0; i != availableSpecs.length; i++) {
            try {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            } catch (Exception e) {
                continue;
            }
        }
        if (paramSpec == null) {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }
    engineInit(opmode, key, paramSpec, random);
    engineParams = params;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DataLengthException(org.bouncycastle.crypto.DataLengthException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) InvalidKeyException(java.security.InvalidKeyException) ShortBufferException(javax.crypto.ShortBufferException)

Example 55 with InvalidKeyException

use of java.security.InvalidKeyException in project robovm by robovm.

the class BaseWrapCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AlgorithmParameterSpec paramSpec = null;
    if (params != null) {
        for (int i = 0; i != availableSpecs.length; i++) {
            try {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            } catch (Exception e) {
            // try next spec
            }
        }
        if (paramSpec == null) {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }
    engineParams = params;
    engineInit(opmode, key, paramSpec, random);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)499 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)263 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)124 SignatureException (java.security.SignatureException)95 IOException (java.io.IOException)94 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)93 BadPaddingException (javax.crypto.BadPaddingException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)87 Cipher (javax.crypto.Cipher)77 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)63 SecretKeySpec (javax.crypto.spec.SecretKeySpec)63 Signature (java.security.Signature)58 SecretKey (javax.crypto.SecretKey)50 PublicKey (java.security.PublicKey)49 PrivateKey (java.security.PrivateKey)47 CertificateException (java.security.cert.CertificateException)46 Mac (javax.crypto.Mac)44 IvParameterSpec (javax.crypto.spec.IvParameterSpec)41 NoSuchProviderException (java.security.NoSuchProviderException)39 KeyStoreException (java.security.KeyStoreException)33