Example usage for javax.crypto Cipher doFinal

List of usage examples for javax.crypto Cipher doFinal

Introduction

In this page you can find the example usage for javax.crypto Cipher doFinal.

Prototype

public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException 

Source Link

Document

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

Usage

From source file:com.sds.acube.ndisc.mts.xserver.util.XNDiscCipher.java

/**
 * /* ww  w  .  j  a  va  2 s . c om*/
 * 
 * @param data ? ?
 * @return ? ?
 */
public static String decode(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte d[] = cipher.doFinal(Base64.decodeBase64(data.getBytes("UTF-8")));
        return replaceChar(new String(d));
    } catch (NoSuchAlgorithmException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (NoSuchPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (IOException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (InvalidKeyException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (IllegalBlockSizeException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (BadPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    }
    return null;
}

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

public static String encrypt(String Text) {
    try {//from   w  w  w. j  av  a 2  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:encryptdecrypt.util.Security.java

public static String decrypt(String strToDecrypt) {
    try {//from ww  w  .j  a v a2s . c om
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt.getBytes())));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:BD.Encriptador.java

public static String Encriptar(String texto) {

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

    try {//from ww w  . j  av  a  2 s.c  om

        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 cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);

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

From source file:Main.java

public static byte[] DESTemplet(byte[] data, byte[] key, String algorithm, String transformation,
        boolean isEncrypt) {
    try {//  w w w .  j a va  2s .co m
        SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
        Cipher cipher = Cipher.getInstance(transformation);
        SecureRandom random = new SecureRandom();
        cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random);
        return cipher.doFinal(data);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] desTemplate(byte[] data, byte[] key, String algorithm, String transformation,
        boolean isEncrypt) {
    if (data == null || data.length == 0 || key == null || key.length == 0)
        return null;
    try {// w w  w  .  j a va 2  s  .  co m
        SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
        Cipher cipher = Cipher.getInstance(transformation);
        SecureRandom random = new SecureRandom();
        cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random);
        return cipher.doFinal(data);
    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static byte[] Des(byte[] byteData, byte[] byteKey, int opmode)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException {
    Cipher cipher = null;
    try {//from w w  w.j  a v  a 2 s.  c o  m
        SecretKeySpec desKey = new SecretKeySpec(byteKey, "DES");

        cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(opmode, desKey);

        return cipher.doFinal(byteData);
    } finally {
        cipher = null;
    }
}

From source file:ar.gob.ambiente.servicios.gestionpersonas.entidades.util.CriptPass.java

/**
 * Mtodo para desencriptar una contrasea encriptada
 * @param textoEncriptado//w  ww.j  a v  a  2s.  c  o  m
 * @return
 * @throws Exception 
 */
public static String desencriptar(String textoEncriptado) throws Exception {

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

    try {
        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 (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        System.out.println(ex.getMessage());
    }
    return base64EncryptedString;
}

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

public static String decrypt(String data, PublicKey pub_key) throws Exception {
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // ?Cipher?/*w  w w . j a  v a  2  s.  c om*/
    cipher.init(Cipher.DECRYPT_MODE, pub_key);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}

From source file:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java

private static byte[] decryptData(byte[] bInput) {
    byte[] outBytes = null;
    try {//from   w w w . j a v a2  s. c  om
        Key key = new SecretKeySpec(secret_key, "DESede");

        Cipher cipher = Cipher.getInstance("DESede", "SunJCE");

        cipher.init(Cipher.DECRYPT_MODE, key, cipher.getParameters());

        outBytes = cipher.doFinal(bInput);

    } catch (Exception ex) {
        //log.error("PacketUtil",ex.getMessage(), ex);
        return outBytes;
    }

    return outBytes;
}