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, Provider provider)
        throws NoSuchAlgorithmException, NoSuchPaddingException 

Source Link

Document

Returns a Cipher object that implements the specified transformation.

Usage

From source file:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java

private static byte[] decryptData(byte[] bInput) {
    byte[] outBytes = null;
    try {/*w  w w.  j  av  a 2  s.c om*/
        Key key = new SecretKeySpec(secret_key, "DESede");

        Cipher cipher = Cipher.getInstance("DESede", "SunJCE");

        cipher.init(Cipher.DECRYPT_MODE, key, cipher.getParameters());

        outBytes = cipher.doFinal(bInput);

    } catch (Exception ex) {
        //log.error("PacketUtil",ex.getMessage(), ex);
        return outBytes;
    }

    return outBytes;
}

From source file:com.iterzp.momo.utils.RSAUtils.java

/**
 * //  ww  w  .ja va2  s .  c  om
 * 
 * @param publicKey
 *            
 * @param data
 *            ?
 * @return ??
 */
public static byte[] encrypt(PublicKey publicKey, byte[] data) {
    Assert.notNull(publicKey);
    Assert.notNull(data);
    try {
        Cipher cipher = Cipher.getInstance("RSA", PROVIDER);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.kixeye.chassis.transport.crypto.SymmetricKeyCryptoUtils.java

/**
 * Loads a cipher./*from   www.j  a v  a 2 s.  c  om*/
 * 
 * @param transformation
 * @param provider
 * @return
 * @throws GeneralSecurityException
 */
public static Cipher loadCipher(String transformation, String provider) throws GeneralSecurityException {
    if (StringUtils.isNotBlank(provider)) {
        return Cipher.getInstance(transformation, provider);
    } else {
        return Cipher.getInstance(transformation);
    }
}

From source file:com.jk.security.JKEncDec.java

/**
 * Encrypt./*from w w w.j  a  v a 2  s.  co m*/
 *
 * @param plainText
 *            the plain text
 * @return the string
 */
public static String encrypt(String plainText) {
    try {
        plainText = fixText(plainText);
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
        return toString(cipher.doFinal(plainText.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new JKSecurityException(e);
    }
}

From source file:com.sirius.utils.encrypt.DESEncryptor.java

@Override
public byte[] encrypt(Object key, byte[] data) throws EncryptException {
    try {//from w w  w.ja v  a 2  s  .c  o m
        Cipher cipher = Cipher.getInstance(ALGORITHM, security_provider);
        cipher.init(Cipher.ENCRYPT_MODE, getKey(key));
        return cipher.doFinal(data);
    } catch (Exception e) {
        throw new EncryptException(e);
    }
}

From source file:com.lingxiang2014.util.RSAUtils.java

public static byte[] decrypt(PrivateKey privateKey, byte[] data) {
    Assert.notNull(privateKey);/*from   w  w  w  . j a  v a  2  s  .c o m*/
    Assert.notNull(data);
    try {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", PROVIDER);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.forsrc.utils.MyRsa2Utils.java

/**
 * Decrypt string.//from   w ww.j  a va2s  .com
 *
 * @param privateKey the private key
 * @param cipherText the cipher text
 * @return the string
 * @throws RsaException the rsa exception
 */
public static String decrypt(PrivateKey privateKey, String cipherText) throws RsaException {
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance(RsaKey.ALGORITHM, new org.bouncycastle.jce.provider.BouncyCastleProvider());
    } catch (NoSuchAlgorithmException e) {
        throw new RsaException(e);
    } catch (NoSuchPaddingException e) {
        throw new RsaException(e);
    }
    try {
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
    } catch (InvalidKeyException e) {
        throw new RsaException(e);
    }
    byte[] input = null;
    try {
        input = new Base64().decode(cipherText);
    } catch (Exception e) {
        throw new RsaException(e);
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    try {
        int blockSize = cipher.getBlockSize();
        blockSize = blockSize == 0 ? 117 : blockSize;
        int i = 0;
        int start = 0;

        do {
            start = i++ * blockSize;
            baos.write(cipher.doFinal(input, start, blockSize));
        } while (input.length - start - blockSize > 0);

    } catch (IllegalBlockSizeException e) {
        throw new RsaException(e);
    } catch (BadPaddingException e) {
        throw new RsaException(e);
    } catch (IOException e) {
        throw new RsaException(e);
    }
    return new String(baos.toByteArray());
}

From source file:com.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java

public static String decrypt(String encryptedData, String password) throws Exception {

    Security.addProvider(new BouncyCastleProvider());

    byte[] encrypted = Base64.decode(encryptedData);

    Cipher cipher0 = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");

    // Openssl puts SALTED__ then the 8 byte salt at the start of the file.
    // We simply copy it out.
    byte[] salt = new byte[SALT_SIZE];
    System.arraycopy(encrypted, SALT_SIZE, salt, 0, SALT_SIZE);

    SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC");
    cipher0.init(Cipher.DECRYPT_MODE,
            fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE)));

    // Decrypt the rest of the byte array (after stripping off the salt)
    byte[] decValue = cipher0.doFinal(encrypted, SALT_STRIP_LENGTH, encrypted.length - SALT_STRIP_LENGTH);

    return new String(decValue);
}

From source file:de.serverfrog.pw.crypt.SerpentUtil.java

public static CipherInputStream getInputStream(InputStream os, Key key) throws IOException {
    try {// w  w  w  .j  a  v  a 2s  . c o m
        Cipher instance = Cipher.getInstance("Serpent", "BC");
        instance.init(Cipher.DECRYPT_MODE, key);
        return new CipherInputStream(os, instance);
    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
            | NoSuchPaddingException ex) {
        if (ex.getClass().equals(InvalidKeyException.class)) {
            throw new IOException("Password Wrong");
        }
        Logger.getLogger(SerpentUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.sshutils.utils.CryptHelper.java

public static String decrypt(String strToDecrypt) {
    try {/*w w  w.j av a 2  s  .c om*/

        //                for (Provider provider: Security.getProviders()) {
        //  log.info(provider.getName());
        //  for (String key: provider.stringPropertyNames())
        //   log.info("\t" + key + "\t" + provider.getProperty(key));
        //}

        Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE, "BC");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
        return decryptedString;
    } catch (Exception e) {
        log.error("Error while decrypting", e);

    }
    return null;
}