Example usage for javax.crypto Cipher init

List of usage examples for javax.crypto Cipher init

Introduction

In this page you can find the example usage for javax.crypto Cipher init.

Prototype

public final void init(int opmode, Certificate certificate, SecureRandom random) throws InvalidKeyException 

Source Link

Document

Initializes this cipher with the public key from the given certificate and a source of randomness.

Usage

From source file:Main.java

private static Cipher getCipherFromPassphrase(String passphrase, byte[] salt, int iterations, int opMode)
        throws GeneralSecurityException {
    SecretKey key = getKeyFromPassphrase(passphrase, salt, iterations);
    Cipher cipher = Cipher.getInstance(key.getAlgorithm());
    cipher.init(opMode, key, new PBEParameterSpec(salt, iterations));

    return cipher;
}

From source file:com.bytecode.util.Crypto.java

private static byte[] encrypt(String keystring, String message, int bits) throws Exception {
    byte[] encValue = null;
    SecureRandom random = new SecureRandom();
    byte[] nonceBytes = new byte[8];
    random.nextBytes(nonceBytes);//from w ww . j av  a2 s .co  m
    IvParameterSpec nonce = new IvParameterSpec(Arrays.copyOf(nonceBytes, 16));

    Key key = generateKey(keystring, bits);
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key, nonce);
    byte[] ciphertextWithoutNonce = c.doFinal(message.getBytes("UTF-8"));
    encValue = Arrays.copyOf(nonceBytes, nonceBytes.length + ciphertextWithoutNonce.length);
    for (int i = 0; i < ciphertextWithoutNonce.length; i++) {
        encValue[i + 8] = ciphertextWithoutNonce[i];
    }

    return encValue;
}

From source file:Main.java

public static String decode(String key, String ciphertext) throws Exception {
    byte[] bs = parseHexStr2Byte(ciphertext);
    IvParameterSpec ivSpec = new IvParameterSpec(HEX.getBytes());
    SecretKeySpec secretKeySpec = createKey(key);
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
    return new String(c.doFinal(bs), "UTF-8");
}

From source file:Main.java

public static String encode(String key, String cleartext) throws Exception {
    SecretKeySpec secretKeySpec = createKey(key);
    IvParameterSpec ivSpec = new IvParameterSpec(HEX.getBytes());
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
    String result = parseByte2HexStr(c.doFinal(cleartext.getBytes("UTF-8")));

    return result;
}

From source file:Main.java

public static String decryp(String key, String str) throws java.io.UnsupportedEncodingException,
        NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String aesKey;/*from  ww  w. j ava  2s.  co  m*/
    aesKey = key.substring(0, 16);
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, AESkey(key), new IvParameterSpec(aesKey.getBytes("UTF-8")));

    byte[] byteStr = Base64.decode(str.getBytes(), 0);
    String deStr = new String(c.doFinal(byteStr), "UTF-8");

    return deStr;
}

From source file:Main.java

public static String encrypt(String key, String str) throws java.io.UnsupportedEncodingException,
        NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String aesKey;//from w  w  w.  ja  va  2  s.  c  o  m
    aesKey = key.substring(0, 16);
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, AESkey(key), new IvParameterSpec(aesKey.getBytes()));

    byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
    String enStr = new String(Base64.encodeToString(encrypted, 0));

    return enStr;
}

From source file:de.uzk.hki.da.passwordEncryptor.passwordEncryptor.java

private static String encryptPasswordForContentBroker(String password) {

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

    try {/*ww  w  . java  2  s .  co 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:ch.helmchen.camlapse.user.control.Encryption.java

public static String decrypt(String aBase64String) throws GeneralSecurityException, IOException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return new String(pbeCipher.doFinal(base64Decode(aBase64String)));
}

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

/**
 * Verschlsselt den bergebenen String.//from   w ww . j a v  a 2  s  . com
 * 
 * @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:de.uzk.hki.da.utils.PasswordUtils.java

public static String decryptPassword(String password) {

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

    try {//w  w  w . j  a  v  a 2 s.  c  om
        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);
}