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:de.mpg.escidoc.services.aa.crypto.RSAEncoder.java

public static String rsaEncrypt(String string) throws Exception {
    StringWriter resultWriter = new StringWriter();
    byte[] bytes = string.getBytes("UTF-8");
    PublicKey pubKey = (PublicKey) readKeyFromFile(Config.getProperty("escidoc.aa.public.key.file"), true);
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    int blockSize = 245;
    for (int i = 0; i < bytes.length; i += blockSize) {
        byte[] result = cipher.doFinal(bytes, i, (i + blockSize < bytes.length ? blockSize : bytes.length - i));
        if (i > 0) {
            resultWriter.write("&");
        }/*  w  ww .  j  a v a 2s  .  c  o  m*/
        resultWriter.write("auth=");
        resultWriter.write(URLEncoder.encode(new String(Base64.encodeBase64(result)), "ISO-8859-1"));
    }
    return resultWriter.toString();

}

From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java

License:asdf

@Test
public void testDES() throws Exception {
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    // for CBC; must be 8 bytes
    byte[] initVector = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C };

    AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
    Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

    m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
    m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);

    byte[] clearText = "?????;(*)*$(R%*PDSJF>XJIPUFIWE(*#*&$)@#*"
            .getBytes();//from   www .j  a  v a  2 s.  co  m

    System.out.println(clearText.length);

    byte[] encryptedText = m_encrypter.doFinal(clearText);
    System.out.println(encryptedText.length);

    byte[] decryptedText = m_decrypter.doFinal(encryptedText);
    System.out.println(decryptedText.length);

    System.out.println(new String(clearText));
    System.out.println(new String(encryptedText));
    System.out.println(new String(decryptedText));

}

From source file:com.vmware.fdmsecprotomgmt.PasswdEncrypter.java

/**
 * Encrypt the value with key provided//from  w ww. j a  v a2s.c  o  m
 */
private static String encrypt(String key, String value) {
    String encryptedString = null;
    try {
        IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes());
        encryptedString = Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        System.out.println("Caught exception while encrypting string : " + value);
        ex.printStackTrace();
    }

    return encryptedString;
}

From source file:com.boulmier.machinelearning.jobexecutor.encrypted.AES.java

public String encrypt(String strToEncrypt) {
    try {//from  ww  w  .j  ava2 s  .  c  om
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        return (Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))));

    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

From source file:com.arwantech.docjavaui.utils.TripleDES.java

public TripleDES(String secretKey) throws Exception {
    myEncryptionKey = secretKey;/*w  w  w .  jav a2 s  . c o  m*/
    myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
    arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
    ks = new DESedeKeySpec(arrayBytes);
    skf = SecretKeyFactory.getInstance(myEncryptionScheme);
    cipher = Cipher.getInstance(myEncryptionScheme);
    key = skf.generateSecret(ks);
}

From source file:encrypt.algorithms.AESCBC.java

public String decrypt(String key1, String key2, String encrypted) {
    try {/* w ww .  j  av a  2 s . c om*/
        IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));

        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:aes_encryption.AES_Encryption.java

public static String decrypt(String strToDecrypt) {

    try {//from www  . ja  v a2s  . c  o  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");

        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt))));

    } catch (Exception e) {

        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}

From source file:com.sv.udb.controlador.CtrlContras.java

public String decrypt(String encrypted) throws Exception {
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    byte[] enc = decodeBase64(encrypted);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] decrypted = cipher.doFinal(enc);
    return new String(decrypted);
}

From source file:com.blackcrowsys.sinscrypto.AesEncryptor.java

@Override
public String encrypt(String secretkey, String iv, String toEncrypt)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException, DecoderException {
    Cipher cipher = Cipher.getInstance(AESMODE);
    SecretKeySpec secretKeySpec = new SecretKeySpec(secretkey.getBytes(), AES);
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray())));
    return Base64.encodeBase64String(cipher.doFinal(toEncrypt.getBytes()));
}

From source file:com.soctec.soctec.utils.Encryptor.java

/**
 * Constructs the encryptor.//  ww w.  j av a 2 s .  c om
 * Creates the needed keys and cipher for encryption.
 *
 */
public Encryptor() {

    try {
        keySpec = new DESKeySpec(keyString.getBytes("UTF8"));
        keyFactory = SecretKeyFactory.getInstance("DES");
        key = keyFactory.generateSecret(keySpec);

        ecipher = Cipher.getInstance("DES");
        decipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        decipher.init(Cipher.DECRYPT_MODE, key);

    } catch (InvalidKeyException | UnsupportedEncodingException | NoSuchAlgorithmException
            | InvalidKeySpecException | NoSuchPaddingException e) {
        e.printStackTrace();
    }
}