Example usage for javax.crypto Cipher update

List of usage examples for javax.crypto Cipher update

Introduction

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

Prototype

public final byte[] update(byte[] input) 

Source Link

Document

Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

Usage

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * //from   w w  w.  java  2  s  .co  m
 * 
 * @param target
 * @throws Exception
 */
static void decryptionByPublicKey(String target) throws Exception {
    PublicKey publicKey = getPublicKey();
    Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, publicKey);
    cipher.update(decodeBase64(target));
    String source = new String(cipher.doFinal(), "UTF-8");
    System.out.println("??\r\n" + source);
}

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * /*from  w  ww. j a va2 s  .  c o  m*/
 * 
 * @param data
 * @return
 * @throws Exception
 */
static String encryptionByPublicKey(String source) throws Exception {
    PublicKey publicKey = getPublicKey();
    Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    cipher.update(source.getBytes("UTF-8"));
    String target = encodeBase64(cipher.doFinal());
    System.out.println("??\r\n" + target);
    return target;
}

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * ?//from   w  w w.  j a  v a  2 s .  c om
 * 
 * @param target
 * @throws Exception
 */
static void decryptionByPrivateKey(String target) throws Exception {
    PrivateKey privateKey = getPrivateKey();
    Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    cipher.update(decodeBase64(target));
    String source = new String(cipher.doFinal(), "UTF-8");
    System.out.println("???\r\n" + source);
}

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * ?//from  w  ww.j a  va2 s  . com
 * 
 * @param data
 * @return
 * @throws Exception
 */
static String encryptionByPrivateKey(String source) throws Exception {
    PrivateKey privateKey = getPrivateKey();
    Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    cipher.update(source.getBytes("UTF-8"));
    String target = encodeBase64(cipher.doFinal());
    System.out.println("???\r\n" + target);
    return target;
}

From source file:cloudeventbus.pki.CertificateUtils.java

public static byte[] signChallenge(PrivateKey key, byte[] challenge, byte[] salt) {
    try {/* ww w .j av  a  2  s .  com*/
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        cipher.update(challenge);
        cipher.update(salt);
        return cipher.doFinal();
    } catch (GeneralSecurityException e) {
        throw new CertificateSecurityException(e);
    }
}

From source file:im.whistle.crypt.Crypt.java

/**
 * Encrypts a message./*www.  j av  a 2s.  c  om*/
 * @param args Arguments: data, publicKey[, privateKey]
 * @param callback Callback
 */
public static void encrypt(JSONArray args, AsyncCallback<JSONArray> callback) {
    try {
        PRNGProvider.init(); // Ensure OpenSSL fix

        // Get the arguments
        String data = args.getString(0);
        String pub = args.getString(1);
        String priv = null;
        if (args.length() == 3) {
            priv = args.getString(2);
        }
        String sig = null;

        // Convert everything into byte arrays
        byte[] dataRaw = data.getBytes("utf-8");
        byte[] pubRaw = Base64.decode(stripKey(pub), Base64.DEFAULT);

        // Generate random AES key and IV
        byte[] aesKey = new byte[AES_BYTES];
        new SecureRandom().nextBytes(aesKey);
        byte[] aesIv = new byte[16]; // Block size
        new SecureRandom().nextBytes(aesIv);
        Cipher c = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
        c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(aesKey, "AES"), new IvParameterSpec(aesIv));

        // Encrypt data with AES
        byte[] encData = c.doFinal(dataRaw);

        // Encrypt aes data with RSA
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubRaw);
        KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
        c = Cipher.getInstance("RSA/None/OAEPWithSHA-1AndMGF1Padding", "BC");
        c.init(Cipher.ENCRYPT_MODE, kf.generatePublic(publicKeySpec));
        c.update(aesKey);
        c.update(aesIv);
        byte[] encKey = c.doFinal();

        // Concatenate and transform
        byte[] encRaw = new byte[encKey.length + encData.length];
        System.arraycopy(encKey, 0, encRaw, 0, encKey.length);
        System.arraycopy(encData, 0, encRaw, encKey.length, encData.length);
        encKey = null;
        encData = null;
        String enc = new String(Base64.encode(encRaw /* needed for sign */, Base64.NO_WRAP), "utf-8");

        // Sign
        if (priv != null) {
            // Fail on error (no try-catch)
            byte[] privRaw = Base64.decode(stripKey(priv), Base64.DEFAULT);
            PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privRaw);
            Signature s = Signature.getInstance("SHA1withRSA", "BC");
            s.initSign(kf.generatePrivate(privateKeySpec));
            s.update(encRaw);
            sig = new String(Base64.encode(s.sign(), Base64.NO_WRAP), "utf-8");
        }

        JSONArray res = new JSONArray();
        res.put(enc);
        res.put(sig);
        callback.success(res);
    } catch (Exception ex) {
        Log.w("whistle", "Encrypt error: " + ex.getMessage(), ex);
        callback.error(ex);
    }
}

From source file:rotmg.net.RotmgNetworkHandler.java

private byte[] decrypt(byte[] bytes, Cipher key) throws IOException {
    return key.update(bytes);
}

From source file:info.fcrp.keepitsafe.bean.CryptBeanTest.java

@Test
public void symetric() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    KeyGenerator kg = KeyGenerator.getInstance("AES");
    kg.init(256, new SecureRandom());
    Key k = kg.generateKey();//from  w w  w.j  ava2s. c  o m

    Cipher c = Cipher.getInstance("AES");
    String plain = "plain";
    byte[] plainBytes = plain.getBytes();

    c.init(Cipher.ENCRYPT_MODE, k);
    c.update(plainBytes);

    byte[] encBytes = c.doFinal();
    String enc = Base64.encodeBase64String(encBytes);
    assertNotSame(plain, enc);

    c.init(Cipher.DECRYPT_MODE, k);
    c.update(encBytes);
    byte[] decBytes = c.doFinal();
    String dec = new String(decBytes);

    assertEquals(plain, dec);
}

From source file:info.fcrp.keepitsafe.bean.CryptBeanTest.java

@Test
public void assymetric() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(1024, new SecureRandom());
    KeyPair kp = kpg.generateKeyPair();
    PrivateKey priKey = kp.getPrivate();
    PublicKey pubKey = kp.getPublic();

    Cipher c = Cipher.getInstance("RSA");
    String plain = "plain";
    byte[] plainBytes = plain.getBytes();

    c.init(Cipher.ENCRYPT_MODE, pubKey);
    c.update(plainBytes);

    byte[] encBytes = c.doFinal();
    String enc = Base64.encodeBase64String(encBytes);
    assertNotSame(plain, enc);/*from   w w  w . j a v a 2  s .  co  m*/

    c.init(Cipher.DECRYPT_MODE, priKey);
    c.update(encBytes);
    byte[] decBytes = c.doFinal();
    String dec = new String(decBytes);

    assertEquals(plain, dec);
}

From source file:com.joyent.manta.client.crypto.AesCtrCipherDetails.java

@Override
public long updateCipherToPosition(final Cipher cipher, final long position) {
    final int blockSize = getBlockSizeInBytes();
    final long block = position / blockSize;
    final long skip = (position % blockSize);

    byte[] throwaway = new byte[blockSize];
    for (long i = 0; i < block; i++) {
        cipher.update(throwaway);
    }//from www  .j  a  v a2  s.c o m

    return skip;
}