Search in sources :

Example 21 with InvalidKeyException

use of java.security.InvalidKeyException in project qksms by moezbhatti.

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 22 with InvalidKeyException

use of java.security.InvalidKeyException in project ninja by ninjaframework.

the class CookieEncryption method encrypt.

/**
     * Encrypts data with secret key.
     *
     * @param data text to encrypt
     * @return encrypted text in base64 format
     */
public String encrypt(String data) {
    Objects.requireNonNull(data, "Data to be encrypted");
    if (!secretKeySpec.isPresent()) {
        return data;
    }
    try {
        // encrypt data
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec.get());
        byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        // convert encrypted bytes to string in base64
        return Base64.encodeBase64URLSafeString(encrypted);
    } catch (InvalidKeyException ex) {
        logger.error(getHelperLogMessage(), ex);
        throw new RuntimeException(ex);
    } catch (GeneralSecurityException ex) {
        logger.error("Failed to encrypt data.", ex);
        return "";
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) InvalidKeyException(java.security.InvalidKeyException)

Example 23 with InvalidKeyException

use of java.security.InvalidKeyException in project ninja by ninjaframework.

the class CookieEncryption method decrypt.

/**
     * Decrypts data with secret key.
     *
     * @param data text to decrypt in base64 format
     * @return decrypted text
     */
public String decrypt(String data) {
    Objects.requireNonNull(data, "Data to be decrypted");
    if (!secretKeySpec.isPresent()) {
        return data;
    }
    // convert base64 encoded string to bytes
    byte[] decoded = Base64.decodeBase64(data);
    try {
        // decrypt bytes
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec.get());
        byte[] decrypted = cipher.doFinal(decoded);
        // convert bytes to string
        return new String(decrypted, StandardCharsets.UTF_8);
    } catch (InvalidKeyException ex) {
        logger.error(getHelperLogMessage(), ex);
        throw new RuntimeException(ex);
    } catch (GeneralSecurityException ex) {
        logger.error("Failed to decrypt data.", ex);
        return "";
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) InvalidKeyException(java.security.InvalidKeyException)

Example 24 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 25 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)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)478 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)246 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)123 SignatureException (java.security.SignatureException)92 IOException (java.io.IOException)90 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)87 BadPaddingException (javax.crypto.BadPaddingException)83 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)80 Cipher (javax.crypto.Cipher)71 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)61 Signature (java.security.Signature)58 SecretKeySpec (javax.crypto.spec.SecretKeySpec)52 PublicKey (java.security.PublicKey)49 SecretKey (javax.crypto.SecretKey)49 PrivateKey (java.security.PrivateKey)47 CertificateException (java.security.cert.CertificateException)44 IvParameterSpec (javax.crypto.spec.IvParameterSpec)40 NoSuchProviderException (java.security.NoSuchProviderException)38 Mac (javax.crypto.Mac)38 SecureRandom (java.security.SecureRandom)32