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.creditcloud.common.security.impl.DESTextCipher.java

public DESTextCipher() {
    try {//from w  w w . j  av a  2  s  .  co  m
        encryptCipher = Cipher.getInstance("DES");
        decryptCipher = Cipher.getInstance("DES");
        keyFactory = SecretKeyFactory.getInstance("DES");
    } catch (GeneralSecurityException ex) {
        logger.error("I don't think this will happen...", ex);
    }
}

From source file:architecture.common.BlowFishTest.java

@Test
public void testBlowfish() throws Exception {
    String Key = "password";
    byte[] KeyData = Key.getBytes();
    SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
    Cipher cipher = Cipher.getInstance("Blowfish");
    cipher.init(Cipher.ENCRYPT_MODE, KS);

    // encrypt message
    String inputText = "MyTextToEncrypt";
    byte[] encrypted = cipher.doFinal(inputText.getBytes());
    String encryptedString = Hex.encodeHexString(encrypted);
    System.out.println(" : " + encryptedString);

    cipher.init(Cipher.DECRYPT_MODE, KS);
    byte[] decrypt = cipher.doFinal(Hex.decodeHex(encryptedString.toCharArray()));
    System.out.println("   : " + new String(decrypt));

}

From source file:com.example.license.RSAUtil.java

public static String encrypt(String data, PrivateKey pri_key) throws Exception {
    // Cipher??// www  . j a v  a2  s . c om
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // SecureRandom random = new SecureRandom();
    // ?Cipher?
    cipher.init(Cipher.ENCRYPT_MODE, pri_key);
    byte[] results = cipher.doFinal(data.getBytes());
    // http://tripledes.online-domain-tools.com/??
    for (int i = 0; i < results.length; i++) {
        System.out.print(results[i] + " ");
    }
    System.out.println();
    // ??Base64?
    return Base64.encodeBase64String(results);
}

From source file:com.wso2telco.cryptosystem.AESencrp.java

/**
 * Encrypt.//from ww  w  .  ja  v  a2  s.  co  m
 *
 * @param Data the data
 * @return the string
 * @throws Exception the exception
 */
public static String encrypt(String Data) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    //String encryptedValue = new BASE64Encoder().encode(encVal);
    String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

From source file:gsn.http.ac.Protector.java

public static String encrypt(String value) throws Exception {
    logger.debug("Encrypt key");
    Key key = generateKey();/*from w  w w.j av  a 2  s. com*/
    String salt = getSalt();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key);

    String valueToEnc = null;
    String eValue = value;
    for (int i = 0; i < ITERATIONS; i++) {
        valueToEnc = salt + eValue;
        byte[] encValue = c.doFinal(valueToEnc.getBytes());
        eValue = new sun.misc.BASE64Encoder().encode(encValue);
        //eValue = Base64.encodeBase64String(encValue);
    }
    return eValue;
}

From source file:io.apiman.common.util.AesEncrypter.java

/**
 * Encrypt./*from  w w  w .j a va  2s  .  c o  m*/
 * @param plainText the plain text
 * @param secretKey the secret key
 * @return the string
 */
public static String encrypt(String secretKey, String plainText) {
    if (plainText == null) {
        return null;
    }
    byte[] encrypted;
    Cipher cipher;
    try {
        SecretKeySpec skeySpec = keySpecFromSecretKey(secretKey);

        cipher = Cipher.getInstance("AES"); //$NON-NLS-1$
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }
    try {
        encrypted = cipher.doFinal(plainText.getBytes());
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e);
    }
    return "$CRYPT::" + new String(Base64.encodeBase64(encrypted)); //$NON-NLS-1$
}

From source file:Main.java

private static Cipher initCipher(int mode, byte[] key, byte[] iv, String cipherAlgotirhm) {
    try {/*  w w  w .  j a v  a2s . c  o  m*/
        Key k = toKey(key);
        Cipher cipher = Cipher.getInstance(cipherAlgotirhm);
        String CipherAlgotirhm = cipherAlgotirhm.toUpperCase();
        if (CipherAlgotirhm.contains("CFB") || CipherAlgotirhm.contains("CBC"))
            cipher.init(mode, k, new IvParameterSpec(iv));
        else
            cipher.init(mode, k);
        return cipher;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

From source file:it.latraccia.pkcs11.reader.util.AESUtil.java

public static String encryptString(String clearText, String password)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String encrypted;//w  ww.j  a  va 2  s.c om
    byte[] key = password.getBytes();
    // Check the length of the password
    if (key.length != 16) {
        throw new IllegalArgumentException("Invalid password length.");
    }
    byte[] value = clearText.getBytes();

    // Encrypt with AES/CBC/PKCS5Padding
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
    byte[] encryptedBytes = cipher.doFinal(value);

    // Encode the bytes in base64
    encrypted = Base64.encodeBase64String(encryptedBytes);
    return encrypted;
}

From source file:D_common.E_ncript.java

public static byte[] decrypt(byte[] data, String keyStr) {
    try {/*from   w  ww  . j  a  v  a 2  s . co  m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        final SecretKeySpec secretKey = new SecretKeySpec(makeAESKey(keyStr), "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.kylinolap.rest.security.PasswordPlaceholderConfigurer.java

public static String decrypt(String strToDecrypt) {
    try {//ww w  . j  ava2 s  .  c  o  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        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) {
    }
    return null;
}