Search in sources :

Example 1 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 2 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 3 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 4 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 5 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)993 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)535 IOException (java.io.IOException)237 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)210 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)201 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)198 BadPaddingException (javax.crypto.BadPaddingException)190 Cipher (javax.crypto.Cipher)188 SignatureException (java.security.SignatureException)170 SecretKeySpec (javax.crypto.spec.SecretKeySpec)166 URISyntaxException (java.net.URISyntaxException)117 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)108 Signature (java.security.Signature)105 Mac (javax.crypto.Mac)103 StorageException (com.microsoft.azure.storage.StorageException)98 SecretKey (javax.crypto.SecretKey)93 PublicKey (java.security.PublicKey)88 Test (org.junit.Test)87 IvParameterSpec (javax.crypto.spec.IvParameterSpec)83 PrivateKey (java.security.PrivateKey)77