Package javax.crypto.spec

Examples of javax.crypto.spec.IvParameterSpec


  JCECipherProvider(int mode, SecretKey secretKey, byte[] iv, String algorithm, String provider)
     throws StandardException
  {
    Throwable t;
    ivspec = new IvParameterSpec(iv);
    try
    {


      if (provider == null)
View Full Code Here


            if (gcmUseIvParameterSpec) {
                // This override allows to support Java 1.7+ with (usually older versions of) third-party security
                // providers which support or even require GCM via IvParameterSpec rather than GCMParameterSpec,
                // e.g. BouncyCastle <= 1.49 (really <= 1.50 due to a semi-related bug).
                log.debug("Saw AES-GCM block cipher, using IvParameterSpec due to system property override: " + algorithm);
                return new IvParameterSpec(iv);
            }
           
            log.debug("Saw AES-GCM block cipher, attempting to create GCMParameterSpec: " + algorithm);
           
            try {
                // This class only added in Java 1.7. So load reflectively until Santuario starts targeting a minimum of Java 1.7.
                Class<?> gcmSpecClass = ClassLoaderUtils.loadClass("javax.crypto.spec.GCMParameterSpec", this.getClass());
               
                // XML Encryption 1.1 mandates a 128-bit Authentication Tag for AES GCM modes.
                AlgorithmParameterSpec gcmSpec = (AlgorithmParameterSpec) gcmSpecClass.getConstructor(int.class, byte[].class)
                        .newInstance(128, iv);
                log.debug("Successfully created GCMParameterSpec");
                return gcmSpec;
            } catch (Exception e) {
                // This handles the case of Java < 1.7 with a third-party security provider that
                // supports GCM mode using only an IvParameterSpec, such as BouncyCastle.
                log.debug("Failed to create GCMParameterSpec, falling back to returning IvParameterSpec", e);
                return new IvParameterSpec(iv);
            }
        } else {
            log.debug("Saw non-AES-GCM mode block cipher, returning IvParameterSpec: " + algorithm);
            return new IvParameterSpec(iv);
        }
    }
View Full Code Here

            // Extract encrypted data
            byte[] encdata = new byte[bytes.length - macBytes.length - iv.length];
            System.arraycopy(bytes, macBytes.length + iv.length, encdata, 0, encdata.length);

            IvParameterSpec ivspec = new IvParameterSpec(iv);
            Cipher decryptCipher = Cipher.getInstance(CIPHER_CODE);
            decryptCipher.init(Cipher.DECRYPT_MODE, sk, ivspec);

            // verify MAC by regenerating it and comparing it with the received value
            decryptMac.update(iv);
View Full Code Here

            throw new RuntimeException(e);
        }
    }

    private IvParameterSpec getIvParameterSpec(String initialVector) {
        return new IvParameterSpec(initialVector.getBytes());
    }
View Full Code Here

        String key = paddKey(encryptKey);

        try {
            byte[] keyBytes = getKeyBytes(key);
            IvParameterSpec iv = getIvParameterSpec(key);

            SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES");
            Cipher c = Cipher.getInstance(AES_CIPHER);

            c.init(Cipher.ENCRYPT_MODE, sks, iv);
View Full Code Here

    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

    cipher.init(
      Cipher.DECRYPT_MODE,
      new SecretKeySpec(key, "AES"),
      new IvParameterSpec(iv));

    return cipher.doFinal(ciphertext);
  }
View Full Code Here

            SecretKey aesKey = keygen.generateKey();
           
            // Get an IV
            ivBytes = new byte[16];
            prng.read(ivBytes);
            IvParameterSpec iv = new IvParameterSpec(ivBytes);

            // Encrypt the plaintext, then zero it out
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, aesKey, iv);
            encryptedBuf = cipher.doFinal(buf);
View Full Code Here

            byte aesKeyBytes[] = ntruKey.decrypt(wrappedKey);
            SecretKeySpec aesKey = new SecretKeySpec(aesKeyBytes, "AES");
            java.util.Arrays.fill(aesKeyBytes, (byte) 0);
           
            // Decrypt the file contents
            IvParameterSpec iv = new IvParameterSpec(ivBytes);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, aesKey, iv);
            fileContents = cipher.doFinal(encFileContents);
        } catch (java.security.GeneralSecurityException e) {
            System.out.println("AES error: " + e);
View Full Code Here

            Cipher cipher;
            try
            {
                cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                SecretKeySpec keySpec = new SecretKeySpec(encryptionKey, "AES");
                IvParameterSpec ivSpec = new IvParameterSpec(iv);
                cipher.init(decrypt ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, keySpec, ivSpec);

            }
            catch (GeneralSecurityException e)
            {
                throw new IOException(e);
            }

            CipherInputStream cis = new CipherInputStream(data, cipher);
            try
            {
                IOUtils.copy(cis, output);
            }
            finally
            {
                cis.close();
            }
        }
        else
        {
            if (useAES && !decrypt)
            {
                throw new IllegalArgumentException("AES encryption with key length other than 256 bits is not yet implemented.");
            }

            byte[] newKey = new byte[encryptionKey.length + 5];
            System.arraycopy(encryptionKey, 0, newKey, 0, encryptionKey.length);
            // PDF 1.4 reference pg 73
            // step 1
            // we have the reference

            // step 2
            newKey[newKey.length - 5] = (byte) (objectNumber & 0xff);
            newKey[newKey.length - 4] = (byte) (objectNumber >> 8 & 0xff);
            newKey[newKey.length - 3] = (byte) (objectNumber >> 16 & 0xff);
            newKey[newKey.length - 2] = (byte) (genNumber & 0xff);
            newKey[newKey.length - 1] = (byte) (genNumber >> 8 & 0xff);

            // step 3
            MessageDigest md = MessageDigests.getMD5();
            md.update(newKey);
            if (useAES)
            {
                md.update(AES_SALT);
            }
            byte[] digestedKey = md.digest();

            // step 4
            int length = Math.min(newKey.length, 16);
            byte[] finalKey = new byte[length];
            System.arraycopy(digestedKey, 0, finalKey, 0, length);

            if (useAES)
            {
                byte[] iv = new byte[16];

                data.read(iv);

                try
                {
                    Cipher decryptCipher;
                    try
                    {
                        decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                    }
                    catch (NoSuchAlgorithmException e)
                    {
                        // should never happen
                        throw new RuntimeException(e);
                    }

                    SecretKey aesKey = new SecretKeySpec(finalKey, "AES");
                    IvParameterSpec ips = new IvParameterSpec(iv);
                    decryptCipher.init(decrypt ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, aesKey, ips);
                    CipherInputStream cipherStream = new CipherInputStream(data, decryptCipher);

                    try
                    {
View Full Code Here

               
                // Algorithm 8b: Compute UE
                byte[] hashUE = computeHash2B(concat(userPasswordBytes, userKeySalt),
                        userPasswordBytes, null);
                cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(hashUE, "AES"),
                        new IvParameterSpec(new byte[16]));
                byte[] ue = cipher.doFinal(encryptionKey);
               
                // Algorithm 9a: Compute O
                byte[] ownerPasswordBytes = truncate127(ownerPassword.getBytes("UTF-8"));
                byte[] ownerValidationSalt = new byte[8];
                byte[] ownerKeySalt = new byte[8];
                rnd.nextBytes(ownerValidationSalt);
                rnd.nextBytes(ownerKeySalt);
                byte[] hashO = computeHash2B(concat(ownerPasswordBytes, ownerValidationSalt, u),
                        ownerPasswordBytes, u);
                byte[] o = concat(hashO, ownerValidationSalt, ownerKeySalt);
               
                // Algorithm 9b: Compute OE
                byte[] hashOE = computeHash2B(concat(ownerPasswordBytes, ownerKeySalt, u),
                        ownerPasswordBytes, u);
                cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(hashOE, "AES"),
                        new IvParameterSpec(new byte[16]));
                byte[] oe = cipher.doFinal(encryptionKey);
               
                // Set keys and other required constants in encryption dictionary
                encryptionDictionary.setUserKey(u);
                encryptionDictionary.setUserEncryptionKey(ue);
                encryptionDictionary.setOwnerKey(o);
                encryptionDictionary.setOwnerEncryptionKey(oe);
               
                PDCryptFilterDictionary cryptFilterDictionary = new PDCryptFilterDictionary();
                cryptFilterDictionary.setCryptFilterMethod(COSName.AESV3);
                cryptFilterDictionary.setLength(keyLength);
                encryptionDictionary.setStdCryptFilterDictionary(cryptFilterDictionary);
                encryptionDictionary.setStreamFilterName(COSName.STD_CF);
                encryptionDictionary.setStringFilterName(COSName.STD_CF);
                setAES(true);
               
                // Algorithm 10: compute "Perms" value
                byte[] perms = new byte[16];
                perms[0] = (byte)permissionInt;
                perms[1] = (byte)(permissionInt >>> 8);
                perms[2] = (byte)(permissionInt >>> 16);
                perms[3] = (byte)(permissionInt >>> 24);
                perms[4] = perms[5] = perms[6] = perms[7] = (byte)0xFF;
                perms[8] = 'T';    // we always use EncryptMetadata == true
                perms[9] = 'a';
                perms[10] = 'd';
                perms[11] = 'b';
                for (int i = 12; i <= 15; i++)
                {
                    perms[i] = (byte)rnd.nextInt();
                }
               
                cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptionKey, "AES"),
                        new IvParameterSpec(new byte[16]));

                byte[] permsEnc = cipher.doFinal(perms);
               
                encryptionDictionary.setPerms(permsEnc);
            }
View Full Code Here

TOP

Related Classes of javax.crypto.spec.IvParameterSpec

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.