torsdag 29 september 2011

Gstickies a notes program for gnome released under GPLv2

You can find it for free at GitHub:

https://github.com/QubeX2/gstickies























//QubeX2

Tips #2 Add javascript: click() prototype to HTMLElement's for Firefox etc.

If you want to use the click() method on a HTML element on a webbrowser other than Microsoft's Internet Explorer you can add this code at the start somewhere of your code.

if(typeof HTMLElement!='undefined'&&!HTMLElement.prototype.click)
    HTMLElement.prototype.click=function(){
        var evt = this.ownerDocument.createEvent('MouseEvents');
        evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
        this.dispatchEvent(evt);
    }


//QubeX2

onsdag 28 september 2011

Problem #1 How to encrypt data between PHP and C#

One important note. When you want to encrypt the data on the
C# side you have to pad the iv to length of 32.


In PHP encrypt data like this:
<?php
$iv
"01234567890123456789012345678901";
$key"this is they key"; 

$text"This text will be encrypted..."; 
$bsize mcrypt_get_block_size(MCRYPT_RIJNDAEL_256MCRYPT_MODE_CBC);
$pad$bsize - (strlen($text) % $bsize);
$text .= str_repeat(chr($pad), $pad);
$encrypted mcrypt_encrypt(MCRYPT_RIJNDAEL_256$key$textMCRYPT_MODE_CBC$iv);
$enc_text base64_encode($encrypted);
 

echo "$enc_text";

/* the output
 *  MCfBJe4H7U+9Qb48RkHlXUt1/it10mx+TG7lrCUjO4w=
 */
?>


In C# decrypt data like this:

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Encryption
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string enc_text = @"MCfBJe4H7U+9Qb48RkHlXUt1/it10mx+TG7lrCUjO4w=";
            string iv = "01234567890123456789012345678901";
            string key = "this is they key";
            byte[] bytes = Base64_Decode(enc_text);
            Console.WriteLine(Decrypt(bytes, key, iv));
        }

        public static byte[] Base64_Decode (string str)
        {
            return Convert.FromBase64String (str);
        }
        
        public static string Decrypt (byte[] cipher, string key, string iv)
        {
            string retVal = "";
            UTF8Encoding encoding = new UTF8Encoding ();
            byte[] Key = encoding.GetBytes (key);
            byte[] IV = encoding.GetBytes (iv);
            using (RijndaelManaged rj = new RijndaelManaged ()) {
                try {
                    rj.Padding = PaddingMode.PKCS7;
                    rj.Mode = CipherMode.CBC;
                    rj.KeySize = 256;
                    rj.BlockSize = 256;
                    rj.Key = Key;
                    rj.IV = IV;
                    using (MemoryStream ms = new MemoryStream (cipher)) {
                        using (CryptoStream cs = new CryptoStream (ms, rj.CreateDecryptor (Key, IV), CryptoStreamMode.Read)) {
                            using (StreamReader sr = new StreamReader (cs)) {
                                retVal = sr.ReadLine ();
                            }
                        }
                    }
                } finally {
                    rj.Clear ();
                }
            }
            return retVal;
        }
    }
}

//QubeX2

Tips #1 Batch resize images with Bash

find ./ -iname "*.jp*" -exec /-mogrify -resize 800 {} \;

The argument -iname makes find match case insensitive.
The argument -exec continues until it finds a backslash escaped ; eg. "\;".

Good luck
/QubeX2