Example usage for javax.crypto Cipher getInstance

List of usage examples for javax.crypto Cipher getInstance

Introduction

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

Prototype

public static final Cipher getInstance(String transformation)
        throws NoSuchAlgorithmException, NoSuchPaddingException 

Source Link

Document

Returns a Cipher object that implements the specified transformation.

Usage

From source file:Main.java

public static byte[] aesIGEdecrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) {
    try {/* ww w.  j  ava2s .  c  o m*/

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

        byte[] iv2p = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2);
        byte[] ivp = 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.DECRYPT_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:lib.clases_cripto.java

public static String Encriptar(String texto) {

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

    try {// w  w  w  .  j  a v a 2s  .co m

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(texto.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        Md5Crypt.md5Crypt(keyBytes);
        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[] 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  ww . ja  v  a2s.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

/**
 * Encrypts message string using a given symmetric key.
 * @param msg Message string to encrypt.
 * @param key Key to encrypt message with.
 * @return Byte array of encrypted and encoded message.
 * @throws NoSuchAlgorithmException/*from  w  w w  .java2 s.co m*/
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws InvalidAlgorithmParameterException
 */
public static byte[] encryptMessage(String msg, SecretKey key)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException {
    Cipher cipher = Cipher.getInstance("AES");
    byte[] init = new byte[128 / 8];
    //SecureRandom secureRandom = new SecureRandom();
    //secureRandom.nextBytes(init);
    for (int i = 0; i < 16; i++)
        init[i] = 0;
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(init));
    byte[] msgBytes = msg.getBytes();
    //System.out.println(android.util.Base64.encode(msgBytes, Base64.DEFAULT));
    byte[] msgCipherBytes = cipher.doFinal(msgBytes);
    return msgCipherBytes;
}

From source file:com.basp.trabajo_al_minuto.model.business.BusinessSecurity.java

/**
 * Se encarga de encriptar la contrasea ingresada por el usuario *
 *///from   ww  w . j a  v a2  s . co m
public static String encrypt(String value) throws BusinessException {
    String secretKey = "e-business";
    String base64EncryptedString = "";
    try {
        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 = value.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);
    } catch (Exception ex) {
        throw new BusinessException(ex);
    }
    return base64EncryptedString;
}

From source file:com.vico.license.util.rsa.RSAdoEncrypt.java

public static String encrypt(String source, byte[] publickey) throws Exception {

    String path = Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath();
    Key publicKey = null;//from   w  ww  . j a  v a 2s.c o m

    publicKey = (Key) ByteArrayToObj.ByteToObject(publickey);

    /** Cipher???RSA */
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] b = source.getBytes("UTF-8");

    /** ?*/
    byte[] b1 = cipher.doFinal(b);

    String encryptedcode = Base64.encodeBase64String(b1);
    return encryptedcode;
}

From source file:com.agiletec.aps.util.DefaultApsEncrypter.java

public static String decrypt(String source) {
    try {/*from  w w  w  .  j av  a  2s.c o  m*/
        Key key = getKey();
        Cipher desCipher = Cipher.getInstance(TRIPLE_DES);
        byte[] dec = Base64.decodeBase64(source.getBytes());
        desCipher.init(Cipher.DECRYPT_MODE, key);
        byte[] cleartext = desCipher.doFinal(dec);
        // Return the clear text
        return new String(cleartext);
    } catch (Throwable t) {
        throw new RuntimeException("Error decrypting string", t);
    }
}

From source file:encryptdecrypt.util.Security.java

public static String decrypt(String strToDecrypt) {
    try {/*w  ww.java  2s.  co m*/
        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:Main.java

public static byte[] encryptMsg(String msg, RSAPublicKeySpec pubKeySpec) {
    if (msg != null && pubKeySpec != null && !msg.isEmpty()) {
        try {/*from ww w  .j  a  v  a2 s .  c  om*/
            Log.w(TAG, "msg is: " + msg + " with length " + msg.length());
            KeyFactory fact = KeyFactory.getInstance("RSA");

            PublicKey pubKey = fact.generatePublic(pubKeySpec);

            // TODO encrypt the message and send it
            // Cipher cipher = Cipher.getInstance("RSA/None/NoPadding");
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            // Cipher cipher =
            // Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding",
            // "BC");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            Log.d(TAG, "cipher block size is " + cipher.getBlockSize());
            byte[] msgByteArray = msg.getBytes();
            byte[] cipherData = new byte[cipher.getOutputSize(msgByteArray.length)];
            cipherData = cipher.doFinal(msgByteArray);
            Log.d(TAG, "output size is " + cipher.getOutputSize(msgByteArray.length));
            Log.d(TAG, "is the measurement already broken into chunks here? " + (new String(cipherData)));
            return cipherData;

        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "RSA algorithm not available", e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "", e);
        } catch (NoSuchPaddingException e) {
            Log.e(TAG, "", e);
        } catch (InvalidKeyException e) {
            Log.e(TAG, "", e);
        } catch (BadPaddingException e) {
            Log.e(TAG, "", e);
        } catch (IllegalBlockSizeException e) {
            Log.e(TAG, "", e);
        } catch (Exception e) {
            Log.e(TAG, "", e);
        } /*
           * catch (NoSuchProviderException e) { Log.e(TAG, "", e); }
           */
    }
    return null;
}

From source file:Main.java

/**
 * Decrypts a encrypted and encoded message given a key.
 * @param cipherBytes//w  w  w .  j ava  2 s .c  om
 * @param key
 * @return
 * @throws NoSuchAlgorithmException
 * @throws NoSuchAlgorithmException
 * @throws InvalidAlgorithmParameterException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws InvalidAlgorithmParameterException
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 */
public static byte[] decryptMessage(byte[] cipherBytes, SecretKey key) throws NoSuchAlgorithmException,
        NoSuchAlgorithmException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException {
    Cipher cipher = Cipher.getInstance("AES");
    //ipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING");
    byte[] init = new byte[128 / 8];
    SecureRandom secureRandom = new SecureRandom();
    //secureRandom.nextBytes(init);
    for (int i = 0; i < 16; i++)
        init[i] = 0;
    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(init));
    byte[] textBytes = cipher.doFinal(cipherBytes);
    return textBytes;
}