Example usage for javax.crypto Cipher ENCRYPT_MODE

List of usage examples for javax.crypto Cipher ENCRYPT_MODE

Introduction

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

Prototype

int ENCRYPT_MODE

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

Click Source Link

Document

Constant used to initialize cipher to encryption mode.

Usage

From source file:cloudeventbus.pki.CertificateUtils.java

public static Certificate signCertificate(Certificate issuer, PrivateKey issuerPrivateKey,
        Certificate certificate) {
    if (issuer.getSerialNumber() != certificate.getIssuer()) {
        throw new CertificateIssuerMismatchException(
                "The authority certificate serial number doesn't much the certificate issuer.");
    }//  w  ww .ja  v a  2  s.  co m
    validatePermissions(issuer, certificate);
    final byte[] hash = certificate.hash();

    try {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, issuerPrivateKey);
        final byte[] signature = cipher.doFinal(hash);
        return new Certificate(certificate.getType(), certificate.getSerialNumber(), certificate.getIssuer(),
                certificate.getExpirationDate(), certificate.getPublicKey(),
                certificate.getSubscribePermissions(), certificate.getPublishPermissions(),
                certificate.getComment(), signature);
    } catch (GeneralSecurityException e) {
        throw new CertificateSecurityException(e);
    }
}

From source file:com.shenit.commons.codec.DesUtils.java

/**
 * /*from w w  w. j a  v  a2s . c o  m*/
 * @param rawData
 * @param rawKey
 * @return
 */
public static byte[] encrypt(byte[] rawData, KeySpec key) {
    return crypt(rawData, key, Cipher.ENCRYPT_MODE);
}

From source file:com.netcrest.pado.internal.security.AESCipher.java

private static byte[] encryptBinaryToBinary(byte[] privateKeyEncrypted, byte[] clearBinary) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(privateKeyEncrypted, "AES");

    // Instantiate the cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clearBinary);
    return encrypted;
}

From source file:com.glaf.core.security.SecurityUtils.java

/**
 * DES/*from   w  w  w  .  j  a  va 2s  . c o  m*/
 * 
 * @param data
 *            
 * @param key
 *            ???8?
 * @return ?
 */
public static String encode(String key, String data) {
    if (data == null) {
        return null;
    }
    try {
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // key??8?
        Key secretKey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
        AlgorithmParameterSpec paramSpec = iv;
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        byte[] bytes = cipher.doFinal(data.getBytes());
        return byte2hex(bytes);
    } catch (Exception ex) {
        throw new SecurityException(ex);
    }
}

From source file:net.thewaffleshop.nimbus.api.EncryptionAPI.java

/**
 * Encrypt//www.  j  a  v  a  2s . co m
 *
 * @param secretKey
 * @param iv
 * @param secret
 * @return
 */
public byte[] encrypt(SecretKey secretKey, byte[] iv, byte[] secret) {
    try {
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
        byte[] ret = cipher.doFinal(secret);
        return ret;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.springcryptoutils.core.cipher.asymmetric.Base64EncodedCiphererWithChooserByKeyIdImpl.java

/**
 * Encrypts/decrypts a message based on the underlying mode of operation.
 *
 * @param keyId the key id/*from www.  j ava2  s  .  co  m*/
 * @param message if in encryption mode, the clear-text message, otherwise
 *        the base64 encoded message to decrypt
 * @return if in encryption mode, the base64 encoded encrypted message,
 *         otherwise the decrypted message
 * @throws AsymmetricEncryptionException on runtime errors
 * @see #setMode(Mode)
 */
public String encrypt(String keyId, String message) {
    final Key key = keyMap.get(keyId);

    if (key == null) {
        throw new AsymmetricEncryptionException("key not found: keyId=" + keyId);
    }

    try {
        final Cipher cipher = (((provider == null) || (provider.length() == 0)) ? Cipher.getInstance(algorithm)
                : Cipher.getInstance(algorithm, provider));
        switch (mode) {
        case ENCRYPT:
            final byte[] messageAsByteArray = message.getBytes(charsetName);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            return Base64.encodeBase64String(cipher.doFinal(messageAsByteArray));
        case DECRYPT:
            final byte[] encryptedMessage = Base64.decodeBase64(message);
            cipher.init(Cipher.DECRYPT_MODE, key);
            return new String(cipher.doFinal(encryptedMessage), charsetName);
        default:
            return null;
        }
    } catch (Exception e) {
        throw new AsymmetricEncryptionException("error encrypting/decrypting message; mode=" + mode, e);
    }
}

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

/**
 * //from  w w  w  .  ja  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:edu.cmu.sei.ams.cloudlet.impl.AESEncrypter.java

/**
 * Encrypts data and returns the encrypted string.
 * @param clear A byte array to encrypt.
 * @return An encrypted string./*  w ww  . j av  a 2  s .com*/
 * @throws EncryptionException
 */
public String encrypt(byte[] clear) throws EncryptionException {
    try {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        byte[] b = new byte[16];
        random.nextBytes(b);
        byte[] iv = b;
        //log.info("IV: " + String.valueOf(Hex.encodeHex(iv)));

        // TODO: change to CBC method with padding.
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, this.skeySpec, new IvParameterSpec(iv));

        byte[] encrypted = cipher.doFinal(clear);
        //log.info("Cipher Text: " + String.valueOf(Hex.encodeHex(encrypted)));
        String encryptedString = new String(Base64.encodeBase64(ivCipherConcat(iv, encrypted)));
        return encryptedString;
    } catch (Exception e) {
        throw new EncryptionException("Error encrypting information", e);
    }
}

From source file:my.adam.smo.common.AsymmetricEncryptionBox.java

public byte[] encrypt(PublicKey pubKey, byte[] plaintext) {
    try {// ww  w.ja  v a 2s  . com
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        return cipher.doFinal(plaintext);
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:com.servoy.j2db.util.SecuritySupport.java

@SuppressWarnings("nls")
public static String encrypt(Settings settings, String value) throws Exception {
    if (value == null)
        return value;
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.ENCRYPT_MODE, SecuritySupport.getCryptKey(settings));
    return Utils.encodeBASE64(cipher.doFinal(value.getBytes()));
}