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:lib.clases_cripto.java

public static String Desencriptar(String textoEncriptado) throws Exception {

    String secretKey = "qualityinfosolutions"; //llave para desenciptar datos
    String base64EncryptedString = "";

    try {/* w ww.j  a  va 2 s.com*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:aes_encryption.AES_Encryption.java

public static String encrypt(String strToEncrypt) {
    try {/*from  ww  w . j  a  v  a2  s  . c  o  m*/
        Cipher cipher = Cipher.getInstance("AES_Encryption/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))));
    } catch (Exception e) {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

From source file:BD.Encriptador.java

public static String Desencriptar(String textoEncriptado) throws Exception {

    String secretKey = "qualityinfosolutions"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {// w  w w .  j  a va2  s  . c om
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:de.serverfrog.pw.crypt.SerpentUtil.java

public static CipherOutputStream getOutputStream(OutputStream os, Key key) {
    try {//from w  w  w .  j  av  a  2 s .  com
        Cipher instance = Cipher.getInstance("Serpent", "BC");
        instance.init(Cipher.ENCRYPT_MODE, key);
        return new CipherOutputStream(os, instance);
    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
            | NoSuchPaddingException ex) {
        Logger.getLogger(SerpentUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:hudson.util.Protector.java

public static String protect(String secret) {
    try {//from w  w w. j  a  v  a2 s. c o m
        Cipher cipher = Secret.getCipher(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, DES_KEY);
        return new String(Base64.encodeBase64(cipher.doFinal((secret + MAGIC).getBytes("UTF-8"))));
    } catch (GeneralSecurityException e) {
        throw new Error(e); // impossible
    } catch (UnsupportedEncodingException e) {
        throw new Error(e); // impossible
    }
}

From source file:com.liferay.sync.engine.lan.util.LanTokenUtil.java

public static String createEncryptedToken(String lanTokenKey) throws Exception {

    String lanToken = RandomStringUtils.random(32, 0, 0, true, true, null, _secureRandom);

    byte[] bytes = DigestUtils.sha1(lanTokenKey);

    bytes = Arrays.copyOf(bytes, 16);

    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytes, "AES"));

    String encryptedToken = Base64
            .encodeBase64String(cipher.doFinal(lanToken.getBytes(Charset.forName("UTF-8"))));

    _lanTokens.add(lanToken);/*w  w  w. ja  va2s  .  co m*/

    return encryptedToken;
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static String encryptUsingAES(SecretKey secretKey, String dataToEncrypt) throws NoSuchPaddingException,
        NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher aesCipher = Cipher.getInstance("AES");
    aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] byteCipherText = aesCipher.doFinal(dataToEncrypt.getBytes());
    return Base64.getEncoder().encodeToString(byteCipherText);
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static String decryptUsingAES(SecretKey secretKey, String dataToDecrypt) throws NoSuchPaddingException,
        NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher aesCipher = Cipher.getInstance("AES");
    aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] bytePlainText = aesCipher.doFinal(Base64.getDecoder().decode(dataToDecrypt));
    return new String(bytePlainText);
}

From source file:de.serverfrog.pw.crypt.SerpentUtil.java

public static CipherInputStream getInputStream(InputStream os, Key key) throws IOException {
    try {/*from   ww w.  j  av  a2 s .  co m*/
        Cipher instance = Cipher.getInstance("Serpent", "BC");
        instance.init(Cipher.DECRYPT_MODE, key);
        return new CipherInputStream(os, instance);
    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
            | NoSuchPaddingException ex) {
        if (ex.getClass().equals(InvalidKeyException.class)) {
            throw new IOException("Password Wrong");
        }
        Logger.getLogger(SerpentUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.wso2telco.cryptosystem.AESencrp.java

/**
 * Encrypt./*  w  w  w  . jav a 2s .c o  m*/
 *
 * @param Data the data
 * @return the string
 * @throws Exception the exception
 */
public static String encrypt(String Data) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    //String encryptedValue = new BASE64Encoder().encode(encVal);
    String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}