KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > pgp > KeyBasedEncryptionStrategy


1 /*
2  * $Id: KeyBasedEncryptionStrategy.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.extras.pgp;
12
13 import cryptix.message.EncryptedMessage;
14 import cryptix.message.EncryptedMessageBuilder;
15 import cryptix.message.LiteralMessageBuilder;
16 import cryptix.message.Message;
17 import cryptix.message.MessageFactory;
18 import cryptix.message.SignedMessageBuilder;
19 import cryptix.openpgp.PGPArmouredMessage;
20 import cryptix.pki.KeyBundle;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.mule.config.i18n.Messages;
25 import org.mule.umo.UMOEncryptionStrategy;
26 import org.mule.umo.lifecycle.InitialisationException;
27 import org.mule.umo.security.CryptoFailureException;
28
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.util.Collection JavaDoc;
31
32 /**
33  * @author ariva
34  */

35 public class KeyBasedEncryptionStrategy implements UMOEncryptionStrategy
36 {
37     protected static Log logger = LogFactory.getLog(KeyBasedEncryptionStrategy.class);
38
39     private PGPKeyRing keyManager;
40
41     /*
42      * (non-Javadoc)
43      *
44      * @see org.mule.umo.UMOEncryptionStrategy#encrypt(byte[])
45      */

46     public byte[] encrypt(byte[] data, Object JavaDoc cryptInfo) throws CryptoFailureException
47     {
48         try
49         {
50             PGPCryptInfo pgpCryptInfo = (PGPCryptInfo)cryptInfo;
51             KeyBundle publicKey = pgpCryptInfo.getKeyBundle();
52
53             LiteralMessageBuilder lmb = LiteralMessageBuilder.getInstance("OpenPGP");
54
55             lmb.init(data);
56
57             Message msg = lmb.build();
58
59             if (pgpCryptInfo.isSignRequested())
60             {
61                 SignedMessageBuilder smb = SignedMessageBuilder.getInstance("OpenPGP");
62
63                 smb.init(msg);
64                 smb.addSigner(keyManager.getSecretKeyBundle(), keyManager.getSecretPassphrase().toCharArray());
65
66                 msg = smb.build();
67             }
68
69             EncryptedMessageBuilder emb = EncryptedMessageBuilder.getInstance("OpenPGP");
70             emb.init(msg);
71             emb.addRecipient(publicKey);
72             msg = emb.build();
73
74             return new PGPArmouredMessage(msg).getEncoded();
75         }
76         catch (Exception JavaDoc e)
77         {
78             throw new CryptoFailureException(this, e);
79         }
80     }
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see org.mule.umo.UMOEncryptionStrategy#decrypt(byte[])
86      */

87     public byte[] decrypt(byte[] data, Object JavaDoc cryptInfo) throws CryptoFailureException
88     {
89         try
90         {
91             MessageFactory mf = MessageFactory.getInstance("OpenPGP");
92
93             ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(data);
94
95             Collection JavaDoc msgs = mf.generateMessages(in);
96
97             Message msg = (Message)msgs.iterator().next();
98
99             if (msg instanceof EncryptedMessage)
100             {
101                 msg = ((EncryptedMessage)msg).decrypt(keyManager.getSecretKeyBundle(),
102                     keyManager.getSecretPassphrase().toCharArray());
103
104                 return new PGPArmouredMessage(msg).getEncoded();
105             }
106         }
107         catch (Exception JavaDoc e)
108         {
109             throw new CryptoFailureException(this, e);
110         }
111
112         return data;
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.mule.umo.lifecycle.Initialisable#initialise()
119      */

120     public void initialise() throws InitialisationException
121     {
122         try
123         {
124             java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto());
125             java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP());
126         }
127         catch (Exception JavaDoc e)
128         {
129             throw new InitialisationException(new org.mule.config.i18n.Message(Messages.FAILED_TO_CREATE_X,
130                 "KeyBasedEncryptionStrategy"), e, this);
131         }
132     }
133
134     public PGPKeyRing getKeyManager()
135     {
136         return keyManager;
137     }
138
139     public void setKeyManager(PGPKeyRing keyManager)
140     {
141         this.keyManager = keyManager;
142     }
143 }
144
Popular Tags