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:com.sysfore.pos.licensemanagement.LicenceManagementUtil.java

public static String decrypt(String strToDecrypt) {
    try {/*from w  ww  .  j a  v a2 s .  c  om*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));

        return decryptedString;
    } catch (Exception e) {
        log.error("Error while decrypting", e);

    }
    return null;
}

From source file:de.scrubstudios.srvmon.notificator.classes.Crypt.java

public static String encrypt(String key, String data) {
    byte[] encryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {/*  ww  w.  j a va 2 s  .  c  o m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        encryptedData = cipher.doFinal(data.getBytes());

        byte[] encr64 = Base64.encodeBase64(encryptedData);

        //System.out.println(new String(encr64));

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

    return null;
}

From source file:DesEncrypter.java

DesEncrypter(SecretKey key) throws Exception {
    ecipher = Cipher.getInstance("DES");
    dcipher = Cipher.getInstance("DES");
    ecipher.init(Cipher.ENCRYPT_MODE, key);
    dcipher.init(Cipher.DECRYPT_MODE, key);
}

From source file:Main.java

private static Cipher getCipherFromPassphrase(String passphrase, byte[] salt, int opMode)
        throws GeneralSecurityException {
    SecretKey key = getKeyFromPassphrase(passphrase, salt);
    Cipher cipher = Cipher.getInstance(key.getAlgorithm());
    cipher.init(opMode, key, new PBEParameterSpec(salt, 100));

    return cipher;
}

From source file:Encrypt.java

private static String encrypt(String message) throws Exception {
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
    IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
    return toHexString(cipher.doFinal(message.getBytes("UTF-8")));
}

From source file:com.liferay.sync.engine.util.Encryptor.java

public static String decrypt(String value) throws Exception {
    if (value == null) {
        return "";
    }//from w w w .java2s .c  o m

    SecretKey secretKey = new SecretKeySpec(_PASSWORD, _ALGORITHM);

    Cipher cipher = Cipher.getInstance(_ALGORITHM);

    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    String decryptedValue = value;

    for (int i = 0; i < _ITERATIONS; i++) {
        byte[] decodedBytes = Base64.decodeBase64(decryptedValue);

        byte[] decryptedBytes = cipher.doFinal(decodedBytes);

        decryptedValue = new String(decryptedBytes, _UTF8_CHARSET).substring(_SALT_LENGTH);
    }

    return decryptedValue;
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String encrypt1(String Data) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(ALGO);
    keyGen.init(128);/*from ww w .j  av  a  2 s .c om*/
    SecretKey key = keyGen.generateKey();
    System.out.println("Generated key: " + key);
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes("UTF8"));

    String encryptedValue = ConvertionHelper.bytesToHex(encVal);
    //        String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

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

public static String decrypt(String cryptograph) throws Exception {
    Key privateKey;//from  ww w  . j a  v  a2 s  .c o m
    String path = Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath();
    ObjectInputStream ois = null;
    try {
        /** ? */
        ois = new ObjectInputStream(new FileInputStream(path + FileNames.PRIVATEKEY_NAME));
        privateKey = (Key) ois.readObject();
    } catch (Exception e) {
        throw e;
    } finally {
        ois.close();
    }

    /** Cipher?RSA */
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    /** ? */
    byte[] b1 = Base64.decodeBase64(cryptograph);
    /** ? */
    byte[] b = cipher.doFinal(b1);
    return new String(b);
}

From source file:Main.java

public static byte[] TriDesEncryption(byte[] byteKey, byte[] dec) {
    try {// ww w .  j a  va  2  s  .  co  m
        byte[] en_key = new byte[24];
        if (byteKey.length == 16) {
            System.arraycopy(byteKey, 0, en_key, 0, 16);
            System.arraycopy(byteKey, 0, en_key, 16, 8);
        }
        SecretKeySpec key = new SecretKeySpec(en_key, "DESede");
        Cipher ecipher = Cipher.getInstance("DESede/ECB/NoPadding");
        ecipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] en_b = ecipher.doFinal(dec);
        return en_b;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

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

/**
 * ?/*from   w  w  w.j a v a  2  s .c om*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String encrypt(String data, String seed) throws Exception {
    KeyPair keyPair = generatorKeyPair(seed);
    // Cipher??
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // SecureRandom random = new SecureRandom();
    // ?Cipher?
    cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate());
    byte[] results = cipher.doFinal(data.getBytes());
    // http://tripledes.online-domain-tools.com/??
    for (int i = 0; i < results.length; i++) {
        System.out.print(results[i] + " ");
    }
    System.out.println();
    // ??Base64?
    return Base64.encodeBase64String(results);
}