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:net.duckling.ddl.web.agent.util.AuthUtil.java

private static String decodeAuth(String auth) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    SecretKeySpec spec = new SecretKeySpec(getKey(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, spec);
    byte[] result = cipher.doFinal(Base64.decodeBase64(auth));
    return new String(result, "UTF-8");
}

From source file:com.jwm123.loggly.reporter.TripleDesCipher.java

public TripleDesCipher(String keyPath, AppDirectory appDir) throws NoSuchAlgorithmException,
        NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IOException {
    this.appDir = appDir;
    if (key == null) {
        this.keyPath = keyPath;
        getKey();/* w  w w  . j  av a 2s . c o  m*/
    }
    SecretKey keySpec = new SecretKeySpec(key, ALGORITHM);
    encrypter = Cipher.getInstance(TRIPLE_DES_TRANSFORMATION);
    encrypter.init(Cipher.ENCRYPT_MODE, keySpec);
    decrypter = Cipher.getInstance(TRIPLE_DES_TRANSFORMATION);
    decrypter.init(Cipher.DECRYPT_MODE, keySpec);
}

From source file:com.AES256Util.java

public String aesEncode(String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException,

        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,

        IllegalBlockSizeException, BadPaddingException {

    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");

    c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));

    byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));

    String enStr = new String(Base64.encodeBase64(encrypted));

    return enStr;

}

From source file:com.scorpio4.util.io.IOStreamCrypto.java

public CipherInputStream decrypt(InputStream in) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidAlgorithmParameterException, InvalidKeyException {
    final SecretKey key = new SecretKeySpec(bytePassword, cipherSpec);
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    final Cipher cipher = Cipher.getInstance(cipherTransformation);
    cipher.init(Cipher.DECRYPT_MODE, key, IV);
    return new CipherInputStream(new Base64InputStream(in), cipher);
}

From source file:com.marvelution.jira.plugins.hudson.encryption.StringEncrypter.java

/**
 * Constructor//from  ww  w.j  a  va2s .  c  om
 */
public StringEncrypter() {
    try {
        keySpec = new DESedeKeySpec(DEFAULT_ENCRYPTION_KEY.getBytes(UNICODE_FORMAT));
        keyFactory = SecretKeyFactory.getInstance(DESEDE_ENCRYPTION_SCHEME);
        cipher = Cipher.getInstance(DESEDE_ENCRYPTION_SCHEME);
    } catch (Exception e) {
        LOGGER.fatal("Failed to initilise String Encryption classes. Reason: " + e.getMessage(), e);
        throw new StringEncryptionException(
                "Failed to initilise String Encryption classes. Reason: " + e.getMessage(), e);
    }
}

From source file:de.openflorian.crypt.provider.BlowfishCipher.java

@Override
public String decrypt(String str) throws GeneralSecurityException {
    if (key == null || key.isEmpty())
        throw new IllegalStateException("The key is not set or is length=0.");

    if (str == null)
        return null;

    try {//from w w  w  . j  a va 2s .  com
        SecretKeySpec keySpec;
        keySpec = new SecretKeySpec(key.getBytes("UTF8"), "Blowfish");

        Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        return new String(cipher.doFinal(Base64.decodeBase64(str.getBytes("UTF8"))), "UTF8");
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new GeneralSecurityException(e.getMessage(), e);
    }
}

From source file:com.haulmont.cuba.web.test.PasswordEncryptionTest.java

protected String decryptPassword(String password) {
    SecretKeySpec key = new SecretKeySpec(PASSWORD_KEY.getBytes(), "DES");
    IvParameterSpec ivSpec = new IvParameterSpec(PASSWORD_KEY.getBytes());
    String result;//from w w  w. j  a  va 2  s . com
    try {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
        result = new String(cipher.doFinal(Hex.decodeHex(password.toCharArray())));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:com.liusoft.dlog4j.upgrade.StringUtils.java

/**
 * //from   w  w w .j ava2s  .c  om
 * @param src ??
 * @param key 8?
 * @return     ??
 * @throws Exception
 */
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    //      DES????
    SecureRandom sr = new SecureRandom();
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??
    // SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
    // ??
    // ??
    return cipher.doFinal(src);
}

From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java

public static String makeTicket(String username, String hash, long ticketValidity) {
    Cipher cipher;/*from  w  w  w  .  ja va  2s .  c om*/
    try {
        cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        Ticket ticket = new Ticket(username, hash, ticketValidity);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(ticket);
        MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        byte[] digest = md.digest(bos.toByteArray());
        bos.write(digest);
        byte[] encryptedBytes = cipher.doFinal(bos.toByteArray());
        return Base64.encodeBase64URLSafeString(encryptedBytes);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException
            | BadPaddingException | IllegalBlockSizeException e) {
        LOGGER.log(Level.SEVERE, "Error when making a ticket", e);
    }
    return "";
}

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

public static String decryptString(String encryptedText, String password)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String decrypted = null;// w  w w  . j  a v  a 2 s  .  c om
    byte[] key = password.getBytes();
    if (key.length != 16) {
        throw new IllegalArgumentException("Invalid key size.");
    }
    byte[] value = Base64.decodeBase64(encryptedText);

    // Decrypt with AES/CBC/PKCS5Padding
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
    byte[] original = cipher.doFinal(value);
    decrypted = new String(original);

    return decrypted;
}