Beispiel #1
0
        /// <summary>
        /// Decrypt the encryptedData using the keyBits according the encrypt params.
        /// </summary>
        ///
        /// <param name="keyBits">The key value.</param>
        /// <param name="encryptedData">The data to decrypt.</param>
        /// <param name="params"></param>
        /// <returns>The decrypted data.</returns>
        public static Blob decrypt(Blob keyBits, Blob encryptedData,
                                   EncryptParams paras)
        {
            if (paras.getAlgorithmType() == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesEcb)
            {
                Cipher cipher = javax.crypto.Cipher.getInstance("AES/ECB/PKCS5PADDING");
                cipher.init(javax.crypto.Cipher.DECRYPT_MODE,
                            new SecretKeySpec(keyBits.getImmutableArray(), "AES"));
                return(new Blob(cipher.doFinal(encryptedData.getImmutableArray()),
                                false));
            }
            else if (paras.getAlgorithmType() == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc)
            {
                if (paras.getInitialVector().size() != BLOCK_SIZE)
                {
                    throw new Exception("incorrect initial vector size");
                }

                Cipher cipher_0 = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5PADDING");
                cipher_0.init(javax.crypto.Cipher.DECRYPT_MODE,
                              new SecretKeySpec(keyBits.getImmutableArray(), "AES"),
                              new IvParameterSpec(paras.getInitialVector()
                                                  .getImmutableArray()));
                return(new Blob(cipher_0.doFinal(encryptedData.getImmutableArray()),
                                false));
            }
            else
            {
                throw new Exception("unsupported encryption mode");
            }
        }
Beispiel #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");
            }
        }
Beispiel #3
0
        /// <summary>
        /// Decrypt the encryptedData using the keyBits according the encrypt params.
        /// </summary>
        ///
        /// <param name="keyBits">The key value.</param>
        /// <param name="encryptedData">The data to decrypt.</param>
        /// <param name="params"></param>
        /// <returns>The decrypted data.</returns>
        public static Blob decrypt(Blob keyBits, Blob encryptedData,
				EncryptParams paras)
        {
            if (paras.getAlgorithmType() == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesEcb) {
                Cipher cipher = javax.crypto.Cipher.getInstance("AES/ECB/PKCS5PADDING");
                cipher.init(javax.crypto.Cipher.DECRYPT_MODE,
                        new SecretKeySpec(keyBits.getImmutableArray(), "AES"));
                return new Blob(cipher.doFinal(encryptedData.getImmutableArray()),
                        false);
            } else if (paras.getAlgorithmType() == net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc) {
                if (paras.getInitialVector().size() != BLOCK_SIZE)
                    throw new Exception("incorrect initial vector size");

                Cipher cipher_0 = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5PADDING");
                cipher_0.init(javax.crypto.Cipher.DECRYPT_MODE,
                        new SecretKeySpec(keyBits.getImmutableArray(), "AES"),
                        new IvParameterSpec(paras.getInitialVector()
                                .getImmutableArray()));
                return new Blob(cipher_0.doFinal(encryptedData.getImmutableArray()),
                        false);
            } else
                throw new Exception("unsupported encryption mode");
        }
Beispiel #4
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");
        }