FileDocCategorySizeDatePackage
TransportCipher.javaAPI DocAzureus 3.0.3.44788Sun Jun 18 05:22:46 BST 2006com.aelitis.azureus.core.networkmanager.impl

TransportCipher

public class TransportCipher extends Object

Fields Summary
private static boolean
internal_rc4
private Cipher
cipher
private org.bouncycastle.crypto.engines.RC4Engine
rc4_engine
Constructors Summary
protected TransportCipher(String algorithm, int mode, SecretKeySpec key_spec, AlgorithmParameterSpec params)

	
	
	
							
								
					
			 
	
		 
	
    	cipher = Cipher.getInstance( algorithm );
    
    	cipher.init( mode, key_spec, params );
	
TransportCipher(String algorithm, int mode, SecretKeySpec key_spec)

	    if ( algorithm.equals( "RC4" )){
	    	
	    	if ( !internal_rc4 ){
	    		
	    		try{
	    	    	cipher = Cipher.getInstance( algorithm );
	    		    
	    	    	cipher.init( mode, key_spec );
	    	    	
	    		}catch( Throwable e ){
	    			
	    			internal_rc4	= true;
	    		}
	    	}
	    	
	    	if ( internal_rc4 ){
	    		    		
	    		rc4_engine	= new RC4Engine();
	    		
	    		CipherParameters	params = new KeyParameter(key_spec.getEncoded());
	    		
	    		rc4_engine.init( mode == Cipher.ENCRYPT_MODE, params ); 
	    	}
	    	
	    	//System.out.println( "RC4 key: " + ByteFormatter.encodeString( key_spec.getEncoded()));
	    	
    			// skip first 1024 bytes of stream to protected against a Fluhrer, Mantin and Shamir attack
    	
	    	byte[]	temp = new byte[1024];
    	
	    	temp = update( temp );
	    	
	    	//System.out.println( "RC4: first discard = " + ByteFormatter.encodeString( temp, 0, 4 ));
	    }else{
	    	
	    	cipher = Cipher.getInstance( algorithm );
	    
	    	cipher.init( mode, key_spec );
	    }
	
Methods Summary
public java.lang.StringgetName()

		if ( cipher != null ){
			
			String	s = cipher.getAlgorithm();
			
			int	pos = s.indexOf("/");
			
			if ( pos != -1 ){
				
				s = s.substring(0,pos);
			}
			
			if ( s.equals( "RC4" )){
				
				s = "RC4-160";
				
			}else{
				
				s += "-" + cipher.getBlockSize()*8;
			}
			
			return( s );
		}else{
			
			return( "RC4-160" );
		}
	
protected byte[]update(byte[] data)

		return( update( data, 0, data.length ));
	
protected byte[]update(byte[] data, int offset, int length)

		byte[]	result;
	
		if ( length == 0 ){
			
				// watch out, cipher.update returns NULL with 0 length input
			
			result = new byte[0];
			
		}else if ( cipher != null ){
						
			result = cipher.update( data, offset, length );
			
		}else{
						
			result = new byte[length];
			
			rc4_engine.processBytes( data, offset, length, result, 0 );
		}
	
		return( result );
   	
protected voidupdate(java.nio.ByteBuffer source_buffer, java.nio.ByteBuffer target_buffer)

		try{
			// TODO: 1.5 supports update( ByteBuffer, ByteBuffer )
			
			byte[]	source_bytes;
			int		offset;
			int		length	= source_buffer.remaining();
			
			if ( source_buffer.hasArray()){
				
				source_bytes 	= source_buffer.array();
				
				offset			= source_buffer.arrayOffset() + source_buffer.position();
				
			}else{
				
				source_bytes 	= new byte[length];
				
				offset			= 0;
				
				source_buffer.get( source_bytes );
			}
			
			byte[]	target_bytes = update( source_bytes, offset, length ); 
			
			source_buffer.position( source_buffer.limit());
			
			target_buffer.put( target_bytes );
			
		}catch( Throwable e ){
						
			throw( new IOException( Debug.getNestedExceptionMessage( e )));
		}