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);
        }
Exemple #2
0
        /// <summary>
        /// Encrypt the payload using the symmetric key according to params, and return
        /// an EncryptedContent.
        /// </summary>
        ///
        /// <param name="payload">The data to encrypt.</param>
        /// <param name="key">The key value.</param>
        /// <param name="keyName">The key name for the EncryptedContent key locator.</param>
        /// <param name="params">The parameters for encryption.</param>
        /// <returns>A new EncryptedContent.</returns>
        private static EncryptedContent encryptSymmetric(Blob payload, Blob key,
				Name keyName, EncryptParams paras)
        {
            EncryptAlgorithmType algorithmType = paras.getAlgorithmType();
            Blob initialVector = paras.getInitialVector();
            KeyLocator keyLocator = new KeyLocator();
            keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEYNAME);
            keyLocator.setKeyName(keyName);

            if (algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc
                    || algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesEcb) {
                if (algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc) {
                    if (initialVector.size() != net.named_data.jndn.encrypt.algo.AesAlgorithm.BLOCK_SIZE)
                        throw new Exception("incorrect initial vector size");
                }

                Blob encryptedPayload = net.named_data.jndn.encrypt.algo.AesAlgorithm.encrypt(key, payload, paras);

                EncryptedContent result = new EncryptedContent();
                result.setAlgorithmType(algorithmType);
                result.setKeyLocator(keyLocator);
                result.setPayload(encryptedPayload);
                result.setInitialVector(initialVector);
                return result;
            } else
                throw new Exception("Unsupported encryption method");
        }
        /// <summary>
        /// Decode input as a EncryptedContent in NDN-TLV and set the fields of the
        /// encryptedContent object.
        /// </summary>
        ///
        /// <param name="encryptedContent"></param>
        /// <param name="input"></param>
        /// <param name="copy">unchanged while the Blob values are used.</param>
        /// <exception cref="EncodingException">For invalid encoding</exception>
        public override void decodeEncryptedContent(EncryptedContent encryptedContent,
				ByteBuffer input, bool copy)
        {
            TlvDecoder decoder = new TlvDecoder(input);
            int endOffset = decoder
                    .readNestedTlvsStart(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EncryptedContent);

            Tlv0_2WireFormat.decodeKeyLocator(net.named_data.jndn.encoding.tlv.Tlv.KeyLocator,
                    encryptedContent.getKeyLocator(), decoder, copy);

            int algorithmType = (int) decoder
                    .readNonNegativeIntegerTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EncryptionAlgorithm);
            if (algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesEcb.getNumericType())
                encryptedContent.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesEcb);
            else if (algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc.getNumericType())
                encryptedContent.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc);
            else if (algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaPkcs.getNumericType())
                encryptedContent.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaPkcs);
            else if (algorithmType == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep.getNumericType())
                encryptedContent.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep);
            else
                throw new EncodingException(
                        "Unrecognized EncryptionAlgorithm code " + algorithmType);

            encryptedContent.setInitialVector(new Blob(decoder.readOptionalBlobTlv(
                    net.named_data.jndn.encoding.tlv.Tlv.Encrypt_InitialVector, endOffset), copy));
            encryptedContent.setPayload(new Blob(decoder
                    .readBlobTlv(net.named_data.jndn.encoding.tlv.Tlv.Encrypt_EncryptedPayload), copy));

            decoder.finishNestedTlvs(endOffset);
        }
        public void testSetterGetter()
        {
            EncryptedContent content = new EncryptedContent();
            Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.NONE, content.getAlgorithmType());
            Assert.AssertEquals(true, content.getPayload().isNull());
            Assert.AssertEquals(true, content.getInitialVector().isNull());
            Assert.AssertEquals(net.named_data.jndn.KeyLocatorType.NONE, content.getKeyLocator().getType());

            content.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep);
            Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, content.getAlgorithmType());
            Assert.AssertEquals(true, content.getPayload().isNull());
            Assert.AssertEquals(true, content.getInitialVector().isNull());
            Assert.AssertEquals(net.named_data.jndn.KeyLocatorType.NONE, content.getKeyLocator().getType());

            KeyLocator keyLocator = new KeyLocator();
            keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEYNAME);
            keyLocator.getKeyName().set("/test/key/locator");
            content.setKeyLocator(keyLocator);
            Assert.AssertTrue(content.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE);
            Assert.AssertTrue(content.getKeyLocator().getKeyName()
                    .equals(new Name("/test/key/locator")));
            Assert.AssertEquals(true, content.getPayload().isNull());
            Assert.AssertEquals(true, content.getInitialVector().isNull());

            content.setPayload(new Blob(message, false));
            Assert.AssertTrue(content.getPayload().equals(new Blob(message, false)));

            content.setInitialVector(new Blob(iv, false));
            Assert.AssertTrue(content.getInitialVector().equals(new Blob(iv, false)));

            Blob encoded = content.wireEncode();
            Blob contentBlob = new Blob(encrypted, false);
            Assert.AssertTrue(contentBlob.equals(encoded));
        }