Package java.security

Examples of java.security.Key

Keys are generally obtained through key generators, certificates, or various Identity classes used to manage keys. Keys may also be obtained from key specifications (transparent representations of the underlying key material) through the use of a key factory (see {@link KeyFactory}).

A Key should use KeyRep as its serialized representation. Note that a serialized Key may contain sensitive information which should not be exposed in untrusted environments. See the Security Appendix of the Serialization Specification for more information. @see PublicKey @see PrivateKey @see KeyPair @see KeyPairGenerator @see KeyFactory @see KeyRep @see java.security.spec.KeySpec @see Identity @see Signer @version 1.57 06/04/21 @author Benjamin Renaud


     */
    public KeyPair getKeyPair(String alias, char[] password) {
        try {
            checkKeyStore();
            if (isKeyStoreExists() && !isKeyStoreEmpty()) {
                Key key = keyStore.getKey(alias, password);
                if (key instanceof PrivateKey) {
                    Certificate cert = keyStore.getCertificate(alias);
                    PublicKey publicKey = cert.getPublicKey();
                    return new KeyPair(publicKey, (PrivateKey) key);
                }
View Full Code Here


         
          Certificate c[] = kspkcs12.getCertificateChain(alias);
          // Make sure we don't have a null chain
          if (c == null)
              c = new Certificate[] {};
          Key key = kspkcs12.getKey(alias, password == null ? null : password.toCharArray());
            if(key == null) {
                throw new Exception("No alias of '" + alias + "' in imported PKCS12 key file.");
            }
          this.keyStore.setKeyEntry(newAlias, key, getKeyStorePassword().toCharArray(), c);
        } finally {
View Full Code Here

                keyblob = bar.readBinaryString();

                Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
                KeySpec keyspec = new DESedeKeySpec(keydata);
                Key key = SecretKeyFactory.getInstance("DESede").generateSecret(keyspec);
                cipher.init(Cipher.DECRYPT_MODE, key,
                    new IvParameterSpec(iv, 0, cipher.getBlockSize()));

                ByteArrayReader data = new ByteArrayReader(cipher.doFinal(
                            keyblob));
View Full Code Here

                    Utils.getRND().nextBytes(iv);

                    Cipher cipher = Cipher.getInstance(
                            "DESede/CBC/PKCS5Padding");
                    KeySpec keyspec = new DESedeKeySpec(keydata);
                    Key key = SecretKeyFactory.getInstance("DESede")
                                              .generateSecret(keyspec);
                    cipher.init(Cipher.ENCRYPT_MODE, key,
                        new IvParameterSpec(iv, 0, cipher.getBlockSize()));

                    ByteArrayWriter data = new ByteArrayWriter();
View Full Code Here

           final String strAlias = pkcs12Aliases.nextElement();
           System.err.println("Importing Alias '" + strAlias + "'");

           if (this.kspkcs12.isKeyEntry(strAlias)) {
              System.err.println("- Alias has key");
              final Key key = this.kspkcs12.getKey(strAlias, (this.kspkcs12Pass!=null)?this.kspkcs12Pass.toCharArray():null);
              System.err.println("- Alias key imported");

              final Certificate[] chain = this.kspkcs12.getCertificateChain(strAlias);
              System.err.println("- Alias certificate chain size: " + chain.length);
View Full Code Here

   */
  public MimeMessage createEncryptedMessage(UserProfile profile, MimeMessage mm) throws MessagingException, java.io.IOException, java.security.GeneralSecurityException {
    if (cryptoType == NO_CRYPTO)
      return mm;

    Key signKey = null;

    signKey = getSignatureKey();
    if (signKey == null){
      switch (cryptoType) {
      case SMIME_SIGN:
      case SMIME_BOTH:
        signKey = profile.getEncryptionKey(EncryptionManager.SMIME, true);
        break;
      case PGP_SIGN:
      case PGP_BOTH:
        signKey = profile.getEncryptionKey(EncryptionManager.PGP, true);
        break;
      }
    }

    PookaEncryptionManager cryptoManager = Pooka.getCryptoManager();

    InternetAddress from = (InternetAddress) mm.getFrom()[0];

    // Find the keys to sign and encrypt the messages
    if(signKey == null && (cryptoType == SMIME_SIGN || cryptoType == SMIME_BOTH)){
      Key[] keys = cryptoManager.getPrivateKeysForAddress(from.getAddress(), EncryptionManager.SMIME,  true);
      if (keys == null || keys.length == 0) {
        // show dialog
        signKey = CryptoKeySelector.selectPrivateKey(Pooka.getProperty("Pooka.crypto.privateKey.forSign", "Select key to sign this message."), EncryptionManager.SMIME, true);
      } else {
        signKey = keys[0];
      }

      if (signKey == null) {
        throw new GeneralSecurityException("No signature key selected.");
      }
    }

    if(signKey == null &&  (cryptoType == PGP_SIGN || cryptoType == PGP_BOTH)){
      Key[] keys = cryptoManager.getPrivateKeysForAddress(from.getAddress(), EncryptionManager.PGP,  true);
      if (keys == null || keys.length == 0) {
        // show dialog
        signKey = CryptoKeySelector.selectPrivateKey(Pooka.getProperty("Pooka.crypto.privateKey.forSign", "Select key to sign this message."), EncryptionManager.PGP, true);

      } else {
        signKey = keys[0];
      }

      if (signKey == null) {
        throw new GeneralSecurityException("No signature key selected.");
      }
    }

    List encKeys = new LinkedList();

    //TODO: get the encKey from the available public keys
    if (cryptoType == SMIME_ENCRYPT || cryptoType == SMIME_BOTH || cryptoType == PGP_ENCRYPT || cryptoType == PGP_BOTH) {
      String type = (cryptoType == SMIME_ENCRYPT || cryptoType == SMIME_BOTH) ? EncryptionManager.SMIME : EncryptionManager.PGP;
      // Get the public key of the senders
      Address[] froms = mm.getFrom();
      for (int i = 0; i < froms.length; i++) {
        from = (InternetAddress) froms[i];
        Key[] keys = cryptoManager.getPublicKeys(from.getAddress(), type, false);
        if (keys != null && keys.length > 0) {
          encKeys.add(keys[0]);
        }
      }

      // Get the public key of the receivers
      Address[] receivers = mm.getAllRecipients();
      for (int i = 0; i < receivers.length; i++) {
        InternetAddress rec = (InternetAddress) receivers[i];
        Key[] keys = cryptoManager.getPublicKeys(rec.getAddress(), type, false);
        if (keys != null && keys.length > 0) {
          encKeys.add(keys[0]);
        } else {
          Key key = CryptoKeySelector.selectPublicKey(Pooka.getProperty("Pooka.crypto.publicKey.forEncrypt", "Select key to encrypt this message."), EncryptionManager.PGP, false);
          if (key != null)
            encKeys.add(key);
          else
            throw new GeneralSecurityException("found no certificate for " + rec.getAddress());
        }
View Full Code Here

    Key[] cryptKeys = null;

    if (getEncryptMessage() != CRYPTO_NO)
      cryptKeys = getEncryptionKeys();

    Key sigKey = null;
    if (getSignMessage() != CRYPTO_NO)
      sigKey = getSignatureKey();

    mRecipientsInfo = new CryptoRecipientsInfo(sigKey, cryptKeys, toAddresses, ccAddresses, bccAddresses);
View Full Code Here

        returnValue.setRecipients(Message.RecipientType.TO, getRecipients(Message.RecipientType.TO));
        returnValue.setRecipients(Message.RecipientType.CC, getRecipients(Message.RecipientType.CC));
        returnValue.setRecipients(Message.RecipientType.BCC, getRecipients(Message.RecipientType.BCC));
      */

      Key sigKey = getSignatureKey();
      Key[] cryptoKeys = getEncryptionKeys();

      /*      if (sigKey instanceof EncryptionKey && cryptoKey instanceof EncryptionKey) {
              if (((EncryptionKey)sigKey).getType() != ((EncryptionKey)cryptoKey).getType()) {
              throw new MessagingException(Pooka.getProperty("error.NewMessage.differentEncryption", "Encryption and Signature Keys must be of same type (PGP or S/MIME)"));
View Full Code Here

    }
     
    it = aliases.iterator();
    while(it.hasNext()){
     alias = (String) it.next();
     Key key = null;
    try {
      key = this.getPrivateKey(alias, type);
    } catch (Exception e) {
      continue;
    }
View Full Code Here

   }else{
     return null;
   }
  
    // first check to see if this is in the cache.
    Key cachedKey = (Key) cachedPrivateKeys.get(alias);
    if (cachedKey != null){
      return cachedKey;
  }
   
    KeyStoreException caughtException = null;
    if (pgpKeyMgr != null || smimeKeyMgr != null) {
      // check to see if this exists anywhere.
      if (pgpKeyMgr != null && EncryptionManager.PGP.equalsIgnoreCase(type)) {
    try {
      if (pgpKeyMgr.containsPrivateKeyAlias(alias)) {
        Key returnValue = pgpKeyMgr.getPrivateKey(alias, passphrase);

        cachedPrivateKeys.put(alias, returnValue);
        return returnValue;
      }
    } catch (KeyStoreException kse) {
      caughtException = kse;
    }
      }
     
      if (smimeKeyMgr!= null && EncryptionManager.SMIME.equalsIgnoreCase(type)) {
    try {
      if (smimeKeyMgr.containsPrivateKeyAlias(alias)) {
        Key returnValue = smimeKeyMgr.getPrivateKey(alias, passphrase);
        cachedPrivateKeys.put(alias, returnValue);
        return returnValue;
      }
    } catch (KeyStoreException kse) {
      if (caughtException == null)
View Full Code Here

TOP

Related Classes of java.security.Key

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.