Example usage for javax.crypto Cipher DECRYPT_MODE

List of usage examples for javax.crypto Cipher DECRYPT_MODE

Introduction

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

Prototype

int DECRYPT_MODE

To view the source code for javax.crypto Cipher DECRYPT_MODE.

Click Source Link

Document

Constant used to initialize cipher to decryption mode.

Usage

From source file:info.fcrp.keepitsafe.bean.CryptBeanTest.java

@Test
public void assymetric() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(1024, new SecureRandom());
    KeyPair kp = kpg.generateKeyPair();
    PrivateKey priKey = kp.getPrivate();
    PublicKey pubKey = kp.getPublic();

    Cipher c = Cipher.getInstance("RSA");
    String plain = "plain";
    byte[] plainBytes = plain.getBytes();

    c.init(Cipher.ENCRYPT_MODE, pubKey);
    c.update(plainBytes);/*from w  w w  .  j  a  v  a 2  s  . co m*/

    byte[] encBytes = c.doFinal();
    String enc = Base64.encodeBase64String(encBytes);
    assertNotSame(plain, enc);

    c.init(Cipher.DECRYPT_MODE, priKey);
    c.update(encBytes);
    byte[] decBytes = c.doFinal();
    String dec = new String(decBytes);

    assertEquals(plain, dec);
}

From source file:de.codesourcery.eve.apiclient.utils.PasswordCipherProvider.java

@Override
public Cipher createCipher(boolean decrypt) {

    final char[] password = getPassword();

    if (ArrayUtils.isEmpty(password)) {
        log.warn("createCipher(): No password , returning NULL cipher.");
        return new NullCipher();
    }//w ww .  j  ava 2s. co  m

    try {

        final int iterations = 20;

        final byte[] salt = new byte[] { (byte) 0xab, (byte) 0xfe, 0x03, 0x47, (byte) 0xde, (byte) 0x99,
                (byte) 0xff, 0x1c };

        final PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iterations);

        final PBEKeySpec spec = new PBEKeySpec(password, salt, iterations);
        final SecretKeyFactory fac = SecretKeyFactory.getInstance("PBEWithMD5andDES");
        final SecretKey secretKey;
        try {
            secretKey = fac.generateSecret(spec);
        } catch (InvalidKeySpecException e) {
            throw e;
        }

        final Cipher cipher = Cipher.getInstance("PBEWithMD5andDES");
        if (decrypt) {
            cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeSpec);
        } else {
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeSpec);
        }
        return cipher;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:cl.niclabs.tscrypto.common.messages.EncryptedData.java

/**
 * Returns a decrypted version of the included data
 * @return/*from www  .j  a v a 2 s.c  om*/
 * @throws NoSuchAlgorithmException
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 */
public byte[] decrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        IllegalBlockSizeException, BadPaddingException {

    byte[] plainAESKey = KeyChain.getInstance().decrypt(rsaKeyAlias, encryptedKey);
    SecretKeySpec skeySpec = new SecretKeySpec(plainAESKey, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    return cipher.doFinal(Base64.decodeBase64(encryptedData));
}

From source file:com.sldeditor.common.property.EncryptedPropertiesApache.java

@Override
public void initialise(String password) {
    PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
    SecretKeyFactory kf;/*from  www .jav a2s. c  o  m*/
    try {
        kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(password.toCharArray()));
        encrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        decrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        encrypter.init(Cipher.ENCRYPT_MODE, k, ps);
        decrypter.init(Cipher.DECRYPT_MODE, k, ps);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
}

From source file:testFileHandler.java

public void decrypt(String encryptedText, String secretKey, javax.swing.JTextArea plainTextField)
        throws Exception {

    byte[] message = Base64.decodeBase64(encryptedText.getBytes("utf-8"));

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    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);
    plainTextField.setText(new String(plainText, "UTF-8"));
}

From source file:jfs.sync.meta.AbstractMetaStorageAccess.java

protected Map<String, FileInfo> getMetaData(String rootPath, String relativePath) {
    if (directoryCache.containsKey(relativePath)) {
        return directoryCache.get(relativePath);
    } // if// w w  w  .  j  a  v a 2 s  .  c o  m
    Map<String, FileInfo> result = new HashMap<>();
    ObjectInputStream ois = null;
    try {
        InputStream inputStream = getInputStream(rootPath, getMetaDataPath(relativePath));
        byte[] credentials = getCredentials(relativePath);
        Cipher cipher = SecurityUtils.getCipher(getCipherSpec(), Cipher.DECRYPT_MODE, credentials);
        inputStream = new CipherInputStream(inputStream, cipher);
        if (LOG.isDebugEnabled()) {
            LOG.debug("getMetaData() reading infos for " + relativePath);
        } // if
        ois = new ObjectInputStream(inputStream);
        Object o;
        while ((o = ois.readObject()) != null) {
            if (o instanceof FileInfo) {
                FileInfo fi = (FileInfo) o;
                if (fi.isDirectory()) {
                    String date;
                    synchronized (FORMATTER) {
                        date = FORMATTER.format(new Date(fi.getModificationDate()));
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(
                                "getMetaData() " + relativePath + getSeparator() + fi.getName() + ": " + date);
                    } // if
                } // if
                result.put(fi.getName(), fi);
            } // if
        } // while
        ois.close();
    } catch (FileNotFoundException | EOFException e) {
        // empty directory or - who cares?
    } catch (Exception e) {
        if (LOG.isInfoEnabled()) {
            LOG.info("getMetaData() possible issue while reading infos " + e, e);
        } // if
    } finally {
        try {
            if (ois != null) {
                ois.close();
            } // if
        } catch (Exception ex) {
            // who cares?
        } // try/catch
    } // try/catch
    directoryCache.put(relativePath, result);
    return result;
}

From source file:cipher.UsableCipher.java

public String decrypt(byte[] cypherText) throws Exception {
    initCipher(Cipher.DECRYPT_MODE);
    byte[] plainText = cipher.doFinal(cypherText);
    return new String(plainText);
}

From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java

public static String decrypt(String key, String encrypted) throws Exception {
    Key skeySpec = generateKeySpec(key);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", new BouncyCastleProvider());
    String abc = encrypted.substring(0, 16);
    System.out.println(abc);/*from  w  ww.j a  va  2  s  .c  o  m*/
    byte[] ivBytes = abc.getBytes();
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
    //cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decodedBytes = Base64.decodeBase64(encrypted.substring(16).getBytes());
    byte[] original = cipher.doFinal(decodedBytes);
    return new String(original);
}

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

public static String decrypt(Key key, String encryptedString) throws EncryptorException {

    try {//from w ww.ja va2s .  c  o m
        Security.addProvider(getProvider());

        Cipher cipher = Cipher.getInstance(key.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, key);

        byte[] encryptedBytes = Base64.decode(encryptedString);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

        String decryptedString = new String(decryptedBytes, ENCODING);

        return decryptedString;
    } catch (Exception e) {
        throw new EncryptorException(e);
    }
}

From source file:com.aqnote.shared.encrypt.symmetric.AES.java

private static void generateCipher(String rawKey) {
    try {/*from  w ww.j  a  v a 2s.  c o m*/
        SecretKeySpec keySpec = new SecretKeySpec(rawKey.getBytes(ENCODE_UTF_8), ALGORITHM);
        encodeCipher = Cipher.getInstance(ALGORITHM);
        encodeCipher.init(Cipher.ENCRYPT_MODE, keySpec);
        decodeCipher = Cipher.getInstance(ALGORITHM);
        decodeCipher.init(Cipher.DECRYPT_MODE, keySpec);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

}