Search in sources :

Example 81 with InvalidKeyException

use of java.security.InvalidKeyException in project Openfire by igniterealtime.

the class N method A.

public static byte[] A(byte[] abyte0, byte[] abyte1, int i, byte[] abyte2) {
    try {
        SecretKeySpec secretkeyspec = new SecretKeySpec(abyte2, "hmacSHA256");
        Mac mac = Mac.getInstance("hmacSHA256");
        mac.init(secretkeyspec);
        mac.update(abyte1, 0, i);
        byte[] abyte3 = mac.doFinal();
        secretkeyspec = new SecretKeySpec(abyte0, "hmacSHA256");
        mac = Mac.getInstance("hmacSHA256");
        mac.init(secretkeyspec);
        return mac.doFinal(abyte3);
    } catch (NoSuchAlgorithmException nosuchalgorithmexception) {
        E.error(nosuchalgorithmexception.getMessage(), nosuchalgorithmexception);
    } catch (InvalidKeyException invalidkeyexception) {
        E.error(invalidkeyexception.getMessage(), invalidkeyexception);
    }
    return null;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 82 with InvalidKeyException

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

the class Diagnostic method tryMacWithPassword.

private static void tryMacWithPassword(String algorithmName, String message, String password) {
    try {
        SecretKeySpec key = new SecretKeySpec(password.getBytes(), algorithmName);
        // a string like "HmacSHA256"
        Mac mac = Mac.getInstance(algorithmName, "BC");
        mac.init(key);
        byte[] digest = mac.doFinal(message.getBytes());
        System.out.println("Created " + algorithmName + " digest of length " + digest.length);
    } catch (NoSuchProviderException e) {
        System.err.println("Cannot use provider: BC: " + e.toString());
    } catch (NoSuchAlgorithmException e) {
        System.err.println("Cannot use algorithm: " + algorithmName + ": " + e.toString());
    } catch (InvalidKeyException e) {
        System.err.println("Cannot use key: " + password + ": " + e.toString());
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 83 with InvalidKeyException

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

the class Diagnostic method trySignature.

private static void trySignature() {
    String algorithmName = "SHA1withRSA";
    try {
        // generate keypair
        // NoSuchAlgorithmException, NoSuchProviderException
        KeyPair keyPair = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        String plaintext = "This is the message being signed";
        // generate signature
        // NoSuchAlgorithmException, NoSuchProviderException
        Signature instance = Signature.getInstance("SHA1withRSAEncryption", "BC");
        // InvalidKeyException
        instance.initSign(privateKey);
        // SignatureException
        instance.update((plaintext).getBytes());
        byte[] signature = instance.sign();
        System.out.println("Generated SHA1 with RSA signature of length: " + signature.length);
    } catch (NoSuchProviderException e) {
        System.err.println("Cannot use provider: BC: " + e.toString());
    } catch (NoSuchAlgorithmException e) {
        System.err.println("Cannot use algorithm: " + algorithmName + ": " + e.toString());
    } catch (InvalidKeyException e) {
        System.err.println("Cannot use key: " + e.toString());
    } catch (SignatureException e) {
        System.err.println("Cannot generate signature: " + e.toString());
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) JDKDigestSignature(org.bouncycastle.jce.provider.JDKDigestSignature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeyException(java.security.InvalidKeyException)

Example 84 with InvalidKeyException

use of java.security.InvalidKeyException in project kafka by apache.

the class ScramFormatter method generateCredential.

public ScramCredential generateCredential(String password, int iterations) {
    try {
        byte[] salt = secureRandomBytes();
        byte[] saltedPassword = saltedPassword(password, salt, iterations);
        byte[] clientKey = clientKey(saltedPassword);
        byte[] storedKey = storedKey(clientKey);
        byte[] serverKey = serverKey(saltedPassword);
        return new ScramCredential(salt, storedKey, serverKey, iterations);
    } catch (InvalidKeyException e) {
        throw new KafkaException("Could not create credential", e);
    }
}
Also used : KafkaException(org.apache.kafka.common.KafkaException) InvalidKeyException(java.security.InvalidKeyException)

Example 85 with InvalidKeyException

use of java.security.InvalidKeyException in project kafka by apache.

the class ScramSaslClient method handleServerFirstMessage.

private ClientFinalMessage handleServerFirstMessage(char[] password) throws SaslException {
    try {
        byte[] passwordBytes = formatter.normalize(new String(password));
        this.saltedPassword = formatter.hi(passwordBytes, serverFirstMessage.salt(), serverFirstMessage.iterations());
        ClientFinalMessage clientFinalMessage = new ClientFinalMessage("n,,".getBytes(StandardCharsets.UTF_8), serverFirstMessage.nonce());
        byte[] clientProof = formatter.clientProof(saltedPassword, clientFirstMessage, serverFirstMessage, clientFinalMessage);
        clientFinalMessage.proof(clientProof);
        return clientFinalMessage;
    } catch (InvalidKeyException e) {
        throw new SaslException("Client final message could not be created", e);
    }
}
Also used : ClientFinalMessage(org.apache.kafka.common.security.scram.ScramMessages.ClientFinalMessage) InvalidKeyException(java.security.InvalidKeyException) SaslException(javax.security.sasl.SaslException)

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