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:de.hybris.platform.b2b.punchout.services.impl.SymmetricManager.java

public static String decrypt(final String encrypted, final String key) throws PunchOutCipherException {
    String decrypted = null;/*from   w w w.  j a v  a2 s. c o  m*/

    try {
        final Key skeySpec = new SecretKeySpec(new Base64().decode(key), ALGORITHM);
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        final byte[] decodedValue = new Base64().decode(encrypted.getBytes());
        final byte[] decryptedValue = cipher.doFinal(decodedValue);
        decrypted = new String(decryptedValue);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
        // should never happen
        LOG.error("System was unable instantiate Cipher.", e);
    } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
        final String msg = "Error occured during decryption." + e.getMessage();
        LOG.info(msg);
        throw new PunchOutCipherException(msg, e);
    }
    return decrypted;
}

From source file:license.rsa.WakeRSA.java

/**
 * rsa//from  w ww .  j  a  va2s.  c  om
 * @param key
 * @return
 * @throws Exception
 */
private static byte[] rsa(byte[] key) throws Exception {
    PublicKey pubKey = readPublicKeyFromFile();
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, pubKey);
    return cipher.doFinal(key);
}

From source file:net.ftb.util.CryptoUtils.java

/**
 * Method to AES encrypt string if fails, will attempt to use {@link #encryptLegacy(String str, byte[] key)}
 * @param str string to encrypt/*www. j  ava2 s . c  om*/
 * @param key encryption key
 * @return encrypted string or "" if legacy fails
 */
public static String encrypt(String str, byte[] key) {
    try {
        Cipher aes = Cipher.getInstance("AES");
        aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(pad(key), "AES"));
        return Base64.encodeBase64String(aes.doFinal(("FDT:" + str).getBytes("utf8")));
    } catch (Exception e) {
        Logger.logError("Error Encrypting information, reverting to legacy format", e);
        return encryptLegacy(str, key);
    }
}

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

public static String decrypt(String value) throws Exception {
    if (value == null) {
        return "";
    }//from  w ww .j  a va2s.  c o m

    SecretKey secretKey = new SecretKeySpec(_PASSWORD, _ALGORITHM);

    Cipher cipher = Cipher.getInstance(_ALGORITHM);

    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    String decryptedValue = value;

    for (int i = 0; i < _ITERATIONS; i++) {
        byte[] decodedBytes = Base64.decodeBase64(decryptedValue);

        byte[] decryptedBytes = cipher.doFinal(decodedBytes);

        decryptedValue = new String(decryptedBytes, _UTF8_CHARSET).substring(_SALT_LENGTH);
    }

    return decryptedValue;
}

From source file:com._64bitlabs.util.Encryptors.java

/**
 * Encrypts string value with the specified AES algorithm
 * @param Data string to be encrypted//from w w  w. j a v a  2  s . co  m
 * @return encrypted string
 */
public static String encrypt(String Data) {
    try {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        Base64 encoder = new Base64();
        String encryptedValue = new String(encoder.encode(encVal)).trim();
        return encryptedValue;
    } catch (Exception e) {
        return null;
    }
}

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

public static String encrypt(String value) throws Exception {
    if (value == null) {
        return "";
    }/* w ww .j ava  2s.  co m*/

    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:com._64bitlabs.util.Encryptors.java

/**
 * Given AES encryption algorithm, decrypts the string value.
 * @param encryptedData//  www  .j  a  v  a  2 s  . c o  m
 * @return decrypted String
 */
public static String decrypt(String encryptedData) {
    Key key = generateKey();
    try {
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);

        Base64 dec = new Base64();
        byte[] decodedValue = dec.decode(encryptedData.getBytes());
        byte[] decValue = c.doFinal(decodedValue);

        return new String(decValue);
    } catch (Exception e) {
        return null;
    }
}

From source file:de.fhdo.helper.DES.java

public static String encrypt(String Text) {
    try {/*from   ww w. j a  va2 s. c  o  m*/
        DESKeySpec keySpec = new DESKeySpec("schluessel_stdrepository15".getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);

        // ENCODE plainTextPassword String
        byte[] cleartext = Text.getBytes("UTF8");
        Cipher cipher = Cipher.getInstance("DES");
        // cipher is not thread safe
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return Base64.encodeBase64URLSafeString(cipher.doFinal(cleartext));

    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

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

/**
 * Decode the password on base 64/* ww w  .  j  a v a 2s.  com*/
 * @param msg
 * @return
 */
public static String decode(String msg) {
    try {
        SecretKeySpec keySpec = new SecretKeySpec(KEY, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        byte[] de64 = Base64.decodeBase64(msg);
        byte[] decrypted = cipher.doFinal(de64);

        return new String(decrypted);
    } 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:net.ftb.util.CryptoUtils.java

/**
 * Method to AES decrypt string if fails, will attempt to use {@link #decryptLegacy(String str, byte[] key)}
 * @param str string to decrypt/*from   ww w.j a  v  a  2s  .  c om*/
 * @param key decryption key key
 * @return decrypted string or "" if legacy fails
 */
public static String decrypt(String str, byte[] key) {
    try {
        Cipher aes = Cipher.getInstance("AES");
        aes.init(Cipher.DECRYPT_MODE, new SecretKeySpec(pad(key), "AES"));
        String s = new String(aes.doFinal(Base64.decodeBase64(str)), "utf8");
        if (s.startsWith("FDT:") && s.length() > 4)
            return s.split(":", 2)[1];//we don't want the decryption test
        else
            return decryptLegacy(str, key);
    } catch (Exception e) {
        Logger.logError("Error Decrypting information, attempting legacy decryption", e);
        return decryptLegacy(str, key);
    }
}