Example usage for java.security GeneralSecurityException GeneralSecurityException

List of usage examples for java.security GeneralSecurityException GeneralSecurityException

Introduction

In this page you can find the example usage for java.security GeneralSecurityException GeneralSecurityException.

Prototype

public GeneralSecurityException(Throwable cause) 

Source Link

Document

Creates a GeneralSecurityException with the specified cause and a detail message of (cause==null ?

Usage

From source file:Main.java

/**
 * @throws GeneralSecurityException if the {@code sizeInBytes} is not a valid AES key size.
 *//*w  ww.  ja  va 2  s .c o m*/
public static void validateAesKeySize(int sizeInBytes) throws GeneralSecurityException {
    if (sizeInBytes != 16 && sizeInBytes != 24 && sizeInBytes != 32) {
        throw new GeneralSecurityException("invalid Aes key size");
    }
}

From source file:Main.java

/**
 * @throws GeneralSecurityException if {@code candidate} is negative
 * or larger than {@code maxExpected}./*w  w w  . j av  a2 s . c o m*/
 */
public static void validateVersion(int candidate, int maxExpected) throws GeneralSecurityException {
    if (candidate < 0 || candidate > maxExpected) {
        throw new GeneralSecurityException(
                String.format("key has version %d; only keys with version in range [0..%d] are supported",
                        candidate, maxExpected));
    }
}

From source file:Main.java

/**
 * Returns the concatenation of the input arrays in a single array. For example,
 * {@code concat(new byte[] {a, b}, new byte[] {}, new byte[] {c}} returns the array
 * {@code {a, b, c}}./*w w  w  .ja  va  2s.  c  o m*/
 *
 * @return a single array containing all the values from the source arrays, in order
 */
public static byte[] concat(byte[]... chunks) throws GeneralSecurityException {
    int length = 0;
    for (byte[] chunk : chunks) {
        if (length > Integer.MAX_VALUE - chunk.length) {
            throw new GeneralSecurityException("exceeded size limit");
        }
        length += chunk.length;
    }
    byte[] res = new byte[length];
    int pos = 0;
    for (byte[] chunk : chunks) {
        System.arraycopy(chunk, 0, res, pos, chunk.length);
        pos += chunk.length;
    }
    return res;
}

From source file:Main.java

/**
 * Encrypt and encode message using 256-bit AES with key generated from password.
 *
 * @param password used to generated key
 * @param message  the thing you want to encrypt assumed String UTF-8
 * @return Base64 encoded CipherText/*from   www . j a v a  2s.  c  o  m*/
 * @throws GeneralSecurityException if problems occur during encryption
 */
public static String encrypt(final String password, String message) throws GeneralSecurityException {
    try {
        final SecretKeySpec key = generateKey(password);

        log("message", message);

        byte[] cipherText = encrypt(key, ivBytes, message.getBytes(CHARSET));

        //NO_WRAP is important as was getting \n at the end
        String encoded = Base64.encodeToString(cipherText, Base64.NO_WRAP);
        log("Base64.NO_WRAP", encoded);
        return encoded;
    } catch (UnsupportedEncodingException e) {
        if (DEBUG_LOG_ENABLED)
            Log.e(TAG, "UnsupportedEncodingException ", e);
        throw new GeneralSecurityException(e);
    }
}

From source file:Main.java

/**
 * Encrypt and encode message using 256-bit AES with key generated from password.
 *
 * @param password used to generated key
 * @param message  the thing you want to encrypt assumed String UTF-8
 * @return Base64 encoded CipherText/*from w  ww. j av a  2  s.  c  om*/
 * @throws GeneralSecurityException if problems occur during encryption
 */
public static String encrypt(final String password, String message) throws GeneralSecurityException {

    try {
        final SecretKeySpec key = generateKey(password);

        log("message", message);

        byte[] cipherText = encrypt(key, ivBytes, message.getBytes(CHARSET));

        //NO_WRAP is important as was getting \n at the end
        String encoded = Base64.encodeToString(cipherText, Base64.NO_WRAP);
        log("Base64.NO_WRAP", encoded);
        return encoded;
    } catch (UnsupportedEncodingException e) {
        if (DEBUG_LOG_ENABLED)
            Log.e(TAG, "UnsupportedEncodingException ", e);
        throw new GeneralSecurityException(e);
    }
}

From source file:Main.java

/**
 * Decrypt and decode ciphertext using 256-bit AES with key generated from password
 *
 * @param password used to generated key
 * @param base64EncodedCipherText the encrpyted message encoded with base64
 * @return message in Plain text (String UTF-8)
 * @throws GeneralSecurityException if there's an issue decrypting
 *///from ww w . ja va 2  s. com
public static String decrypt(final String password, String base64EncodedCipherText)
        throws GeneralSecurityException {
    try {
        final SecretKeySpec key = generateKey(password);

        log("base64EncodedCipherText", base64EncodedCipherText);
        byte[] decodedCipherText = Base64.decode(base64EncodedCipherText, Base64.NO_WRAP);
        log("decodedCipherText", decodedCipherText);

        byte[] decryptedBytes = decrypt(key, ivBytes, decodedCipherText);

        log("decryptedBytes", decryptedBytes);
        String message = new String(decryptedBytes, CHARSET);
        log("message", message);

        return message;
    } catch (UnsupportedEncodingException e) {
        if (DEBUG_LOG_ENABLED)
            Log.e(TAG, "UnsupportedEncodingException ", e);

        throw new GeneralSecurityException(e);
    }
}

From source file:Main.java

/**
 * Decrypt and decode ciphertext using 256-bit AES with key generated from password
 *
 * @param password                used to generated key
 * @param base64EncodedCipherText the encrpyted message encoded with base64
 * @return message in Plain text (String UTF-8)
 * @throws GeneralSecurityException if there's an issue decrypting
 */// www .  j a  va  2 s  .  c  o  m
public static String decrypt(final String password, String base64EncodedCipherText)
        throws GeneralSecurityException {

    try {
        final SecretKeySpec key = generateKey(password);

        log("base64EncodedCipherText", base64EncodedCipherText);
        byte[] decodedCipherText = Base64.decode(base64EncodedCipherText, Base64.NO_WRAP);
        log("decodedCipherText", decodedCipherText);

        byte[] decryptedBytes = decrypt(key, ivBytes, decodedCipherText);

        log("decryptedBytes", decryptedBytes);
        String message = new String(decryptedBytes, CHARSET);
        log("message", message);

        return message;
    } catch (UnsupportedEncodingException e) {
        if (DEBUG_LOG_ENABLED)
            Log.e(TAG, "UnsupportedEncodingException ", e);

        throw new GeneralSecurityException(e);
    }
}

From source file:org.glite.slcs.pki.Certificate.java

/**
 * Read a PEM source to extract the certificate and its chain. The
 * certificate must be the first in the source, all others are considered as
 * chain members./*w w  w . j  ava  2s.  co  m*/
 * 
 * @param reader
 *            The Reader to read the source.
 * @return The Certificate (with its chain)
 * @throws IOException
 *             If an error occurs while reading the source.
 * @throws GeneralSecurityException
 *             If an error occurs while creating the Certificate.
 */
static public Certificate readPEM(Reader reader) throws IOException, GeneralSecurityException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("read cert and chain...");
    }
    X509Certificate certificates[] = Codec.readPEMEncodedCertificates(reader);

    // check array size
    int length = certificates.length;
    if (length < 1) {
        LOG.error("No X509 certificate found in source");
        throw new GeneralSecurityException("No valid X509 certificates found");
    }

    // The first is the main cert
    X509Certificate cert = certificates[0];
    // all others go in the chain
    X509Certificate chain[] = null;
    int chainLength = length - 1;
    if (chainLength > 0) {
        chain = new X509Certificate[chainLength];
        System.arraycopy(certificates, 1, chain, 0, chainLength);
    }
    return new Certificate(cert, chain);
}

From source file:org.nebula.service.auth.AuthenticationHelper.java

private void findUserCredentials() throws GeneralSecurityException {
    String accessId = authorization.getAccessId();

    userCredentials = userCredentialsPool.getUserCredentials(accessId);

    if (userCredentials == null) {
        throw new GeneralSecurityException("The accessId " + accessId + " not found");
    }/*from   www.  ja v a2s  .c  o  m*/
}

From source file:org.sipfoundry.sipxconfig.cert.CertificateUtils.java

public static X509Certificate generateCert(X509v3CertificateBuilder gen, String algorithm, PrivateKey key)
        throws GeneralSecurityException {
    ContentSigner sigGen;/*from   w w w  .ja  v  a 2 s  .  com*/
    try {
        sigGen = new JcaContentSignerBuilder(algorithm).setProvider(PROVIDER).build(key);
        JcaX509CertificateConverter converter = new JcaX509CertificateConverter().setProvider(PROVIDER);
        return converter.getCertificate(gen.build(sigGen));
    } catch (OperatorCreationException e) {
        throw new GeneralSecurityException(e);
    } catch (CertificateException e) {
        throw new GeneralSecurityException(e);
    }
}