Go to the documentation of this file.00001
00002
00003
00004
00005 package crypto;
00006
00007 import java.io.File;
00008 import java.io.IOException;
00009 import java.security.SignedObject;
00010
00011 import utils.Log;
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 public class CipherEngine
00023 {
00024
00025
00026
00027 private static AsymmetricCipher privateCipher = null;
00028
00029
00030
00031
00032 private final static String myCipherAlgorithm = "Blowfish";
00033
00034
00035
00036
00037 private final static int myCipherKeySize = 32;
00038
00039
00040
00041
00042 private static final String defaultPrivateKeyDirectory = ".mykf";
00043
00044
00045
00046
00047 private static String myPrivateKeyPath = "";
00048
00049
00050
00051
00052 private static SymmetricCipher myPduCipher = null;
00053
00054
00055
00056
00057 public static String getPrivateKeyDirectory ()
00058 {
00059 return myPrivateKeyPath;
00060 }
00061
00062
00063
00064
00065
00066 public static String getSignedPublicKey ()
00067 {
00068 if ( privateCipher == null ) {
00069 return null;
00070 }
00071
00072 return privateCipher.getSerializedAndSignedPublicKey ();
00073 }
00074
00075
00076
00077
00078 public static String getNamedPublicKey ()
00079 {
00080 if ( privateCipher == null ) {
00081 return null;
00082 }
00083
00084 return privateCipher.getNamedPublicKey ();
00085 }
00086
00087
00088
00089
00090 public static SymmetricCipher getCipher ()
00091 {
00092 return myPduCipher;
00093 }
00094
00095
00096
00097
00098 public static SignedObject getSignedSecretKey ()
00099 {
00100 if ( privateCipher == null ) {
00101 return null;
00102 }
00103
00104 return privateCipher.signObject( myPduCipher.getSecretKey () );
00105 }
00106
00107
00108
00109
00110
00111
00112 public static SymmetricCipher deserializeEncryptedSecretKey( String encryptedSecret )
00113 {
00114 if ( privateCipher == null ) {
00115 return null;
00116 }
00117
00118 return privateCipher.deserializeEncryptedSecretKey( encryptedSecret );
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 public static void initialize ()
00133 {
00134
00135
00136
00137
00138 try
00139 {
00140 myPrivateKeyPath = "";
00141
00142 String dirPath = System.getProperty( "user.home" )
00143 + System.getProperty( "file.separator" )
00144 + defaultPrivateKeyDirectory;
00145
00146 File directory = new File( dirPath );
00147
00148 if ( ! directory.exists () ) {
00149 directory.mkdir ();
00150 }
00151
00152 if ( directory.exists () )
00153 {
00154 myPrivateKeyPath = dirPath + System.getProperty( "file.separator" );
00155
00156 String osName = System.getProperty( "os.name" ).toLowerCase();
00157 if ( ! osName.matches( "^.*windows.*$" ) )
00158 {
00159 try
00160 {
00161 Runtime.getRuntime().exec( new String[] { "chmod", "go=", dirPath } );
00162 }
00163 catch( IOException e )
00164 {
00165 Log.trace( "Failed to do chmod; OS = " + osName );
00166 Log.exception( Log.TRACE, e );
00167 }
00168 }
00169 }
00170 }
00171 catch( Exception e )
00172 {
00173 Log.exception( Log.ERROR, e );
00174 }
00175
00176 Log.trace( "Private Key directory: " + myPrivateKeyPath );
00177
00178
00179
00180
00181 Runnable nonBlockingInitThread = new Runnable ()
00182 {
00183 @Override
00184 public void run()
00185 {
00186
00187
00188 if ( myPduCipher == null ) {
00189 myPduCipher = new SymmetricCipher(
00190 myCipherAlgorithm, myCipherKeySize, false );
00191 }
00192
00193 if ( privateCipher == null ) {
00194 privateCipher = new AsymmetricCipher ();
00195 }
00196
00197
00198
00199 PublicEncryptor.loadAuthorizedPublicKeys ();
00200 }
00201 };
00202
00203 ( new Thread( nonBlockingInitThread, "CipherEngine" ) ).start ();
00204 }
00205
00206
00207
00208
00209 public static void reloadAuthorizedPublicKeys ()
00210 {
00211 PublicEncryptor.loadAuthorizedPublicKeys ();
00212 }
00213
00214
00215
00216
00217
00218
00219 public static boolean generateNewSecret( String algorithm, int keySize, boolean verbose )
00220 {
00221 if ( algorithm == null || algorithm.isEmpty () ) {
00222 algorithm = myCipherAlgorithm;
00223 }
00224
00225 if ( keySize <= 0 ) {
00226 keySize = myCipherKeySize;
00227 }
00228
00229 myPduCipher = new SymmetricCipher( algorithm, keySize, verbose );
00230
00231 return myPduCipher.isActive ();
00232 }
00233 }