Wednesday, April 18, 2012

listen to the Internet Explorer download event

I need to write an application in C# and VS2010 that can listen to the download event from Internet Explorer and capture the file URL to add it to a certain database.



My only problem is on how to implement an interface that actually captures that event.



What would I need in order to build or implement such functionality?



Looking for functionality as the "Free download manager or FDM software", each time you start a download on Internet Explorer, a "FDM" window pops up containing the URL of the download.





Python Base64 string shorter than Botan Base64 string

I've managed to get AES/Rijndael [256bit key / 128bit block size] symmetric encryption working: encrypt with pycrypto and decrypting with Botan in C++.



However, when I try to base64 encode the encryption result in python, the resulting string is shorter than the same string generated by Botan using a Base64_Encoder. Example:



Botan Base64:




zjjxmJf5KPs183I/EvC+JuNbOdmbm4bWyhLsdZI8fuVUnKQAeSj0ivmKIYu7HBjM7gLgLV+xtSKcsCeQD7Gy4w==




Py-3k Base64:




zjjxmJf5KPs183I/EvC+JuNbOdmbm4bWyhLsdZI8fuVUnKQAeSj0ivmKIYu7HBjM




You can see that the strings are exactly the same up until the 64 character mark. If I try to decrypt the Python base64 string in Botan it complains about "not enough input".



How do I get the Python base64 string to be acceptable by Botan?



-- EDIT --
When decoding the Botan base64 encoded string in Python:



Botan Decoded:[b'\xce8\xf1\x98\x97\xf9(\xfb5\xf3r?\x12\xf0\xbe&\xe3[9\xd9\x9b\x9b\x86\xd6\xca\x12\xecu\x92<~\xe5T\x9c\xa4\x00y(\xf4\x8a\xf9\x8a!\x8b\xbb\x1c\x18\xcc\xee\x02\xe0-_\xb1\xb5"\x9c\xb0\'\x90\x0f\xb1\xb2\xe3']
Botan Encoded:[b'zjjxmJf5KPs183I/EvC+JuNbOdmbm4bWyhLsdZI8fuVUnKQAeSj0ivmKIYu7HBjM7gLgLV+xtSKcsCeQD7Gy4w==']


Thus, the Python pycrypto result:



Encryption result: b'\xce8\xf1\x98\x97\xf9(\xfb5\xf3r?\x12\xf0\xbe&\xe3[9\xd9\x9b\x9b\x86\xd6\xca\x12\xecu\x92<~\xe5T\x9c\xa4\x00y(\xf4\x8a\xf9\x8a!\x8b\xbb\x1c\x18\xcc'

Base64 encoded: b'zjjxmJf5KPs183I/EvC+JuNbOdmbm4bWyhLsdZI8fuVUnKQAeSj0ivmKIYu7HBjM


Python seems to be "omitting" something. But what?



-- EDIT 2 --



When I try to base64decode & decrypt the pycrypto result, Botan throws this:



Botan exception caught: Buffered_Operation::final - not enough input


So pycrypto is not producing "enough" output such that it can be decrypted by Botan.



-- EDIT 3 ---
Code examples:



Python: changed sensitive info.



import sys
import base64
import binascii
from Crypto.Cipher import AES

plaintext = "097807897-340284-083-08-8034-0843324890098324948"

hex_key = b'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
key = binascii.unhexlify( hex_key )
hex_iv = b'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
iv = binascii.unhexlify( hex_iv )

aes_enc_bytes = AES.new(key, AES.MODE_CBC, iv).encrypt( plaintext )
aes_enc = base64.encodebytes(aes_enc_bytes )

print( "Encrypted:[{}]".format( aes_enc ) )

aes_dec = AES.new(key, AES.MODE_CBC, iv).decrypt( binascii.a2b_base64( aes_enc ) )
print( "Decrypted:[{}]".format( aes_dec ) )


C++ (Qt + Botan)



void botanDecryptor::decrypt()
{
Botan::SymmetricKey key( private_key );
Botan::InitializationVector iv( iv_value );
try
{
// Now decrypt...
Botan::Pipe dec_pipe(new Base64_Decoder, get_cipher("AES-256/CBC", key, iv, Botan::DECRYPTION));

dec_pipe.process_msg( ciphertext );

string decrypted = dec_pipe.read_all_as_string();

cout << "Decrypted:[" << decrypted << "]" << endl;
}
catch(Botan::Exception& e)
{
cout << "Botan exception caught: " << e.what() << endl;
return;
}


-- EDIT 4 --



I decided to try and decrypt the Botan encrypted, base64 encoded string in python and it worked, but it added a bunch of what looks like padding:



Decrypted:[b'097807897-340284-083-08-8034-0843324890098324948\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10']


I then proceeded to add that padding to my pycrypto result before base64 encoding to produce the following, which Botan refuses to decrypt ;(



zjjxmJf5KPs183I/EvC+JuNbOdmbm4bWyhLsdZI8fuVUnKQAeSj0ivmKIYu7HBjMEBAQEBAQEBAQ\nEBAQEBAQEA==


-- ANSWER --
(system wouldn't allow me to self answer for another 5 hours!)



I've finally schlepped through all the documentation and found the answer! One needs to specify what padding method is to be used for the mode. I specified NoPadding e.g.



Pipe dec_pipe(new Base64_Decoder, get_cipher("AES-256/CBC/NoPadding", key, iv, Botan::DECRYPTION));


and viola! The output matches the pycrypto exactly. For reference: [http://botan.randombit.net/filters.html][1]



[1]: Botan Docs: Cipher Filters





Perl to Pseudocode

So i'm trying to re-write a code in python (Im an undergrad and they need their perl code rewritten for python) and I have VERY limitted knowledge of Perl.
I've tried digging around and haven't really found much. So far I've worked parts of teh psuedocode.



Perl:



($colour =~ /^\\#/)


Psuedo:



The Variable Colour does not Equal... 


My problem is the last section, /^\\#/ would anyone be able to tell me what this means?
I have checked its not a variable used in the code. If its any help the code was designed to interact with GIMP.



Please and thankyou :)





toast message android

I am trying to pop up a toast message as long as the phone rings and destroy the toast them the call is rejected or answered.



In the OnReceive method I have something like this:



Bundle bundle=intent.getExtras();
final String state=bundle.getString(TelephonyManager.EXTRA_STATE);

if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
Toast toast= new Toast(context);
toast.show();

new CountDownTimer(3500,1000)
{

@Override
public void onFinish()
{
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)||
(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
toast.cancel();
}
else
{
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
start();
}
}


The problem is that even after the call is hanged up the toast message keeps poping up. It's like the state is never in the HANG_UP or IDLE Mode.



What did I do wrong?





using either checkbox or textbox for an enum type

if I have this structure:



public class Parent
{
public string Name{get; set;}
public List<Child> Childs {get; set;}
}

public class Child
{
public string Name{get; set;}
public string Value{get; set;}
public enum ValueType {get; set;}
}

public enum ValueType
{
Int,
Boolean,
String
};

public class ParentFactory
{
public List<Parent> Parents {get; set;}

public ParentFactory()
{
Child child1 = new Child() {Name="Filter1", Value="1", ValueType=ValueType.String};
Child child2 = new Child() {Name="isExecuted", Value="true", ValueType=ValueType.Boolean};
Child child3 = new Child() {Name="Width", Value="10", ValueType=ValueType.Int};

Parent parent1 = new Parent(){Name="Adam", Childs = new List<Child>(){child1, child2}};
Parent parent2 = new Parent(){Name="Kevin", Childs = new List<Child>(){child3}};

Parents = new List<Parent>(){parent1, parent2};
}
}


I want to bind the object: ParentFactory parentFactory = new ParentFactory(); to ItemsControl:



<DockPanel>
<ItemsControl ItemsSource="{Binding Parents}">
</ItemsControl>
</DockPanel>

<Window.Resources>
<DataTemplate DataType="{x:Type Parent}">
<StackPanel Margin="2,2,2,1">
<Expander Header="{Binding Name}">
<ItemsControl ItemsSource="{Binding Childs}" />
</Expander>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type Child}">
<StackPanel>
<TextBox Grid.Column="0" Text="{Binding Name}" />
<TextBox Grid.Column="1" Text="{Binding Value}"/>
<TextBox Grid.Column="2" Text="{Binding ValueType}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>


In the Stackpanel, there are one control: TextBox. However, I want it to be more dynamic: if the ValueType is a Boolean then use a Checkbox and else use a Textbox for the: .



Would it be possible? and if yes, how can I achieve them. could you please provide some code samples?





Implementation of long list selector in wp7

Is it necessary to use Grouping class for long list selector? In my application i am using a simple list control for holding a large amount of data. Now i feel some performance issues (memory and loading) issues with the ordinary list. So i decided to change my list to long list selector. My doubt is that is it possible to use same Item template and item source for implementing long list. In an example i found a grouping class. Does this grouping is necessary?? If no is the answer how i can implement this with my existing data. Thanks in advance .





jquery timeout-function never called on mouseenter mouseleave

I'm having a little problem with the setTimeout-function.



$(this) is every DOM-Element with a specific class.



When the mouse enters an elememt, and then leave it, there is no problem. But when the mouse leaves an element directly to another (within the 500ms timeout) the first element (that one, the mouse left from) never fades out.



So the new mouseenter-Event kind of prevent the timeOut to call the function.
Without the setTimeout-wrapper everything is just working fine.



Here's my code:



$(this).hover(methods['mouseenterManager'], methods['mouseleaveManager']);


/**
* manage mouseenter events
*/
mouseenterManager: function() {

clearTimeout(timer);

//create toolbar, if no toolbar is in dom
if ($(this).data("layouter").toolbar == undefined) {

//get bottom center of this element
pos_left = ($(this).width() / 2) + $(this).offset().left;
pos_top = $(this).height() + $(this).offset().top;

//create toolbar element
toolbar = $('<div style="display:none; left:' + parseInt(pos_left) + 'px; top:' + parseInt(pos_top) + 'px;" class="layouter_bar"><ul><li><a class="edit" href="javascript:;">Edit</a></li><li><a class="copy" href="javascript:;">Edit</a></li><li><a class="remove" href="javascript:;">Edit</a></li></ul></div>');

//bind this element to toolbar
toolbar.data("layouter", {
parent: $(this),
});

//bind toolbar to this element
data = $(this).data("layouter");
data.toolbar = toolbar;
$(this).data("layouter", data);

//bind this element to toolbar
data = toolbar.data("layouter");
data.parent = $(this);
toolbar.data("layouter", data);

element = $(this);
toolbar.mouseleave(function() {

toolbar = $(this);
timer = setTimeout(function() {
if (!toolbar.is(":hover") && !element.is(":hover")) {

toolbar.fadeOut("fast", function() {
$(this).remove();
});

data = element.data("layouter");
data.toolbar = undefined;
element.data("layouter", data);
}
}, 500);
});

//display the toolbar
$("body").append(toolbar);
toolbar.fadeIn("fast");
}
},


/**
* manage mouseleave events
*/
mouseleaveManager: function() {

toolbar = $(this).data("layouter").toolbar;
element = $(this);
if (toolbar != undefined) {
timer = setTimeout(function() {
if (!toolbar.is(":hover")) {

toolbar.fadeOut("fast", function() {
$(this).remove();
});

data = element.data("layouter");
data.toolbar = undefined;
element.data("layouter", data);
}
}, 500);
}
},

};?


Any ideas?



thank you!