Example usage for javax.crypto Cipher init

List of usage examples for javax.crypto Cipher init

Introduction

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

Prototype

public final void init(int opmode, Certificate certificate) throws InvalidKeyException 

Source Link

Document

Initializes this cipher with the public key from the given certificate.

Usage

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

public static String decrypt(String Text) {
    try {/*w w w . j  a v  a 2  s. c om*/
        DESKeySpec keySpec = new DESKeySpec("schluessel_stdrepository15".getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);

        byte[] encrypedPwdBytes = Base64.decodeBase64(Text);
        Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe
        cipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes));

        return new String(plainTextPwdBytes);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:illab.nabal.util.SecurityHelper.java

/**
  * AES-encrypt a plain message. Return null if message is empty.
  * /*from   w w w  .j  av a  2s  .  co  m*/
  * @param message
  * @return AES encrypted hex
  * @throws Exception
  */
public static String cipher(String message) throws Exception {
    if (StringHelper.isEmpty(message) == false) {
        Cipher cipher = Cipher.getInstance(AES);
        cipher.init(Cipher.ENCRYPT_MODE, SYSTEM_SECRET_KEY_SPEC);
        return Base64.encodeToString(cipher.doFinal(message.getBytes()), Base64.DEFAULT);
    } else {
        return null;
    }
}

From source file:illab.nabal.util.SecurityHelper.java

/**
 * Decrypt a AES-encrypted message./*from  www.  ja  v  a  2s  .  c  o  m*/
 * 
 * @param encrypted
 * @return String
 * @throws Exception
 */
public static String decipher(String encrypted) throws Exception {
    if (StringHelper.isEmpty(encrypted) == false) {
        Cipher cipher = Cipher.getInstance(AES);
        cipher.init(Cipher.DECRYPT_MODE, SYSTEM_SECRET_KEY_SPEC);
        return new String(cipher.doFinal(Base64.decode(encrypted, Base64.DEFAULT)), HTTP.UTF_8);
    } else {
        return null;
    }
}

From source file:Main.java

private static byte[] getDesCode(Context mContext, byte[] byteD) {
    Cipher cipher;
    byte[] byteFina = null;
    try {//from   ww w .j  a v  a  2s  .co m
        cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, getKey(mContext));
        byteFina = cipher.doFinal(byteD);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }
    return byteFina;
}

From source file:com.fpt.crypto.CipherDemo.java

public static String encrypt(String in, String key) throws InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
    Cipher cipher = Cipher.getInstance("AES");
    byte[] inputBytes = in.getBytes();
    SecretKey secKey = new SecretKeySpec(key.getBytes(), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, secKey);
    byte[] outputBytes = cipher.doFinal(inputBytes);
    return Hex.encodeHexString(outputBytes);
}

From source file:com.dragoniade.encrypt.EncryptionHelper.java

private static Cipher getEncrypter(String seed) {
    byte[] byteKey = getSeed(seed);
    for (int i = 0; i < algorithmes.length; i++) {
        String algorithm = algorithmes[i];
        try {/*from  w  w w  .  j  a va 2s.c om*/
            SecretKeySpec keySpec = new SecretKeySpec(byteKey, algorithm);
            Cipher encrypt = Cipher.getInstance(algorithm);
            encrypt.init(Cipher.ENCRYPT_MODE, keySpec);
            return encrypt;
        } catch (Exception e) {
            continue;
        }
    }
    return null;
}

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

public static String encryptBase64(String data, String key) throws Exception {

    DESKeySpec desKey = new DESKeySpec(key.getBytes());
    // ?DESKeySpec??
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(desKey);

    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, securekey);

    byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
    // Encrypt/*from   w  w w  .  j  a v  a 2 s  . c  o  m*/
    // byte[] unencryptedByteArray = data.getBytes("UTF8");
    // byte[] encryptedBytes = encryptCipher.doFinal(unencryptedByteArray);

    // Encode bytes to base64 to get a string
    byte[] encodedBytes = Base64.encodeBase64(results);
    return new String(encodedBytes);
}

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?//from   w ww . j a v  a 2s . co  m
    cipher.init(Cipher.DECRYPT_MODE, pub_key);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}

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

/**
 * ?/*w ww  .jav  a 2  s.  c om*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decrypt(String data, String seed) throws Exception {
    KeyPair keyPair = generatorKeyPair(seed);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // ?Cipher?
    cipher.init(Cipher.DECRYPT_MODE, keyPair.getPublic());
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}

From source file:de.hybris.platform.b2b.punchout.services.impl.SymmetricManager.java

public static String encrypt(final String unsecureText, final String key) throws PunchOutCipherException {
    String encrypted = null;//from ww w . j  a  v a 2s .c  o m
    try {
        final Key skeySpec = new SecretKeySpec(new Base64().decode(key), ALGORITHM);
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        final byte[] encryptedValue = cipher.doFinal(unsecureText.getBytes());
        encrypted = new Base64().encodeAsString(encryptedValue);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
        // should never happen
        LOG.error("System was unable instantiate Cipher.", e);
    } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
        final String msg = "Error occured during encryption." + e.getMessage();
        LOG.error(msg);
        throw new PunchOutCipherException(msg, e);
    }

    return encrypted;
}