FileDocCategorySizeDatePackage
CryptoManagerImpl.javaAPI DocAzureus 3.0.3.46898Thu Jun 15 08:42:22 BST 2006com.aelitis.azureus.core.security.impl

CryptoManagerImpl

public class CryptoManagerImpl extends Object implements com.aelitis.azureus.core.security.CryptoManager

Fields Summary
private static final int
PBE_ITERATIONS
private static final String
PBE_ALG
private static CryptoManagerImpl
singleton
private byte[]
secure_id
private com.aelitis.azureus.core.security.CryptoHandler
ecc_handler
private List
listeners
Constructors Summary
protected CryptoManagerImpl()

	
	
	
	
		SESecurityManager.initialise();
		
		ecc_handler = new CryptoHandlerECC( this, 1 );
	
Methods Summary
public voidaddPasswordHandler(com.aelitis.azureus.core.security.CryptoManagerPasswordHandler handler)

		listeners.add( handler );
	
protected byte[]decryptWithPBE(byte[] data, char[] password)

		boolean fail_is_pw_error = false;
		
		try{
			byte[]	salt = new byte[8];
			
			System.arraycopy( data, 0, salt, 0, 8 );
			
			PBEKeySpec keySpec = new PBEKeySpec(password);
	
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( PBE_ALG );
	
			SecretKey key = keyFactory.generateSecret(keySpec);
	
			PBEParameterSpec paramSpec = new PBEParameterSpec(salt, PBE_ITERATIONS);
	
			Cipher cipher = Cipher.getInstance( PBE_ALG );
			
			cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
	
			fail_is_pw_error = true;
			
			return( cipher.doFinal( data, 8, data.length-8 ));
			
		}catch( Throwable e ){
			
			if ( fail_is_pw_error ){
				
				throw( new CryptoManagerPasswordException( e ));
				
			}else{
				throw( new CryptoManagerException( "PBE decryption failed", e ));
			}
		}
   	
protected byte[]encryptWithPBE(byte[] data, char[] password)

		try{
			byte[]	salt = new byte[8];
			
			new SecureRandom().nextBytes( salt );
			
			PBEKeySpec keySpec = new PBEKeySpec(password);
		
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( PBE_ALG );
		
			SecretKey key = keyFactory.generateSecret(keySpec);
		
			PBEParameterSpec paramSpec = new PBEParameterSpec( salt, PBE_ITERATIONS );
		
			Cipher cipher = Cipher.getInstance( PBE_ALG );
			
			cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
	
			byte[]	enc = cipher.doFinal( data );
			
			byte[]	res = new byte[salt.length + enc.length];
			
			System.arraycopy( salt, 0, res, 0, salt.length );
			
			System.arraycopy( enc, 0, res, salt.length, enc.length );
			
			return( res );
			
		}catch( Throwable e ){
			
			throw( new CryptoManagerException( "PBE encryption failed", e ));
		}
	
public com.aelitis.azureus.core.security.CryptoHandlergetECCHandler()

		return( ecc_handler );
	
protected char[]getPassword(int handler, int action, java.lang.String reason)

		System.out.println( "getPassword:" + handler + "/" + action + "/" + reason );
		
		if ( listeners.size() == 0 ){
			
			throw( new CryptoManagerException( "No password handlers registered" ));
		}
		
		for (int i=0;i<listeners.size();i++){
			
			try{
				char[]	pw = ((CryptoManagerPasswordHandler)listeners.get(i)).getPassword( handler, action, reason );
				
				if ( pw != null ){
					
					return( pw );
				}
			}catch( Throwable e ){
				
				Debug.printStackTrace(e);
			}
		}
		
		throw( new CryptoManagerException( "No password handlers returned a password" ));
	
public byte[]getSecureID()

		if ( secure_id == null ){
			
			secure_id = COConfigurationManager.getByteParameter( "core.crypto.id", null );
		}
		
		if ( secure_id == null ){
			
			secure_id = new byte[20];
		
			new SecureRandom().nextBytes( secure_id );
			
			COConfigurationManager.setParameter( "core.crypto.id", secure_id );
			
			COConfigurationManager.save();
		}
		
		return( secure_id );
	
public static synchronized com.aelitis.azureus.core.security.CryptoManagergetSingleton()

	
	   
	
	
		if ( singleton == null ){
			
			singleton = new CryptoManagerImpl();
		}
		
		return( singleton );
	
public static voidmain(java.lang.String[] args)

		try{

			String	stuff = "12345";
			
			CryptoManagerImpl man = (CryptoManagerImpl)getSingleton();
			
			man.addPasswordHandler(
				new CryptoManagerPasswordHandler()
				{
					public char[] 
					getPassword(
							int handler_type, 
							int action_type, 
							String reason )
					{
						return( "trout".toCharArray());
					}
				});
			
			CryptoHandler	handler1 = man.getECCHandler();
			
			CryptoHandler	handler2 = new CryptoHandlerECC( man, 2 );
			

			//handler.resetKeys( "monkey".toCharArray() );
			
			byte[]	sig = handler1.sign( stuff.getBytes(), "Test signing" );
			
			System.out.println( handler1.verify( handler1.getPublicKey(  "Test verify" ), stuff.getBytes(), sig ));
			
			byte[]	enc = handler1.encrypt( handler2.getPublicKey( "" ), stuff.getBytes(), "" );
			
			System.out.println( "pk1 = " + ByteFormatter.encodeString( handler1.getPublicKey("")));
			System.out.println( "pk2 = " + ByteFormatter.encodeString( handler2.getPublicKey("")));
			
			System.out.println( "dec: " + new String( handler2.decrypt(handler1.getPublicKey( "" ), enc, "" )));
			
		}catch( Throwable e ){
			
			e.printStackTrace();
		}
	
public voidremovePasswordHandler(com.aelitis.azureus.core.security.CryptoManagerPasswordHandler handler)

		listeners.remove( handler );