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:org.oscarehr.common.hl7.v2.oscar_to_oscar.SendingUtils.java

private static byte[] encryptData(byte[] dataBytes, SecretKey senderSecretKey) throws InvalidKeyException,
        NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    SecretKeySpec secretKeySpec = new SecretKeySpec(senderSecretKey.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    return (cipher.doFinal(dataBytes));
}

From source file:com.ikon.util.SecureStore.java

/**
 * DES encoder//from w w  w.ja  v  a 2s  .  com
 */
public static byte[] desEncode(String key, byte[] src)
        throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(keySpec);

    Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
    cipher.init(Cipher.ENCRYPT_MODE, sKey);
    byte[] dst = cipher.doFinal(src);

    return dst;
}

From source file:com.ikon.util.SecureStore.java

/**
 * DES decoder//w  ww  .j a  va 2s .c o  m
 */
public static byte[] desDecode(String key, byte[] src)
        throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(keySpec);

    Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
    cipher.init(Cipher.DECRYPT_MODE, sKey);
    byte[] dst = cipher.doFinal(src);

    return dst;
}

From source file:com.credomatic.gprod.db2query2csv.Security.java

/**
 * Cifra una cadena de carateres utilizando el algoritmo AES y una llave (128, 256, o 512 bits). 
 * @param KeySize tamao de la llave autogenerada para relizar el cifrado
 * @param value cadena de caracteres que sera cifrada
 * @return instancia de tipo ${@link SecurityParams} con el resultado del proceso de cifrado
 *//*from  www . ja v  a 2 s  .c  o m*/
public static SecurityParams encrypt(int KeySize, String value) {

    SecurityParams result = null;
    try {
        // Get the KeyGenerator
        final KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(KeySize);

        // Generate the secret key specs.
        final SecretKey skey = kgen.generateKey();
        final byte[] raw = skey.getEncoded();
        final SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        final Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        final String key = new Base64().encodeAsString(raw);
        final String encrypt = (new Base64()).encodeAsString(cipher.doFinal(value.getBytes()));

        result = new SecurityParams(encrypt, key, KeySize);

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

From source file:com.example.license.DESUtil.java

/**
 * ?/*from   w  ww  . jav a  2s.  co m*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decrypt(String data, String key) throws Exception {
    Key deskey = keyGenerator(key);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // ?Cipher?
    cipher.init(Cipher.DECRYPT_MODE, deskey);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}

From source file:de.dennishoersch.web.chat.Encryption.java

private static String decryptRSA(Key key, byte[] input) {
    try {/*ww w.j a v  a  2  s  . c  o m*/
        Cipher rsa;
        rsa = Cipher.getInstance("RSA");
        rsa.init(Cipher.DECRYPT_MODE, key);
        byte[] utf8 = rsa.doFinal(input);
        return new String(utf8, "UTF8");
    } catch (Exception e) {
        throw Throwables.throwUnchecked(e);
    }
}

From source file:models.logic.CipherDecipher.java

public static void encryptOrDecrypt(SecretKey key, int mode, InputStream is, OutputStream os) throws Throwable {
    Cipher cipher = Cipher.getInstance("AES");
    if (mode == Cipher.ENCRYPT_MODE) {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        doCopy(cis, os);/*  w ww  .j  a  v  a  2s.  c o m*/
    } else if (mode == Cipher.DECRYPT_MODE) {
        cipher.init(Cipher.DECRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        doCopy(is, cos);
    }
}

From source file:com.miyue.util.Cryptos.java

/**
 * AES?, ?.//from w  ww.  java2s  .c  o  m
 * 
 * @param input 
 * @param key ?AES?
 * @param mode Cipher.ENCRYPT_MODE  Cipher.DECRYPT_MODE
 */
private static byte[] aes(byte[] input, byte[] key, int mode) {
    try {
        SecretKey secretKey = new SecretKeySpec(key, AES);
        Cipher cipher = Cipher.getInstance(AES);
        cipher.init(mode, secretKey);
        return cipher.doFinal(input);
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:com.sshutils.utils.CryptHelper.java

public static String encrypt(String strToEncrypt) {
    try {/*ww w. j  av a2  s .  c o m*/

        Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE, "BC");
        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) {
        log.error("Error while encrypting", e);
    }
    return null;

}

From source file:com.tremolosecurity.provisioning.customTasks.CreateOTPKey.java

public static String generateEncryptedToken(String userID, GoogleAuthenticatorKey key, String hostName,
        ConfigManager cfg, String encryptionKey) throws ProvisioningException {
    TOTPKey totpkey = new TOTPKey();
    totpkey.setHost(hostName);/*from w w  w.  ja  va  2s  .c  om*/
    totpkey.setScratchCodes(key.getScratchCodes());
    totpkey.setSecretKey(key.getKey());
    totpkey.setUserName(userID);
    totpkey.setValidationCode(key.getVerificationCode());

    Gson gson = new Gson();
    String json = gson.toJson(totpkey);
    SecretKey sc = cfg.getSecretKey(encryptionKey);
    String attrVal = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(json.getBytes("UTF-8"));

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, sc);

        byte[] encJson = cipher.doFinal(baos.toByteArray());
        String base64d = new String(org.bouncycastle.util.encoders.Base64.encode(encJson));

        Token token = new Token();
        token.setEncryptedRequest(base64d);
        token.setIv(new String(org.bouncycastle.util.encoders.Base64.encode(cipher.getIV())));

        json = gson.toJson(token);
        attrVal = new String(org.bouncycastle.util.encoders.Base64.encode(json.getBytes("UTF-8")));

    } catch (Exception e) {
        throw new ProvisioningException("Could not encrypt key", e);
    }
    return attrVal;
}