Package javax.crypto

Examples of javax.crypto.Cipher$Transform


    Map  secure_payload = new HashMap();
   
    try{
        byte[]  message_bytes = StaticUtilities.getFormatters().bEncode( plain_payload );
       
      Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
     
      cipher.init(Cipher.ENCRYPT_MODE, session_key );
 
      byte[]  encrypted_message = cipher.doFinal( message_bytes );
 
      secure_payload.put( "ver", "1" );
      secure_payload.put( "alg", "DESede" );
      secure_payload.put( "key", encryped_session_key );
      secure_payload.put( "content", encrypted_message );
View Full Code Here


    Map  secure_payload = delegate.receiveMessage();
   
    byte[]  encrypted_message  = (byte[])secure_payload.get( "content" );
   
    try{
      Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
     
      cipher.init(Cipher.DECRYPT_MODE, session_key );
 
      byte[]  message_bytes = cipher.doFinal( encrypted_message );
 
      Map plain_payload = StaticUtilities.getFormatters().bDecode( message_bytes );

      return( plain_payload );
     
View Full Code Here

    if (logger.isLoggable(java.util.logging.Level.FINE))                                     logger.log(java.util.logging.Level.FINE, "Serialized octets:\n" + serializedOctets);

        byte[] encryptedBytes = null;

    // Now create the working cipher if none was created already
    Cipher c;
    if (_contextCipher == null) {
      String jceAlgorithm =
        JCEMapper.translateURItoJCEID(_algorithm);

      if (logger.isLoggable(java.util.logging.Level.FINE))                                     logger.log(java.util.logging.Level.FINE, "alg = " + jceAlgorithm);

      try {
                            if (_requestedJCEProvider == null)
        c = Cipher.getInstance(jceAlgorithm);
                            else
                                c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider);
      } catch (NoSuchAlgorithmException nsae) {
        throw new XMLEncryptionException("empty", nsae);
      } catch (NoSuchProviderException nspre) {
        throw new XMLEncryptionException("empty", nspre);
      } catch (NoSuchPaddingException nspae) {
        throw new XMLEncryptionException("empty", nspae);
      }
    }
    else {
      c = _contextCipher;
    }
    // Now perform the encryption

    try {
      // Should internally generate an IV
      // todo - allow user to set an IV
      c.init(_cipherMode, _key);
    } catch (InvalidKeyException ike) {
      throw new XMLEncryptionException("empty", ike);
    }

        try {
            encryptedBytes =
                c.doFinal(serializedOctets.getBytes("UTF-8"));

            if (logger.isLoggable(java.util.logging.Level.FINE))                                     logger.log(java.util.logging.Level.FINE, "Expected cipher.outputSize = " +
                Integer.toString(c.getOutputSize(
                    serializedOctets.getBytes().length)));
            if (logger.isLoggable(java.util.logging.Level.FINE))                                     logger.log(java.util.logging.Level.FINE, "Actual cipher.outputSize = " +
                Integer.toString(encryptedBytes.length));
        } catch (IllegalStateException ise) {
            throw new XMLEncryptionException("empty", ise);
        } catch (IllegalBlockSizeException ibse) {
            throw new XMLEncryptionException("empty", ibse);
        } catch (BadPaddingException bpe) {
            throw new XMLEncryptionException("empty", bpe);
        } catch (UnsupportedEncodingException uee) {
         throw new XMLEncryptionException("empty", uee);
    }

    // Now build up to a properly XML Encryption encoded octet stream
    // IvParameterSpec iv;

    byte[] iv = c.getIV();
    byte[] finalEncryptedBytes =
      new byte[iv.length + encryptedBytes.length];
    System.arraycopy(iv, 0, finalEncryptedBytes, 0,
             iv.length);
    System.arraycopy(encryptedBytes, 0, finalEncryptedBytes,
View Full Code Here

        logger.log(java.util.logging.Level.FINE, "Serialized octets:\n" + serializedOctets);
       
        byte[] encryptedBytes = null;
       
        // Now create the working cipher if none was created already
        Cipher c;
        if (_contextCipher == null) {
            String jceAlgorithm =
                    JCEMapper.translateURItoJCEID(_algorithm);
           
            logger.log(java.util.logging.Level.FINE, "alg = " + jceAlgorithm);
           
            try {
                if (_requestedJCEProvider == null)
                    c = Cipher.getInstance(jceAlgorithm);
                else
                    c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider);
            } catch (NoSuchAlgorithmException nsae) {
                throw new XMLEncryptionException("empty", nsae);
            } catch (NoSuchProviderException nspre) {
                throw new XMLEncryptionException("empty", nspre);
            } catch (NoSuchPaddingException nspae) {
                throw new XMLEncryptionException("empty", nspae);
            }
        } else {
            c = _contextCipher;
        }
        // Now perform the encryption
       
        try {
            // Should internally generate an IV
            // todo - allow user to set an IV
            c.init(_cipherMode, _key);
        } catch (InvalidKeyException ike) {
            throw new XMLEncryptionException("empty", ike);
        }
       
        try {
            encryptedBytes =
                    c.doFinal(serializedOctets);
           
            logger.log(java.util.logging.Level.FINE, "Expected cipher.outputSize = " +
                    Integer.toString(c.getOutputSize(
                    serializedOctets.length)));
            logger.log(java.util.logging.Level.FINE, "Actual cipher.outputSize = " +
                    Integer.toString(encryptedBytes.length));
        } catch (IllegalStateException ise) {
            throw new XMLEncryptionException("empty", ise);
        } catch (IllegalBlockSizeException ibse) {
            throw new XMLEncryptionException("empty", ibse);
        } catch (BadPaddingException bpe) {
            throw new XMLEncryptionException("empty", bpe);
        } catch (Exception uee) {
            throw new XMLEncryptionException("empty", uee);
        }
       
        // Now build up to a properly XML Encryption encoded octet stream
        // IvParameterSpec iv;
       
        byte[] iv = c.getIV();
        byte[] finalEncryptedBytes =
                new byte[iv.length + encryptedBytes.length];
        System.arraycopy(iv, 0, finalEncryptedBytes, 0,
                iv.length);
        System.arraycopy(encryptedBytes, 0, finalEncryptedBytes,
View Full Code Here

    }

    _contextDocument = doc;

    byte[] encryptedBytes = null;
    Cipher c;

    if (_contextCipher == null) {
      // Now create the working cipher

      String jceAlgorithm =
        JCEMapper.translateURItoJCEID(_algorithm);

      if (logger.isLoggable(java.util.logging.Level.FINE))                                     logger.log(java.util.logging.Level.FINE, "alg = " + jceAlgorithm);

      try {
          if (_requestedJCEProvider == null)
        c = Cipher.getInstance(jceAlgorithm);
                            else
                                c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider);
      } catch (NoSuchAlgorithmException nsae) {
        throw new XMLEncryptionException("empty", nsae);
      } catch (NoSuchProviderException nspre) {
        throw new XMLEncryptionException("empty", nspre);
      } catch (NoSuchPaddingException nspae) {
        throw new XMLEncryptionException("empty", nspae);
      }
    } else {
      c = _contextCipher;
    }
    // Now perform the encryption

    try {
      // Should internally generate an IV
      // todo - allow user to set an IV
      c.init(Cipher.WRAP_MODE, _key);
      encryptedBytes = c.wrap(key);
    } catch (InvalidKeyException ike) {
      throw new XMLEncryptionException("empty", ike);
    } catch (IllegalBlockSizeException ibse) {
      throw new XMLEncryptionException("empty", ibse);
    }
View Full Code Here

    byte [] encryptedBytes = cipherInput.getBytes();

    String jceKeyAlgorithm =
      JCEMapper.getJCEKeyAlgorithmFromURI(algorithm);

    Cipher c;
    if (_contextCipher == null) {
      // Now create the working cipher

      String jceAlgorithm =
        JCEMapper.translateURItoJCEID(
          encryptedKey.getEncryptionMethod().getAlgorithm());

      if (logger.isLoggable(java.util.logging.Level.FINE))                                     logger.log(java.util.logging.Level.FINE, "JCE Algorithm = " + jceAlgorithm);

      try {
                            if (_requestedJCEProvider == null)
        c = Cipher.getInstance(jceAlgorithm);
                            else
                                c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider);
      } catch (NoSuchAlgorithmException nsae) {
        throw new XMLEncryptionException("empty", nsae);
      } catch (NoSuchProviderException nspre) {
        throw new XMLEncryptionException("empty", nspre);
      } catch (NoSuchPaddingException nspae) {
        throw new XMLEncryptionException("empty", nspae);
      }
    } else {
      c = _contextCipher;
    }

    Key ret;

    try {   
      c.init(Cipher.UNWRAP_MODE, _key);
      ret = c.unwrap(encryptedBytes, jceKeyAlgorithm, Cipher.SECRET_KEY);
     
    } catch (InvalidKeyException ike) {
      throw new XMLEncryptionException("empty", ike);
    } catch (NoSuchAlgorithmException nsae) {
      throw new XMLEncryptionException("empty", nsae);
View Full Code Here

    // Now create the working cipher

    String jceAlgorithm =
      JCEMapper.translateURItoJCEID(encryptedData.getEncryptionMethod().getAlgorithm());

    Cipher c;
    try {
                    if (_requestedJCEProvider == null)
      c = Cipher.getInstance(jceAlgorithm);
                    else
                        c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider);
    } catch (NoSuchAlgorithmException nsae) {
      throw new XMLEncryptionException("empty", nsae);
    } catch (NoSuchProviderException nspre) {
      throw new XMLEncryptionException("empty", nspre);
    } catch (NoSuchPaddingException nspae) {
      throw new XMLEncryptionException("empty", nspae);
    }

    // Calculate the IV length and copy out

    // For now, we only work with Block ciphers, so this will work.
    // This should probably be put into the JCE mapper.

    int ivLen = c.getBlockSize();
    byte[] ivBytes = new byte[ivLen];

    // You may be able to pass the entire piece in to IvParameterSpec
    // and it will only take the first x bytes, but no way to be certain
    // that this will work for every JCE provider, so lets copy the
    // necessary bytes into a dedicated array.

    System.arraycopy(encryptedBytes, 0, ivBytes, 0, ivLen);
    IvParameterSpec iv = new IvParameterSpec(ivBytes);   
   
    try {
      c.init(_cipherMode, _key, iv);
    } catch (InvalidKeyException ike) {
      throw new XMLEncryptionException("empty", ike);
    } catch (InvalidAlgorithmParameterException iape) {
      throw new XMLEncryptionException("empty", iape);
    }

    byte[] plainBytes;

        try {
            plainBytes = c.doFinal(encryptedBytes,
                   ivLen,
                   encryptedBytes.length - ivLen);

        } catch (IllegalBlockSizeException ibse) {
            throw new XMLEncryptionException("empty", ibse);
View Full Code Here

      Instance instance = GetInstance.getInstance(s, SignatureSpi.class);
      return getInstance(instance, RSA_SIGNATURE);
  }
  // check Cipher
  try {
      Cipher c = Cipher.getInstance(RSA_CIPHER, p);
      return new Delegate(new CipherAdapter(c), RSA_SIGNATURE);
  } catch (GeneralSecurityException e) {
      // throw Signature style exception message to avoid confusion,
      // but append Cipher exception as cause
      throw new NoSuchAlgorithmException("no such algorithm: "
View Full Code Here

  private static SignatureSpi newInstance(Service s)
    throws NoSuchAlgorithmException {
      if (s.getType().equals("Cipher")) {
    // must be NONEwithRSA
    try {
        Cipher c = Cipher.getInstance(RSA_CIPHER, s.getProvider());
        return new CipherAdapter(c);
    } catch (NoSuchPaddingException e) {
        throw new NoSuchAlgorithmException(e);
    }
      } else {
View Full Code Here

    private static byte[] getEncrypted(byte[] plaintext, char[] password) {

        try {
            /* Create PBE Cipher */
            Cipher pbeCipher = getCipher(Cipher.ENCRYPT_MODE, password);
            /* Encrypt the plaintext */
            return pbeCipher.doFinal(plaintext);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
View Full Code Here

TOP

Related Classes of javax.crypto.Cipher$Transform

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.