Example usage for javax.crypto Cipher getInstance

List of usage examples for javax.crypto Cipher getInstance

Introduction

In this page you can find the example usage for javax.crypto Cipher getInstance.

Prototype

public static final Cipher getInstance(String transformation)
        throws NoSuchAlgorithmException, NoSuchPaddingException 

Source Link

Document

Returns a Cipher object that implements the specified transformation.

Usage

From source file:baggage.hypertoolkit.security.CipherText.java

public ClearText decrypt(SecretKey secretKey) throws MessageIntegrityException {
    try {/*from   ww  w .  j  ava2s  .  c o  m*/
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new ClearText(cipher.doFinal(bytes));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new MessageIntegrityException();
    } catch (BadPaddingException e) {
        throw new MessageIntegrityException();
    }
}

From source file:com.telefonica.euro_iaas.sdc.util.RSASignerImpl.java

/**
 * {@inheritDoc}//from  w ww  . j a  v a  2  s .c  om
 */

public String sign(String message, File pemFile) {
    try {
        Security.addProvider(new BouncyCastleProvider());
        PrivateKey privateKey = readKeyPair(pemFile, "".toCharArray()).getPrivate();

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);

        byte[] digest = cipher.doFinal(message.getBytes());
        return Base64.encodeBase64String(digest);
    } catch (IOException e) {
        throw new SdcRuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SdcRuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new SdcRuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new SdcRuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new SdcRuntimeException(e);
    } catch (BadPaddingException e) {
        throw new SdcRuntimeException(e);
    }
}

From source file:de.extra.client.plugins.outputplugin.crypto.ExtraCryptoUtil.java

/** Encrypts the specified string, using the specified secret key. */
private static String encrypt(String sValue, String secretKey) {
    if (secretKey == null) {
        secretKey = SYM_KEY_STR;/*from w w w. j  av a2 s . c o  m*/
    }

    if (sValue == null || sValue.equals("")) {
        return "";
    }

    String textEncode = null;
    Cipher encryptCipher = null;

    try {
        SecretKeySpec skeySpec = decodeKey(secretKey);
        encryptCipher = Cipher.getInstance(TRANSFORMATION);
        encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        byte[] plainText = sValue.trim().getBytes(CHARSET);

        // do the actual encryption
        byte[] cipherText = encryptCipher.doFinal(plainText);

        // Changed to encode() to avoid <cr> on end of string
        // textEncode = base64Encoder.encodeBuffer(cipherText);
        textEncode = new Base64().encodeAsString(cipherText);

    } catch (Exception e) {
        e.printStackTrace(System.err);
    }

    return textEncode;
}

From source file:de.thischwa.pmcms.tool.DESCryptor.java

public String decrypt(String encryptedTxt) throws CryptorException {
    if (encryptedTxt == null || encryptedTxt.trim().length() == 0)
        return null;
    if (key == null)
        key = buildKey();//from  w  ww.ja  v a2s  .com
    try {
        byte[] encrypedPwdBytes = Base64.decodeBase64(encryptedTxt);
        Cipher cipher = Cipher.getInstance(algorithm); // cipher is not thread safe
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainTextPwdBytes = cipher.doFinal(encrypedPwdBytes);
        return new String(plainTextPwdBytes);
    } catch (Exception e) {
        throw new CryptorException(e);
    }
}

From source file:edu.harvard.i2b2.analysis.security.RijndaelAlgorithm.java

public RijndaelAlgorithm(String password, int ksize, String encryptionType, String emethod) throws Exception {

    SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), encryptionType);

    cipherEnc = Cipher.getInstance(emethod);
    cipherDec = Cipher.getInstance(emethod);

    keySize = ksize;//from ww w .j ava  2s.co  m

    //setKey( pword );
    cipherEnc.init(Cipher.ENCRYPT_MODE, skeySpec);
    cipherDec.init(Cipher.DECRYPT_MODE, skeySpec);

}

From source file:com.cellngine.crypto.RSACipher.java

public RSACipher() {
    try {//from  w  w  w . j a v  a  2s. co m
        this.cipher = Cipher.getInstance(TRANSFORMATION);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
        LOG.error("Unable to get cipher instance (" + TRANSFORMATION + ")", e);
    }
}

From source file:com.almende.util.EncryptionUtil.java

/**
 * Encrypt a string./*from  w  ww  . j  av  a  2s  .com*/
 * 
 * @param text
 *            the text
 * @return encryptedText
 * @throws InvalidKeyException
 *             the invalid key exception
 * @throws InvalidAlgorithmParameterException
 *             the invalid algorithm parameter exception
 * @throws NoSuchAlgorithmException
 *             the no such algorithm exception
 * @throws InvalidKeySpecException
 *             the invalid key spec exception
 * @throws NoSuchPaddingException
 *             the no such padding exception
 * @throws IllegalBlockSizeException
 *             the illegal block size exception
 * @throws BadPaddingException
 *             the bad padding exception
 * @throws UnsupportedEncodingException
 *             the unsupported encoding exception
 */
public static String encrypt(final String text) throws InvalidKeyException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException,
        BadPaddingException, UnsupportedEncodingException {
    final PBEParameterSpec pbeParamSpec = new PBEParameterSpec(S, C);
    final PBEKeySpec pbeKeySpec = new PBEKeySpec(P);
    final SecretKeyFactory keyFac = SecretKeyFactory.getInstance(ENC);
    final SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

    final Cipher pbeCipher = Cipher.getInstance(ENC);
    pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

    final byte[] encryptedText = pbeCipher.doFinal(text.getBytes("UTF-8"));
    return Base64.encodeBase64String(encryptedText);
}

From source file:com.gvmax.common.util.Enc.java

public Enc(String password, int keyLength) {
    if (password == null || password.trim().equals("")) {
        enabled = false;//ww w.j  av  a  2  s  .  c o  m
    } else {
        enabled = true;
        try {
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
            KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 1024, keyLength);
            key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
            c = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (Exception e) {
            logger.error(e);
        }
    }
}