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:com.aast.encrypt.EncryptManager.java

public static String decryptAES(String key, String encrypted) {
    try {/*from w w w . j  a  v  a2s . com*/
        byte[] keyBytes = getKeyFromString(key);
        IvParameterSpec iv = new IvParameterSpec(keyBytes);
        SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * More flexible AES decrypt that doesn't encode
 *
 * @param key AES key typically 128, 192 or 256 bit
 * @param iv Initiation Vector/*from w w  w .j  av a  2 s .  co  m*/
 * @param decodedCipherText in bytes (assumed it's already been decoded)
 * @return Decrypted message cipher text (not encoded)
 * @throws GeneralSecurityException if something goes wrong during encryption
 */
public static byte[] decrypt(final SecretKeySpec key, final byte[] iv, final byte[] decodedCipherText)
        throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance(AES_MODE);
    //IvParameterSpec ivSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decryptedBytes = cipher.doFinal(decodedCipherText);

    log("decryptedBytes", decryptedBytes);

    return decryptedBytes;
}

From source file:com.hovans.netty.tcpsample.network.ChannelDecoder.java

public ChannelDecoder(Context context) {
    super();//www. j  a v a 2 s  .  c  om
    mContext = context;
    try {
        mCipher = Cipher.getInstance("aes/cbc/pkcs5padding");
    } catch (NoSuchAlgorithmException e) {
        // ? ?, ? 
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        // ? ?, ? 
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:com.javiermoreno.springboot.rest.CryptographyServiceImplBlowfish.java

public CryptographyServiceImplBlowfish()
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
    keyGenerator.init(128);/*from  ww w.j ava  2s .c o  m*/
    key = keyGenerator.generateKey();
    cipherEncrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);
    cipherDecrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherDecrypt.init(Cipher.DECRYPT_MODE, key);
}

From source file:com.projectsontracks.model.CaribooKey.java

public CaribooKey() throws NoSuchAlgorithmException, NoSuchPaddingException {
    // create AES shared key cipher
    this.aesCipher = Cipher.getInstance("AES");
    this.size = AES_DEFAULT_SIZE;
}

From source file:com.aerohive.nms.engine.admin.task.licensemgr.common.AerohiveEncryptTool.java

public AerohiveEncryptTool(String arg_Key) {
    if (arg_Key != null && !arg_Key.trim().equals(""))
        m_Str_Key = arg_Key;

    try {/*from   w  w  w . j a va2  s  . c  o  m*/
        // Create the key
        KeySpec keySpec = new PBEKeySpec(m_Str_Key.toCharArray(), m_Byte_Salt, m_Int_IterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        m_Cipher_Ecipher = Cipher.getInstance(key.getAlgorithm());
        m_Cipher_Dcipher = Cipher.getInstance(key.getAlgorithm());

        // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(m_Byte_Salt, m_Int_IterationCount);

        // Create the ciphers
        m_Cipher_Ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        m_Cipher_Dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    } catch (Exception e) {
        //DebugUtil.commonDebugWarn(e.getMessage());
    }
}

From source file:com.CardPaymentGateway.Decrypt.java

public String DecrypData(String MyDecrypData) {
    String DecryptedData = "";
    try {/* w w w  . j  a v  a2s.  c o  m*/
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(strPassword.getBytes());
        //Whatever you want to encrypt/decrypt using AES /CBC padding
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        //You can use ENCRYPT_MODE or DECRYPT_MODE
        cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

        //decode data using standard decoder
        byte[] output = new BASE64Decoder().decodeBuffer(MyDecrypData);

        // Decrypt the data
        byte[] decrypted = cipher.doFinal(output);

        //                  System.out.println("Original string: " +
        //                          new String(input));
        //                  
        // decryptedData .;
        //System.out.println("Decrypted string: " + new String(decrypted));
        DecryptedData = new String(decrypted);

    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchPaddingException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidAlgorithmParameterException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalBlockSizeException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BadPaddingException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex);
    }
    return DecryptedData;
}

From source file:com.formatAdmin.utils.UtilsSecurity.java

public static String decifrarMD5(String textoEncriptado) throws Exception {
    String base64EncryptedString = "";

    try {/*w w  w.ja  va  2  s  .  c  o m*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(SECRET_MD5_KEY.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, ENCRYPT_ALGORITHM);

        Cipher decipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException
            | IllegalBlockSizeException | NoSuchPaddingException ex) {
        throw ex;
    }
    return base64EncryptedString;
}

From source file:com.juicioenlinea.application.secutiry.Security.java

public static String desencriptar(String textoEncriptado) {

    final String secretKey = "hunter"; //llave para encriptar datos
    String encryptedString = null;

    try {//from  w  ww .j  a  v  a 2  s  .c  o m
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("UTF-8"));
        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("UTF-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        encryptedString = new String(plainText, "UTF-8");

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException
            | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }

    return encryptedString;
}

From source file:com.earldouglas.xjdl.io.LicenseCreator.java

public String encryptLicense(License license, String key)
        throws Exception, NoSuchAlgorithmException, NoSuchPaddingException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(license);
    objectOutputStream.close();/*from  w w  w.  j  a  v a 2 s  .com*/
    byte[] serializedLicense = byteArrayOutputStream.toByteArray();

    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    byte[] encryptedLicense = cipher.doFinal(serializedLicense);
    String encodedLicense = Base64.encodeBase64String(encryptedLicense);
    encodedLicense = encodedLicense.replaceAll("\n", "");
    encodedLicense = encodedLicense.replaceAll("\r", "");
    return encodedLicense;
}