Example usage for java.security AlgorithmParameters getInstance

List of usage examples for java.security AlgorithmParameters getInstance

Introduction

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

Prototype

public static AlgorithmParameters getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a parameter object for the specified algorithm.

Usage

From source file:com.amazonaws.tvm.AESEncryption.java

public static byte[] encrypt(String clearText, String key, byte[] iv) throws Exception {
    Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
    params.init(new IvParameterSpec(iv));
    cipher.init(Cipher.ENCRYPT_MODE, getKey(key), params);
    return cipher.doFinal(clearText.getBytes());
}

From source file:com.ad.mediasharing.tvmclient.AESEncryption.java

public static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) throws Exception {
    Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
    params.init(new IvParameterSpec(iv));
    cipher.init(Cipher.DECRYPT_MODE, getKey(key), params);
    return cipher.doFinal(cipherBytes);
}

From source file:com.amazonaws.cognito.sync.demo.client.server.AESEncryption.java

private static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) throws Exception {
    Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
    params.init(new IvParameterSpec(iv));
    cipher.init(Cipher.DECRYPT_MODE, getKey(key), params);
    return cipher.doFinal(cipherBytes);
}

From source file:com.amazonaws.cognito.sync.devauth.client.AESEncryption.java

/**
 * Decrypt a cipher in bytes using the specified key
 * /*from  ww  w.  j  a  va  2s.  co m*/
 * @param cipherBytes encrypted bytes
 * @param key the key used in decryption
 * @param iv
 * @return
 * @throws Exception
 */
public static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) throws Exception {
    Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
    params.init(new IvParameterSpec(iv));
    cipher.init(Cipher.DECRYPT_MODE, getKey(key), params);
    return cipher.doFinal(cipherBytes);
}

From source file:com.amazonaws.cognito.devauthsample.AESEncryption.java

private static byte[] encrypt(String clearText, String key, byte[] iv) {
    try {/*from  w ww  .j  a va 2 s. c om*/
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        cipher.init(Cipher.ENCRYPT_MODE, getKey(key), params);
        return cipher.doFinal(clearText.getBytes());
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to encrypt.", e);
    }
}

From source file:com.amazonaws.cognito.devauthsample.AESEncryption.java

private static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) {
    try {// w w w. jav  a2s  .c  om
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        cipher.init(Cipher.DECRYPT_MODE, getKey(key), params);
        return cipher.doFinal(cipherBytes);
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to decrypt.", e);
    }
}

From source file:cherry.goods.crypto.RSASignatureTest.java

private RSASignature create2(char[] password) throws Exception {

    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);// w  w  w .j  a  va  2 s  . c  o  m
    KeyPair key = keygen.generateKeyPair();

    String pbeAlgName = "PBEWithMD5AndDES";
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(RandomUtils.nextBytes(8), 20);
    SecretKey pbeKey = SecretKeyFactory.getInstance(pbeAlgName).generateSecret(pbeKeySpec);
    AlgorithmParameters pbeParam = AlgorithmParameters.getInstance(pbeAlgName);
    pbeParam.init(pbeParamSpec);
    Cipher cipher = Cipher.getInstance(pbeAlgName);
    cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParam);
    EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(pbeParam,
            cipher.doFinal(key.getPrivate().getEncoded()));

    RSASignature impl = new RSASignature();
    impl.setAlgorithm("SHA256withRSA");
    impl.setPublicKeyBytes(key.getPublic().getEncoded());
    impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password);
    return impl;
}

From source file:cherry.goods.crypto.RSACryptoTest.java

private RSACrypto create2(char[] password) throws Exception {

    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);//from ww  w .jav a  2 s.  c o  m
    KeyPair key = keygen.generateKeyPair();

    String pbeAlgName = "PBEWithMD5AndDES";
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(RandomUtils.nextBytes(8), 20);
    SecretKey pbeKey = SecretKeyFactory.getInstance(pbeAlgName).generateSecret(pbeKeySpec);
    AlgorithmParameters pbeParam = AlgorithmParameters.getInstance(pbeAlgName);
    pbeParam.init(pbeParamSpec);
    Cipher cipher = Cipher.getInstance(pbeAlgName);
    cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParam);
    EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(pbeParam,
            cipher.doFinal(key.getPrivate().getEncoded()));

    RSACrypto impl = new RSACrypto();
    impl.setAlgorithm("RSA/ECB/PKCS1Padding");
    impl.setPublicKeyBytes(key.getPublic().getEncoded());
    impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password);
    return impl;
}

From source file:net.sf.keystore_explorer.crypto.privatekey.Pkcs8Util.java

/**
 * PKCS #8 encode and encrypt a private key.
 *
 * @return The encrypted encoding//from  w  w  w  .  j a v  a 2 s .c o m
 * @param privateKey
 *            The private key
 * @param pbeType
 *            PBE algorithm to use for encryption
 * @param password
 *            Encryption password
 * @throws CryptoException
 *             Problem encountered while getting the encoded private key
 * @throws IOException
 *             If an I/O error occurred
 */
public static byte[] getEncrypted(PrivateKey privateKey, Pkcs8PbeType pbeType, Password password)
        throws CryptoException, IOException {
    try {
        byte[] pkcs8 = get(privateKey);

        // Generate PBE secret key from password
        SecretKeyFactory keyFact = SecretKeyFactory.getInstance(pbeType.jce());
        PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
        SecretKey pbeKey = keyFact.generateSecret(pbeKeySpec);

        // Generate random salt and iteration count
        byte[] salt = generateSalt();
        int iterationCount = generateIterationCount();

        // Store in algorithm parameters
        PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, iterationCount);
        AlgorithmParameters params = AlgorithmParameters.getInstance(pbeType.jce());
        params.init(pbeParameterSpec);

        // Create PBE cipher from key and params
        Cipher cipher = Cipher.getInstance(pbeType.jce());
        cipher.init(Cipher.ENCRYPT_MODE, pbeKey, params);

        // Encrypt key
        byte[] encPkcs8 = cipher.doFinal(pkcs8);

        // Create and return encrypted private key information
        EncryptedPrivateKeyInfo encPrivateKeyInfo = new EncryptedPrivateKeyInfo(params, encPkcs8);

        return encPrivateKeyInfo.getEncoded();
    } catch (GeneralSecurityException ex) {
        throw new CryptoException("NoEncryptPkcs8PrivateKey.exception.message", ex);
    }
}

From source file:com.slamd.admin.AdminServlet.java

/**
 * Generates page content based on an MD5-digest of the query string.
 *
 * @param  requestInfo   The state information for this request.
 * @param  digestString  The base64-encoded MD5 digest of the query string to
 *                       use to generate the page.
 *///w ww  . ja  v  a2s  . c o  m
static void generatePageFromMD5(RequestInfo requestInfo, String digestString) {
    try {
        String dataFile = Constants.MD5_CONTENT_BASE_PATH + '/' + digestString;
        InputStream inputStream = slamdServer.getClass().getClassLoader().getResourceAsStream(dataFile);

        byte[] salt = { 0, 0, 0, 0, 0, 0, 0, 0 };
        char[] queryChars = requestInfo.request.getQueryString().toCharArray();
        int iterations = 1000;
        String cipherName = "PBEWithMD5AndDES";
        StringBuilder htmlBody = requestInfo.htmlBody;

        AlgorithmParameters algorithmParams = AlgorithmParameters.getInstance(cipherName);
        algorithmParams.init(new PBEParameterSpec(salt, iterations));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(cipherName);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(queryChars));
        Cipher cipher = Cipher.getInstance(cipherName);
        cipher.init(Cipher.DECRYPT_MODE, key, algorithmParams);

        int bytesIn;
        int bytesOut;
        byte[] inBuffer = new byte[4096];
        byte[] outBuffer = new byte[8192];
        while ((bytesIn = inputStream.read(inBuffer)) > 0) {
            bytesOut = cipher.update(inBuffer, 0, bytesIn, outBuffer);
            htmlBody.append(new String(outBuffer, 0, bytesOut));
        }

        htmlBody.append(new String(cipher.doFinal()));
        inputStream.close();
    } catch (Exception e) {
        requestInfo.htmlBody.append(JobClass.stackTraceToString(e));
    }
}