Exemple #1
0
        /// <summary>
        /// Encrypt the plainData using the existing Content Key (CK) and return a new
        /// EncryptedContent.
        /// </summary>
        ///
        /// <param name="plainData">The data to encrypt.</param>
        /// <returns>The new EncryptedContent.</returns>
        public EncryptedContent encrypt(byte[] plainData)
        {
            // Generate the initial vector.
            byte[] initialVector = new byte[AES_IV_SIZE];
            net.named_data.jndn.util.Common.getRandom().nextBytes(initialVector);

            Cipher cipher = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5PADDING");

            try {
                cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, new SecretKeySpec(ckBits_, "AES"),
                            new IvParameterSpec(initialVector));
            } catch (InvalidKeyException ex) {
                throw new Exception(
                          "If the error is 'Illegal key size', try installing the "
                          + "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files: "
                          + ex);
            }
            byte[] encryptedData = cipher.doFinal(plainData);

            EncryptedContent content = new EncryptedContent();

            content.setInitialVector(new Blob(initialVector, false));
            content.setPayload(new Blob(encryptedData, false));
            content.setKeyLocatorName(ckName_);

            return(content);
        }