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) throws InvalidKeyException 

Source Link

Document

Initializes this cipher with the public key from the given certificate.

Usage

From source file:it.geosolutions.figis.persistence.dao.util.PwEncoder.java

/**
 * Encoding the password on base64/*  w w  w .  j a  va 2  s .  c  om*/
 * @param msg
 * @return
 */
public static String encode(String msg) {
    try {
        SecretKeySpec keySpec = new SecretKeySpec(KEY, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] input = msg.getBytes();
        byte[] encrypted = cipher.doFinal(input);
        byte[] output = Base64.encodeBase64(encrypted);

        return new String(output);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (NoSuchPaddingException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (IllegalBlockSizeException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (BadPaddingException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (InvalidKeyException ex) {
        throw new RuntimeException("Error while encoding", ex);
    }
}

From source file:com.kixeye.chassis.transport.crypto.SymmetricKeyCryptoUtils.java

/**
 * Encrypts a data blob using the given key and cipher.
 * /*from  w  w w .  ja  v a2s. co  m*/
 * @param data
 * @param offset
 * @param length
 * @param key
 * @param cipher
 * @return
 * @throws GeneralSecurityException
 */
public static byte[] encrypt(byte[] data, int offset, int length, Key key, String cipherTransformation,
        String cipherProvider) throws GeneralSecurityException {
    Cipher cipher = loadCipher(cipherTransformation, cipherProvider);
    cipher.init(Cipher.ENCRYPT_MODE, key);

    return cipher.doFinal(data, offset, length);
}

From source file:com.kixeye.chassis.transport.crypto.SymmetricKeyCryptoUtils.java

/**
 * Decrypts a data blob using the given key and cipher.
 * //from   w  w w . j a v  a2s.co m
 * @param data
 * @param offset
 * @param length
 * @param key
 * @param cipher
 * @return
 * @throws GeneralSecurityException
 */
public static byte[] decrypt(byte[] data, int offset, int length, Key key, String cipherTransformation,
        String cipherProvider) throws GeneralSecurityException {
    Cipher cipher = loadCipher(cipherTransformation, cipherProvider);
    cipher.init(Cipher.DECRYPT_MODE, key);

    return cipher.doFinal(data, offset, length);
}

From source file:com.drisoftie.cwdroid.util.CredentialUtils.java

private static byte[] encrypt(byte[] key, byte[] clear) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    SecretKeySpec skeySpec = new SecretKeySpec(key, AES);
    Cipher cipher = getDefaultCipher();
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    return cipher.doFinal(clear);
}

From source file:com.drisoftie.cwdroid.util.CredentialUtils.java

private static byte[] decrypt(byte[] key, byte[] encrypted) throws NoSuchPaddingException,
        NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    SecretKeySpec skeySpec = new SecretKeySpec(key, AES);
    Cipher cipher = getDefaultCipher();
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    return cipher.doFinal(encrypted);
}

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

public static String decrypt(String strToDecrypt) {
    try {//from   w w w .  ja v a2  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;
}

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

public static String encrypt(String strToEncrypt) {
    try {/* w  ww .j  a va  2 s  .com*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
        return encryptedString;
    } catch (Exception e) {
    }
    return null;

}

From source file:com.yukthi.utils.CryptoUtils.java

/**
 * Encrypts/Decrypts input string using AES algorithm
 * @param cipherMode/* www .  ja  v  a 2  s  . co  m*/
 * @param secretKeyStr
 * @param input
 * @return
 */
private static byte[] doCrypto(int cipherMode, String secretKeyStr, byte input[]) {
    try {
        Key secretKey = new SecretKeySpec(secretKeyStr.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(cipherMode, secretKey);

        return cipher.doFinal(input);
    } catch (Exception ex) {
        throw new InvalidArgumentException(ex, "Error encrypting/decrypting input bytes");
    }
}

From source file:de.scrubstudios.srvmon.agent.classes.Crypt.java

/**
 * This function is used to encrypt the outgoing data.
 * @param key Encryption key/*from  ww  w . j  a v  a  2s.  c  o  m*/
 * @param data Data which will be encrypted.
 * @return The encrypted data string.
 */
public static String encrypt(String key, String data) {
    byte[] encryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        encryptedData = cipher.doFinal(data.getBytes());

        return new String(Base64.encodeBase64(encryptedData));
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:de.scrubstudios.srvmon.agent.classes.Crypt.java

/**
 * This function is used to decrypt incoming data.
 * @param key Encryption key/* w ww  . j  ava 2  s.  co  m*/
 * @param data Data string which should be decrypted.
 * @return The decrypted data string.
 */
public static String decrypt(String key, String data) {
    byte[] decryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        decryptedData = cipher.doFinal(Base64.decodeBase64(data));

        return new String(decryptedData);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}