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:br.com.zeros.tipsandtricks.cryto.EncryptionStrings.java

public static String encrypt(String valueToEnc) throws Exception {
    Key key = generateKey();/*ww  w.  j ava 2s  .  c  o  m*/
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encValue = c.doFinal(valueToEnc.getBytes());
    String encryptedValue = new Base64().encodeToString(encValue);
    return encryptedValue;
}

From source file:Main.java

/**
 * Encrypt data//from ww w .j  a  v a 2 s.co  m
 * @param secretKey   -   a secret key used for encryption
 * @param data      -   data to encrypt
 * @return   Encrypted data
 * @throws Exception
 */
public static String cipher(String secretKey, String data) throws Exception {
    // Key has to be of length 8
    if (secretKey == null || secretKey.length() != 8)
        throw new Exception("Invalid key length - 8 bytes key needed!");

    SecretKey key = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, key);

    return toHex(cipher.doFinal(data.getBytes()));
}

From source file:Main.java

/**
 * RSA encryption//ww w .j a va2s .c  om
 * @param pubKey
 * @param message
 * @return
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
public static byte[] encryptRSA(RSAPublicKey pubKey, byte[] message) throws IllegalBlockSizeException,
        BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
    Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);

    return cipher.doFinal(message);
}

From source file:Main.java

/**
 * RSA decryption//w  w w .  j  a va2s  .  c o  m
 * @param priKey
 * @param message
 * @return
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
public static byte[] decryptRSA(RSAPrivateKey priKey, byte[] message) throws IllegalBlockSizeException,
        BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
    Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, priKey);

    return cipher.doFinal(message);
}

From source file:br.com.zeros.tipsandtricks.cryto.EncryptionStrings.java

public static String decrypt(String encryptedValue) throws Exception {
    Key key = generateKey();/*w  ww .  ja  va2s  .  c  o m*/
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new Base64().decode(encryptedValue);
    byte[] decValue = c.doFinal(decordedValue);//////////LINE 50
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:MainClass.java

public static void desEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;/*w w  w . j av a2  s.  c om*/
    ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser"));
    key = (SecretKey) keyFile.readObject();
    keyFile.close();

    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    key = keygen.generateKey();
    ObjectOutputStream keyFileout = new ObjectOutputStream(new FileOutputStream("DESKey.ser"));
    keyFileout.writeObject(key);
    keyFileout.close();
    Cipher cipher = null;
    cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(f1));

    CipherOutputStream out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(f2)), cipher);
    int i;
    do {
        i = in.read();
        if (i != -1)
            out.write(i);
    } while (i != -1);
    in.close();
    out.close();
}

From source file:Main.java

public static Cipher createCipher(String algorithmProviderPadding, SecretKey sk, int mode) {

    try {/*  ww  w  . j  a  va 2s  .co m*/
        Cipher ch = Cipher.getInstance(algorithmProviderPadding);
        ch.init(mode, sk);
        return ch;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    return cipher.doFinal(clear);
}

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  www.  jav  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:Main.java

public static byte[] des3EncodeECB(byte[] key, byte[] data) throws Exception {

    Key deskey = null;/*from   w  w  w.j a  v  a  2 s .  c o m*/
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

    cipher.init(Cipher.ENCRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(data);

    return bOut;
}