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:de.uzk.hki.da.passwordEncryptor.passwordEncryptor.java

private static String encryptPasswordForContentBroker(String password) {

    byte key[] = "394z57f4".getBytes();
    byte encryptedPassword[];

    try {//w  w w .ja va  2s .  c  o m
        SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));

        Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
        encrypt.init(Cipher.ENCRYPT_MODE, secretKey,
                new IvParameterSpec(new byte[] { 0x01, 0x02, 0x04, 0x10, 0x01, 0x02, 0x04, 0x10 }));
        encryptedPassword = encrypt.doFinal(password.getBytes());
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Couldn't encrypt password " + password + e);
    }

    return new String(Base64.encodeBase64(encryptedPassword));
}

From source file:com.sds.acube.ndisc.xnapi.XNApiDesCipher.java

/**
 * /*from w w  w .j  a  v  a2 s. c  o m*/
 * 
 * @param data  ?
 * @return  ?
 */
public static String encrypt(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, getKey());
        byte e[] = cipher.doFinal(data.getBytes());
        return Base64.encodeBase64String(e);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void decrypt(String fileIn, String fileOut, byte key[])
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(fileIn);
    FileOutputStream fos = new FileOutputStream(fileOut);

    // Length is 32 bytes
    //byte key[] = "1010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100001111000011".getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-256");
    key = sha.digest(key);//from w  w w.  j  av  a 2 s .com
    SecretKeySpec sks = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);

    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }

    fos.flush();
    fos.close();
    cis.close();
}

From source file:de.uzk.hki.da.utils.PasswordUtils.java

public static String decryptPassword(String password) {

    byte key[] = "394z57f4".getBytes();
    byte decryptedPassword[];

    try {//  ww w  .  j  a  v  a2 s.c o m
        SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));
        Cipher decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
        decrypt.init(Cipher.DECRYPT_MODE, secretKey,
                new IvParameterSpec(new byte[] { 0x01, 0x02, 0x04, 0x10, 0x01, 0x02, 0x04, 0x10 }));
        decryptedPassword = decrypt.doFinal(Base64.decodeBase64(password.getBytes()));

    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Couldn't decrypt password " + password + e);
    }

    return new String(decryptedPassword);
}

From source file:com.tydic.dbp.utils.ThreeDesUtils.java

public static String encryptMode(String Src) {
    try {/*w ww  . j a  va2s  . c  om*/
        // ?
        SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
        // 
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.ENCRYPT_MODE, deskey);
        return Hex.encodeHexString(c1.doFinal(Src.getBytes()));
    } catch (java.security.NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (javax.crypto.NoSuchPaddingException e2) {
        e2.printStackTrace();
    } catch (java.lang.Exception e3) {
        e3.printStackTrace();
    }
    return null;
}

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._64bitlabs.util.Encryptors.java

/**
 * Encrypts string value with the specified AES algorithm
 * @param Data string to be encrypted//from  w ww  .  ja va2  s.  c o  m
 * @return encrypted string
 */
public static String encrypt(String Data) {
    try {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        Base64 encoder = new Base64();
        String encryptedValue = new String(encoder.encode(encVal)).trim();
        return encryptedValue;
    } catch (Exception e) {
        return null;
    }
}

From source file:de.hybris.platform.b2b.punchout.services.impl.SymmetricManager.java

public static String encrypt(final String unsecureText, final String key) throws PunchOutCipherException {
    String encrypted = null;//from ww w  .  j a  v a  2  s .  co  m
    try {
        final Key skeySpec = new SecretKeySpec(new Base64().decode(key), ALGORITHM);
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        final byte[] encryptedValue = cipher.doFinal(unsecureText.getBytes());
        encrypted = new Base64().encodeAsString(encryptedValue);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
        // should never happen
        LOG.error("System was unable instantiate Cipher.", e);
    } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
        final String msg = "Error occured during encryption." + e.getMessage();
        LOG.error(msg);
        throw new PunchOutCipherException(msg, e);
    }

    return encrypted;
}

From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java

/**
 * Encrypts a string./*ww  w  .  j a  v a  2s  . c  om*/
 *
 * @param c The string to encrypt.
 * @return The encrypted string in HEX.
 */
public static String encrypt(String c, String key) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encoded = cipher.doFinal(c.getBytes());
        return new String(Hex.encodeHex(encoded));

    } catch (Exception e) {
        logger.warn("Could not encrypt string", e);
        return null;
    }
}

From source file:com.esri.geoevent.datastore.Crypto.java

static public String doEncrypt(String stringToEncrypt) throws GeneralSecurityException {
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, encryptionKey);
    byte[] encVal = c.doFinal(stringToEncrypt.getBytes());
    String encodedEncryptedString = new String(Base64.encodeBase64(encVal));
    return encodedEncryptedString;
}