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:io.zipi.common.util.AesEncrypter.java

/**
 * Decrypt.//from   w  w w  .jav a2  s. co  m
 * @param encryptedText the encrypted text
 * @param secretKey the secret key
 * @return the string
 */
public static final String decrypt(String secretKey, String encryptedText) {
    if (encryptedText == null) {
        return null;
    }
    if (encryptedText.startsWith("$CRYPT::")) { //$NON-NLS-1$
        byte[] decoded = Base64.decodeBase64(encryptedText.substring(8));
        Cipher cipher;
        try {
            SecretKeySpec skeySpec = keySpecFromSecretKey(secretKey);

            cipher = Cipher.getInstance("AES"); //$NON-NLS-1$
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
            throw new RuntimeException(e);
        }
        try {
            return new String(cipher.doFinal(decoded));
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            throw new RuntimeException(e);
        }
    } else {
        return encryptedText;
    }
}

From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java

/**
 * ????//  w w w.j a  va 2s  .  co m
 * <dl>
 * <dt>?
 * <dd>RSA/ECB/PKCS1Padding???????
 * </dl>
 * @param key ?
 * @param input 
 * @return ?
 */
public static byte[] encrypt(final Key key, final byte[] input) {
    try {
        final Cipher cipher = Cipher.getInstance(ALGO_CIPHER_SMALL);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(input);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | IllegalBlockSizeException | BadPaddingException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java

/**
 * ????/*from   w ww . j a  v  a2 s .c om*/
 * <dl>
 * <dt>?
 * <dd>RSA/ECB/PKCS1Padding???????
 * </dl>
 * @param key ?
 * @param input 
 * @return ?
 */
public static byte[] decrypt(final Key key, final byte[] input) {
    try {
        final Cipher cipher = Cipher.getInstance(ALGO_CIPHER_SMALL);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(input);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | IllegalBlockSizeException | BadPaddingException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:com.security.ch08_rsa.RSACoderTextKey.java

/**
 * /*from   w w w.j a v  a  2  s . c o m*/
 *
 * @param data
 *            ?
 * @param key
 *            
 * @return byte[] ?
 * @throws Exception
 */
private static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception {

    // ?
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);

    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);

    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

    cipher.init(Cipher.ENCRYPT_MODE, publicKey);

    return cipher.doFinal(data);
}

From source file:com.sammyun.util.RSAUtils.java

/**
 * //  ww w.  j a v a  2s  .  c  o m
 * 
 * @param content 
 * @param private_key ?
 * @param input_charset ??
 * @return ?
 */
public static String decrypt(String content, String private_key, String input_charset) throws Exception {
    PrivateKey prikey = getPrivateKey(private_key);
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, prikey);
    InputStream ins = new ByteArrayInputStream(Base64Util.decode(content));
    ByteArrayOutputStream writer = new ByteArrayOutputStream();
    // rsa?128?128?
    byte[] buf = new byte[128];
    int bufl;

    while ((bufl = ins.read(buf)) != -1) {
        byte[] block = null;

        if (buf.length == bufl) {
            block = buf;
        } else {
            block = new byte[bufl];
            for (int i = 0; i < bufl; i++) {
                block[i] = buf[i];
            }
        }

        writer.write(cipher.doFinal(block));
    }

    return new String(writer.toByteArray(), input_charset);
}

From source file:com.security.ch08_rsa.RSACoderTextKey.java

/**
 * ?//www.  j  a v a 2s.c om
 *
 * @param data
 *            ?
 * @param key
 *            ?
 * @return byte[] ?
 * @throws Exception
 */
private static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception {

    // ??
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);

    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    // ??
    PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    return cipher.doFinal(data);
}

From source file:com.security.ch08_rsa.RSACoderTextKey.java

/**
 * // w ww .j av a 2s. co m
 *
 * @param data
 *            ?
 * @param key
 *            
 * @return byte[] ?
 * @throws Exception
 */
private static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception {

    // ?
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);

    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    // ?
    PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);

    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

    cipher.init(Cipher.DECRYPT_MODE, publicKey);

    return cipher.doFinal(data);
}

From source file:com.security.ch08_rsa.RSACoderTextKey.java

/**
 * ?/*from  ww w  . j a v a  2 s.co m*/
 *
 * @param data
 *            ?
 * @param key
 *            ?
 * @return byte[] ?
 * @throws Exception
 */
private static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {

    // ??
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);

    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    // ??
    PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

    cipher.init(Cipher.ENCRYPT_MODE, privateKey);

    return cipher.doFinal(data);
}

From source file:de.extra.client.plugins.outputplugin.crypto.ExtraCryptoUtil.java

/** Encrypts the specified string, using the specified secret key. */
private static String encrypt(String sValue, String secretKey) {
    if (secretKey == null) {
        secretKey = SYM_KEY_STR;//from   w  ww  .  j  a v a 2 s.c o  m
    }

    if (sValue == null || sValue.equals("")) {
        return "";
    }

    String textEncode = null;
    Cipher encryptCipher = null;

    try {
        SecretKeySpec skeySpec = decodeKey(secretKey);
        encryptCipher = Cipher.getInstance(TRANSFORMATION);
        encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        byte[] plainText = sValue.trim().getBytes(CHARSET);

        // do the actual encryption
        byte[] cipherText = encryptCipher.doFinal(plainText);

        // Changed to encode() to avoid <cr> on end of string
        // textEncode = base64Encoder.encodeBuffer(cipherText);
        textEncode = new Base64().encodeAsString(cipherText);

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

    return textEncode;
}

From source file:io.apiman.common.util.AesEncrypter.java

/**
 * Decrypt.//  www  .j a va  2 s  . com
 * @param encryptedText the encrypted text
 * @param secretKey the secret key
 * @return the string
 */
public static final String decrypt(String secretKey, String encryptedText) {
    if (encryptedText == null) {
        return null;
    }
    if (encryptedText.startsWith("$CRYPT::")) { //$NON-NLS-1$
        byte[] decoded = Base64.decodeBase64(encryptedText.substring(8));
        Cipher cipher;
        try {
            SecretKeySpec skeySpec = keySpecFromSecretKey(secretKey);

            cipher = Cipher.getInstance("AES"); //$NON-NLS-1$
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
        try {
            String decryptedString = new String(cipher.doFinal(decoded));
            return decryptedString;
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }
    } else {
        return encryptedText;
    }
}