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:MainClass.java

private static byte[] passwordEncrypt(char[] password, byte[] plaintext) throws Exception {
    int MD5_ITERATIONS = 1000;
    byte[] salt = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);/* w  ww.  ja v a 2  s .  c o  m*/

    PBEKeySpec keySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
    SecretKey key = keyFactory.generateSecret(keySpec);
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, MD5_ITERATIONS);
    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(salt);
    baos.write(ciphertext);
    return baos.toByteArray();
}

From source file:illab.nabal.util.SecurityHelper.java

/**
  * AES-encrypt a plain message. Return null if message is empty.
  * //from  w ww  .j  a  va2s . c o m
  * @param message
  * @return AES encrypted hex
  * @throws Exception
  */
public static String cipher(String message) throws Exception {
    if (StringHelper.isEmpty(message) == false) {
        Cipher cipher = Cipher.getInstance(AES);
        cipher.init(Cipher.ENCRYPT_MODE, SYSTEM_SECRET_KEY_SPEC);
        return Base64.encodeToString(cipher.doFinal(message.getBytes()), Base64.DEFAULT);
    } else {
        return null;
    }
}

From source file:adminpassword.Decryption.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText, String idKey) throws Exception {
    String password = idKey;//  w  w w .j  av a 2  s  .  c om

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

    //strip off the salt and iv
    ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText));
    byte[] saltBytes = new byte[20];
    buffer.get(saltBytes, 0, saltBytes.length);
    byte[] ivBytes1 = new byte[cipher.getBlockSize()];
    buffer.get(ivBytes1, 0, ivBytes1.length);
    byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length];
    buffer.get(encryptedTextBytes);

    // Deriving the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1));

    byte[] decryptedTextBytes = null;
    try {
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);

    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

From source file:Main.java

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}

From source file:com.ro.ssc.app.client.licensing.TrialKeyValidator.java

public static String decodeKey(String encodedEncrypted) {
    String decoded = "";
    try {/*from w ww. j a v  a  2  s.  c o m*/
        byte[] saltDecrypt = SALT_DECRYPT.getBytes(StandardCharsets.UTF_8);
        SecretKeyFactory factoryKeyDecrypt = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY);
        SecretKey tmp2 = factoryKeyDecrypt.generateSecret(
                new PBEKeySpec(PASS_DECRYPT.toCharArray(), saltDecrypt, ITERATIONS_DECRYPT, KEY_LENGTH));
        SecretKeySpec decryptKey = new SecretKeySpec(tmp2.getEncoded(), ALGORITHM);
        Cipher aesCipherDecrypt = Cipher.getInstance(CIPHER);
        aesCipherDecrypt.init(Cipher.DECRYPT_MODE, decryptKey);
        byte[] e64bytes = StringUtils.getBytesUtf8(encodedEncrypted);
        byte[] eBytes = Base64.decodeBase64(e64bytes);
        byte[] cipherDecode = aesCipherDecrypt.doFinal(eBytes);
        decoded = StringUtils.newStringUtf8(cipherDecode);
    } catch (Exception e) {
        LOGGER.error("Error while decoding the trial key", e);
    }
    return decoded;
}

From source file:com.imaginary.home.cloud.Configuration.java

static public @Nonnull String decrypt(@Nonnull String keySalt, @Nonnull String value) {
    try {//from ww w  .ja v  a 2s.  c  o m
        SecretKeySpec spec = new SecretKeySpec(getConfiguration().getCustomSalt(keySalt), "AES");
        Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.DECRYPT_MODE, spec);

        byte[] b64 = value.getBytes("utf-8");
        byte[] raw = Base64.decodeBase64(b64);
        byte[] decrypted = cipher.doFinal(raw);
        return new String(decrypted, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:ch.helmchen.camlapse.user.control.Encryption.java

/**
 * Verschlsselt den bergebenen String./*  w w w.j a  v  a  2  s  . c  o  m*/
 * 
 * @param aString zu verschlsselnder String.
 */
public static String encrypt(final String aString) throws GeneralSecurityException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return base64Encode(pbeCipher.doFinal(aString.getBytes()));
}

From source file:com.liferay.sync.engine.util.Encryptor.java

public static String encrypt(String value) throws Exception {
    if (value == null) {
        return "";
    }//ww w.j av a  2 s .c  om

    SecretKey secretKey = new SecretKeySpec(_PASSWORD, _ALGORITHM);

    Cipher cipher = Cipher.getInstance(_ALGORITHM);

    cipher.init(Cipher.ENCRYPT_MODE, secretKey);

    String salt = getSalt();

    String encryptedValue = value;

    for (int i = 0; i < _ITERATIONS; i++) {
        encryptedValue = salt.concat(encryptedValue);

        byte[] encryptedBytes = cipher.doFinal(encryptedValue.getBytes(_UTF8_CHARSET));

        encryptedValue = Base64.encodeBase64String(encryptedBytes);
    }

    return encryptedValue;
}

From source file:love.sola.netsupport.util.RSAUtil.java

public static String encrypt(String value) {
    try {//from  w w w .j a v  a2  s.co m
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encrypted = cipher.doFinal(value.getBytes(StandardCharsets.UTF_8));
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.zf.decipher.DataEn.java

private static byte[] encrypt(byte[] data) throws Exception {
    if (null == secretKey) {
        throw new SecurityException("secretKey is null");
    }// w  w w . ja  v  a2  s  .c om
    Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}