package de.jsd.tools.properties;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class CryptProperties extends Properties
{
private static final long serialVersionUID = 6221146452471126627L;
public enum Mode { ENCYPT, DECYPT};
private final String m_CryptString;
public CryptProperties( String key)
{
this( key, new Properties());
}
public CryptProperties( String key, Properties properties)
{
super();
Enumeration<Object> _keys = properties.keys();
while( _keys.hasMoreElements())
{
String _key = _keys.nextElement().toString();
this.setProperty( _key, properties.getProperty( _key));
}
this.m_CryptString = key;
}
public CryptProperties( String key, String cryptString)throws IOException, ManipulationException
{
this( key);
if( null!=cryptString)
{
try
{
String[] _decypted = this._crypt( this.m_CryptString, cryptString, Mode.DECYPT).split( "&");
for( int i=0; i<_decypted.length; i++)
{
String[] _tmp = _decypted[ i].split( "=");
this.setProperty( _tmp[ 0], _tmp[ 1]);
}
}
catch( ArrayIndexOutOfBoundsException aiobExc)
{
throw new ManipulationException();
}
}
}
/**
* Gibt die enthaltenen Properties als Verschl?sselten String zur?ck
*/
public String toString()
{
StringBuffer _cryptBuffer = new StringBuffer();
Enumeration<Object> _keys = this.keys();
while( _keys.hasMoreElements())
{
String _key = _keys.nextElement().toString();
_cryptBuffer.append( ( ( 0!=_cryptBuffer.length())?"&":"")+_key+"="+this.getProperty( _key));
}
String _retval = null;
try
{
_retval = this._crypt( this.m_CryptString, _cryptBuffer.toString().trim(), Mode.ENCYPT);
}
catch( IOException exc)
{
exc.printStackTrace();
}
return _retval;
}
private String _crypt( String key, String crypt, Mode mode)throws IOException
{
if( null!=crypt)
{
byte[] _crypt = ( mode.equals( Mode.DECYPT))?( new BASE64Decoder().decodeBuffer( crypt)):crypt.getBytes(),
_key = key.getBytes();
for( int i=0, kyPos = 0; i<_crypt.length; i++, kyPos++)
{
if( _key.length==kyPos)
kyPos = 0;
_crypt[ i] = ( byte)( _crypt[ i]^_key[ kyPos]);
}
return ( mode.equals( Mode.ENCYPT))?new BASE64Encoder().encode( _crypt).replaceAll( "\r\n", ""):new String( _crypt);
}
return null;
}
public static String encode( String key, String name, String value)
{
Properties properties = new Properties();
properties.put( name, value);
return encode( key, properties);
}
public static String encode( String key, String props)
{
Properties properties = new Properties();
Matcher matcher = Pattern.compile( "([a-zA-Z0-9]*)=([a-zA-Z0-9]*)").matcher( props);
while( matcher.find())
properties.put( matcher.group( 1), matcher.group( 2));
return encode( key, properties);
}
public static String encode( String key, Properties args)
{
return new CryptProperties( key, args).toString();
}
public static Properties decode( String key, String crpt)
{
try
{
return new CryptProperties( key, crpt);
}
catch ( Exception e)
{
return null;
}
}
public class ManipulationException extends Exception
{
private static final long serialVersionUID = -5897201936298811861L;
private ManipulationException()
{
super( "parameters seems to be manipulated");
}
}
}