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:io.manasobi.utils.CryptoUtils.java

/**
 *  .//  w w w.  j a v  a  2s.  c o m
 *
 * @param keyHex generateHexKey ? ? ?? Hex ? ? 
 * @param data    ?? byte 
 * 
 * @return  ? ??
 */
public static byte[] encryptByDES(String keyHex, byte[] data) {

    SecretKey key = getSecretDESKeyFromHex(keyHex);

    byte[] encryptedData = null;

    try {

        Cipher cipher = Cipher.getInstance(ALGORITHM);

        cipher.init(Cipher.ENCRYPT_MODE, key);

        encryptedData = cipher.doFinal(data);

    } catch (Exception e) {

        throw new CryptoUtilsException(e.getMessage());
    }

    return encryptedData;
}

From source file:io.manasobi.utils.CryptoUtils.java

/**
 *  ./* www.  ja  v  a2 s .com*/
 *
 * @param keyHex generateHexKey ? ? ?? Hex ? ? 
 * @param data    ? ?
 *             
 * @return ? ?? byte 
 */
public static byte[] decryptByDES(String keyHex, byte[] data) {

    SecretKey key = getSecretDESKeyFromHex(keyHex);

    byte[] decryptedData = null;

    try {

        Cipher cipher = Cipher.getInstance(ALGORITHM);

        cipher.init(Cipher.DECRYPT_MODE, key);

        decryptedData = cipher.doFinal(data);

    } catch (Exception e) {

        throw new CryptoUtilsException(e.getMessage());
    }

    return decryptedData;
}

From source file:edu.hku.sdb.udf.util.UDFHandler.java

private static boolean search(byte[] encrypted, String keyword, SecretKey prkey) {
    byte[] keywordbyte = keyword.getBytes();
    if (keywordbyte.length != encrypted.length) {
        return false;
    }/*from w ww. j a v  a  2s .  c  om*/
    byte[] diff1 = new byte[keywordbyte.length - 1];
    for (int i = 0; i < diff1.length; i++) {
        diff1[i] = (byte) (encrypted[i] ^ keywordbyte[i]);
    }
    byte diff2 = (byte) (encrypted[diff1.length] ^ keywordbyte[diff1.length]);
    // get diff = word xor encrypted
    // if it is a match, first part of diff generates second part of diff using pseudo random function

    try {
        // pseudorandom cipher
        Cipher prCipher = Cipher.getInstance(SCHEME);
        prCipher.init(Cipher.ENCRYPT_MODE, prkey);

        // si is chopped with the desired length

        byte[] fksi = prCipher.doFinal(diff1);
        // fksi is F_k(S_i) in the searchable encryption
        // we use the same AES cipher for simplicity

        return fksi[0] == diff2;
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}

From source file:com.alkacon.opencms.commons.CmsStringCrypter.java

/**
 * Decrypts the given value which was encrypted with the encrypt method.<p>
 * //from  w  ww.j  a va 2 s  .  co m
 * @param value the value to be decrypted
 * @param password the passsword used for decryption, has to be the same as used for encryption
 * @return the decrypted string of the value or null if something went wrong
 */
public static String decrypt(String value, String password) {

    // check if given value is valid
    if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
        if (LOG.isWarnEnabled()) {
            LOG.warn(Messages.get().getBundle().key(Messages.LOG_WARN_INVALID_DECRYPT_STRING_1, value));
        }
        return null;
    }

    try {

        // create key
        Key key = new SecretKeySpec(getKey(password), ENCRYPTION);
        Cipher cipher = Cipher.getInstance(ENCRYPTION);
        cipher.init(Cipher.DECRYPT_MODE, key);

        // decode from base64
        BASE64Decoder base64decoder = new BASE64Decoder();
        byte[] cleartext = base64decoder.decodeBuffer(value);

        // decrypt text
        byte[] ciphertext = cipher.doFinal(cleartext);
        return CmsEncoder.decode(new String(ciphertext));
    } catch (Exception ex) {
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_DECRPYT_0), ex);
        }
    }

    return null;
}

From source file:Main.java

public static byte[] aesIGEencrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) {
    try {//  w w  w .  j av a  2  s.  c o  m

        ByteBuffer out = ByteBuffer.allocate(data.length);

        byte[] ivp = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2);
        byte[] iv2p = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length);

        int len = data.length / AES_BLOCK_SIZE;

        byte[] xorInput = null;
        byte[] xorOutput = null;

        SecretKeySpec keySpec = null;
        keySpec = new SecretKeySpec(tmpAesKey, "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] input = null;
        byte[] output = null;

        for (int i = 0; i < len; i++) {

            input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE);
            xorInput = xor(input, ivp);
            output = cipher.doFinal(xorInput);
            xorOutput = xor(output, iv2p);
            out.put(xorOutput);

            ivp = xorOutput;
            iv2p = input;
        }
        return out.array();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.alkacon.opencms.commons.CmsStringCrypter.java

/**
 * Encrypts the given value.<p>/*  ww w  .java 2s . c om*/
 * 
 * @param value the string which should be encrypted
 * @param password the passsword used for encryption, use the same password for decryption
 * @return the encrypted string of the value or null if something went wrong
 */
public static String encrypt(String value, String password) {

    // check if given value is valid
    if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
        if (LOG.isWarnEnabled()) {
            LOG.warn(Messages.get().getBundle().key(Messages.LOG_WARN_INVALID_ENCRYPT_STRING_1, value));
        }
        return null;
    }

    try {

        // create key
        byte[] k = getKey(password);
        Key key = new SecretKeySpec(k, ENCRYPTION);
        Cipher cipher = Cipher.getInstance(ENCRYPTION);
        cipher.init(Cipher.ENCRYPT_MODE, key);

        // encrypt text
        byte[] cleartext = value.getBytes(FORMAT);
        byte[] ciphertext = cipher.doFinal(cleartext);

        // encode with base64 to be used as a url parameter
        BASE64Encoder base64encoder = new BASE64Encoder();
        return CmsEncoder.encode(base64encoder.encode(ciphertext));
    } catch (Exception ex) {
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_ENCRYPT_0), ex);
        }
    }

    return null;
}

From source file:com.jsmartframework.web.manager.TagEncrypter.java

private static Cipher getEncryptCipher(HttpServletRequest request) throws Exception {
    Cipher encryptCipher = (Cipher) request.getAttribute(REQUEST_TAG_ENCRYPT_CIPHER);
    if (encryptCipher == null) {
        encryptCipher = Cipher.getInstance("AES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        request.setAttribute(REQUEST_TAG_ENCRYPT_CIPHER, encryptCipher);
    }//  www. j a v  a  2  s . c  om
    return encryptCipher;
}

From source file:com.jsmartframework.web.manager.TagEncrypter.java

private static Cipher getDecryptCipher(HttpServletRequest request) throws Exception {
    Cipher decryptCipher = (Cipher) request.getAttribute(REQUEST_TAG_DECRYPT_CIPHER);
    if (decryptCipher == null) {
        decryptCipher = Cipher.getInstance("AES");
        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
        request.setAttribute(REQUEST_TAG_DECRYPT_CIPHER, decryptCipher);
    }/*  ww w  .j  av a  2  s  . co  m*/
    return decryptCipher;
}

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

public static String makeTicket(String username, String hash, long ticketValidity) {
    Cipher cipher;
    try {//from   w ww .ja va 2  s .com
        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:cloudeventbus.pki.CertificateUtils.java

public static byte[] signChallenge(PrivateKey key, byte[] challenge, byte[] salt) {
    try {/*  www  .  j a  va2 s.  c  om*/
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        cipher.update(challenge);
        cipher.update(salt);
        return cipher.doFinal();
    } catch (GeneralSecurityException e) {
        throw new CertificateSecurityException(e);
    }
}