Search in sources :

Example 81 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project robovm by robovm.

the class SealedObjectTest method testSealedObject1.

/**
     * SealedObject(Serializable object, Cipher c) method testing. Tests if the
     * NullPointerException is thrown in the case of null cipher.
     */
public void testSealedObject1() throws Exception {
    String secret = "secret string";
    try {
        new SealedObject(secret, null);
        fail("NullPointerException should be thrown in the case " + "of null cipher.");
    } catch (NullPointerException e) {
    }
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key key = kg.generateKey();
    IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, ips);
    SealedObject so = new SealedObject(secret, cipher);
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, key, ips);
    try {
        new SealedObject(secret, cipher);
        fail("IllegalBlockSizeException expected");
    } catch (IllegalBlockSizeException e) {
    //expected
    }
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) SealedObject(javax.crypto.SealedObject) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 82 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project robovm by robovm.

the class IvParameterSpecTest method testGetIV.

public void testGetIV() {
    byte[] iv = new byte[] { 1, 2, 3, 4, 5 };
    IvParameterSpec ivps = new IvParameterSpec(iv);
    iv = ivps.getIV();
    iv[0]++;
    assertFalse("The change of returned array should not cause " + "the change of internal array", iv[0] == ivps.getIV()[0]);
}
Also used : IvParameterSpec(javax.crypto.spec.IvParameterSpec)

Example 83 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project JustAndroid by chinaltz.

the class AbDes method encrypt.

public String encrypt(byte[] encryptByte, String encryptKey) {
    try {
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
        SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
        byte[] encryptedData = cipher.doFinal(encryptByte);
        return AbBase64.encode(encryptedData);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 84 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project jdk8u_jdk by JetBrains.

the class Des method des_cksum.

/**
     * Encrypts the message blocks using DES CBC and output the
     * final block of 8-byte ciphertext.
     * @param ivec Initialization vector.
     * @param msg Input message as an byte array.
     * @param key DES key to encrypt the message.
     * @return the last block of ciphertext.
     *
     * @created by Yanni Zhang, Dec 6, 99.
     */
public static byte[] des_cksum(byte[] ivec, byte[] msg, byte[] key) throws KrbCryptoException {
    Cipher cipher = null;
    byte[] result = new byte[8];
    try {
        cipher = Cipher.getInstance("DES/CBC/NoPadding");
    } catch (Exception e) {
        KrbCryptoException ke = new KrbCryptoException("JCE provider may not be installed. " + e.getMessage());
        ke.initCause(e);
        throw ke;
    }
    IvParameterSpec params = new IvParameterSpec(ivec);
    SecretKeySpec skSpec = new SecretKeySpec(key, "DES");
    try {
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        // SecretKey sk = skf.generateSecret(skSpec);
        SecretKey sk = (SecretKey) skSpec;
        cipher.init(Cipher.ENCRYPT_MODE, sk, params);
        for (int i = 0; i < msg.length / 8; i++) {
            result = cipher.doFinal(msg, i * 8, 8);
            cipher.init(Cipher.ENCRYPT_MODE, sk, (new IvParameterSpec(result)));
        }
    } catch (GeneralSecurityException e) {
        KrbCryptoException ke = new KrbCryptoException(e.getMessage());
        ke.initCause(e);
        throw ke;
    }
    return result;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) KrbCryptoException(sun.security.krb5.KrbCryptoException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) GeneralSecurityException(java.security.GeneralSecurityException) KrbCryptoException(sun.security.krb5.KrbCryptoException)

Example 85 with IvParameterSpec

use of javax.crypto.spec.IvParameterSpec in project jdk8u_jdk by JetBrains.

the class AesDkCrypto method encryptCTS.

/**
     * Encrypt AES in CBC-CTS mode using derived keys.
     */
private byte[] encryptCTS(byte[] baseKey, int usage, byte[] ivec, byte[] new_ivec, byte[] plaintext, int start, int len, boolean confounder_exists) throws GeneralSecurityException, KrbCryptoException {
    byte[] Ke = null;
    byte[] Ki = null;
    if (debug) {
        System.err.println("usage: " + usage);
        if (ivec != null) {
            traceOutput("old_state.ivec", ivec, 0, ivec.length);
        }
        traceOutput("plaintext", plaintext, start, Math.min(len, 32));
        traceOutput("baseKey", baseKey, 0, baseKey.length);
    }
    try {
        // derive Encryption key
        byte[] constant = new byte[5];
        constant[0] = (byte) ((usage >> 24) & 0xff);
        constant[1] = (byte) ((usage >> 16) & 0xff);
        constant[2] = (byte) ((usage >> 8) & 0xff);
        constant[3] = (byte) (usage & 0xff);
        constant[4] = (byte) 0xaa;
        // Encryption key
        Ke = dk(baseKey, constant);
        byte[] toBeEncrypted = null;
        if (confounder_exists) {
            byte[] confounder = Confounder.bytes(BLOCK_SIZE);
            toBeEncrypted = new byte[confounder.length + len];
            System.arraycopy(confounder, 0, toBeEncrypted, 0, confounder.length);
            System.arraycopy(plaintext, start, toBeEncrypted, confounder.length, len);
        } else {
            toBeEncrypted = new byte[len];
            System.arraycopy(plaintext, start, toBeEncrypted, 0, len);
        }
        // encryptedData + HMAC
        byte[] output = new byte[toBeEncrypted.length + hashSize];
        // AES in JCE
        Cipher cipher = Cipher.getInstance("AES/CTS/NoPadding");
        SecretKeySpec secretKey = new SecretKeySpec(Ke, "AES");
        IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, encIv);
        cipher.doFinal(toBeEncrypted, 0, toBeEncrypted.length, output);
        // Derive integrity key
        constant[4] = (byte) 0x55;
        Ki = dk(baseKey, constant);
        if (debug) {
            traceOutput("constant", constant, 0, constant.length);
            traceOutput("Ki", Ki, 0, Ke.length);
        }
        // Generate checksum
        // H1 = HMAC(Ki, conf | plaintext | pad)
        byte[] hmac = getHmac(Ki, toBeEncrypted);
        // encryptedData + HMAC
        System.arraycopy(hmac, 0, output, toBeEncrypted.length, hmac.length);
        return output;
    } finally {
        if (Ke != null) {
            Arrays.fill(Ke, 0, Ke.length, (byte) 0);
        }
        if (Ki != null) {
            Arrays.fill(Ki, 0, Ki.length, (byte) 0);
        }
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Aggregations

IvParameterSpec (javax.crypto.spec.IvParameterSpec)229 Cipher (javax.crypto.Cipher)150 SecretKeySpec (javax.crypto.spec.SecretKeySpec)107 SecretKey (javax.crypto.SecretKey)49 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)46 InvalidKeyException (java.security.InvalidKeyException)43 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)42 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)39 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 BadPaddingException (javax.crypto.BadPaddingException)28 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)25 Key (java.security.Key)21 KeyGenerator (javax.crypto.KeyGenerator)21 IOException (java.io.IOException)19 SecureRandom (java.security.SecureRandom)17 GeneralSecurityException (java.security.GeneralSecurityException)15 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)15 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)14 MessageDigest (java.security.MessageDigest)13 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)13