digest = md.digest();
}
}
if( revision == 2 && length != 5 )
{
throw new CryptographyException(
"Error: Expected length=5 actual=" + length );
}
//STEP 4
byte[] rc4Key = new byte[ (int)length ];
System.arraycopy( digest, 0, rc4Key, 0, (int)length );
//STEP 5
byte[] paddedUser = truncateOrPad( userPassword );
//STEP 6
rc4.setKey( rc4Key );
ByteArrayOutputStream crypted = new ByteArrayOutputStream();
rc4.write( new ByteArrayInputStream( paddedUser ), crypted );
//STEP 7
if( revision == 3 )
{
byte[] iterationKey = new byte[ rc4Key.length ];
for( int i=1; i<20; i++ )
{
System.arraycopy( rc4Key, 0, iterationKey, 0, rc4Key.length );
for( int j=0; j< iterationKey.length; j++ )
{
iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
}
rc4.setKey( iterationKey );
ByteArrayInputStream input = new ByteArrayInputStream( crypted.toByteArray() );
crypted.reset();
rc4.write( input, crypted );
}
}
//STEP 8
return crypted.toByteArray();
}
catch( NoSuchAlgorithmException e )
{
throw new CryptographyException( e.getMessage() );
}
}