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

public static String encryption(String string) throws Exception {
    // TODO Auto-generated method stub

    //generate symmetric key
    KeyGenerator keygt = KeyGenerator.getInstance("AES");
    keygt.init(128);//from   w  w w. j a  va 2 s  .c  o m
    SecretKey symkey = keygt.generateKey();

    //get it encoded
    byte[] aes_ba = symkey.getEncoded();

    //create cipher
    SecretKeySpec skeySpec = new SecretKeySpec(aes_ba, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    //encryption
    byte[] EncSymbyteArray = cipher.doFinal(string.getBytes());

    //encrypt symKey with PublicKey
    //        Key pubKey = getPublicKey();

    Key pubKey = publicKey;

    //RSA cipher
    Cipher cipherAsm = Cipher.getInstance("RSA", "BC");
    cipherAsm.init(Cipher.ENCRYPT_MODE, pubKey);

    //RSA encryption
    byte[] asymEncsymKey = cipherAsm.doFinal(aes_ba);

    //           File f3 = new File(BASE_PATH,"enc.txt");
    //           File f3key = new File(BASE_PATH,"enckey.txt");
    //           File f3file = new File(BASE_PATH,"encfile.txt");
    //           writeToFile2(f3,f3key,f3file, asymEncsymKey, EncSymbyteArray);

    //byte != new String
    //return new String(byteMerger(asymEncsymKey, EncSymbyteArray));
    return Base64.encodeToString(byteMerger(asymEncsymKey, EncSymbyteArray), Base64.DEFAULT);

}

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

public static byte[] encrypt(PublicKey publicKey, byte[] data) {
    Assert.notNull(publicKey);/*from  w ww.  j a  v  a2 s.co  m*/
    Assert.notNull(data);
    try {
        Cipher cipher = Cipher.getInstance("RSA", PROVIDER);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.wso2telco.cryptosystem.AESencrp.java

/**
 * Decrypt./*from   w  w  w  .  j  a  v a 2s. c  o  m*/
 *
 * @param encryptedData the encrypted data
 * @return the string
 * @throws Exception the exception
 */
public static String decrypt(String encryptedData) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);
    //byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
    byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static String encryptUsingRSA(String plainText, String publicKeyContent)
        throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, IOException,
        BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
    Cipher encryptCipher = Cipher.getInstance("RSA");
    encryptCipher.init(Cipher.ENCRYPT_MODE, getRSAPublicKeyFrom(publicKeyContent));
    return Base64.getEncoder().encodeToString(encryptCipher.doFinal(plainText.getBytes(UTF_8)));
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static String decryptUsingRSA(String cipherText, String privateKeyContent)
        throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, InvalidKeySpecException,
        BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
    Cipher decryptCipher = Cipher.getInstance("RSA");
    decryptCipher.init(Cipher.DECRYPT_MODE, getRSAPrivateKeyFrom(privateKeyContent));
    return new String(decryptCipher.doFinal(Base64.getDecoder().decode(cipherText)), UTF_8);
}

From source file:com.agiletec.aps.util.DefaultApsEncrypter.java

public static String encryptString(String plainText) throws ApsSystemException {
    String encryptedString = null;
    try {//from   w w w.  ja v  a  2  s . c o  m
        Key key = getKey();
        Cipher desCipher = Cipher.getInstance(TRIPLE_DES);
        desCipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cleartext = plainText.getBytes();
        byte[] ciphertext = desCipher.doFinal(cleartext);
        encryptedString = new String(Base64.encodeBase64(ciphertext));
    } catch (Throwable t) {
        throw new ApsSystemException("Error detcted while encoding a string", t);
    }
    return encryptedString;
}

From source file:hudson.util.Protector.java

/**
 * Returns null if fails to decrypt properly.
 *///  ww w  .  j av  a2 s . com
public static String unprotect(String data) {
    if (data == null) {
        return null;
    }
    try {
        Cipher cipher = Secret.getCipher(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, DES_KEY);
        String plainText = new String(cipher.doFinal(Base64.decodeBase64(data)), "UTF-8");
        if (plainText.endsWith(MAGIC)) {
            return plainText.substring(0, plainText.length() - 3);
        }
        return null;
    } catch (GeneralSecurityException e) {
        return null;
    } catch (UnsupportedEncodingException e) {
        throw new Error(e); // impossible
    } catch (IOException e) {
        return null;
    }
}

From source file:com.wso2telco.gsma.authenticators.cryptosystem.AESencrp.java

/**
 * Encrypt.//from  w w w  . j  ava  2s . c  o  m
 *
 * @param Data the data
 * @return the string
 * @throws Exception the exception
 */
public static String encrypt(String Data) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    //String encryptedValue = new BASE64Encoder().encode(encVal);
    byte[] encryptedValue = Base64.encodeBase64(encVal);
    return new String(encryptedValue);
}

From source file:com.juicioenlinea.application.secutiry.Security.java

public static String encriptar(String texto) {

    final String secretKey = "hunter"; //llave para encriptar datos
    String encryptedString = null;

    try {/*from   w w w . ja va 2 s . c o m*/

        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("UTF-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("UTF-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        encryptedString = new String(base64Bytes);

    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }
    return encryptedString;
}

From source file:com.formatAdmin.utils.UtilsSecurity.java

public static String cifrarMD5(String texto) {
    String base64EncryptedString = "";
    try {//from w  w  w  .j av  a 2s.  co m
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(SECRET_MD5_KEY.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, ENCRYPT_ALGORITHM);
        Cipher cipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException
            | IllegalBlockSizeException | NoSuchPaddingException ex) {
    }
    return base64EncryptedString;
}