Artem's blog

Mainly .NET (C#, ASP.NET) and my projects

Archives for Programming Tips

Sample of Bubble sort

The MsgBox is there to show how it actually goes through the integer array. You can remove it as well!

    ' A sample of Bubble sort
    ' Have fun!

        Dim i() As Integer = New Integer(4) {2, 5, 3, 1, 4}

        Dim t As Boolean = True
        While (t)
            t = False
            For j As Integer = 0 To 3

                If i(j) > i(j + 1) Then

                    Dim _vec As Integer = i(j)
                    i(j) = i(j + 1)
                    i(j + 1) = _vec
                    t = True

                    'this is only to check the value of i
                    Dim _result As String = ""
                    For y As Integer = 0 To 4
                        _result &= i(y) & ","
                    Next
                    MsgBox(_result)
                End If
            Next
        End While

MsgBox function for C#

        void MsgBox(string _text,string _title = "Message Box")
        {
            //my msgbox. from Visual Basic .NET!!!
            MessageBox.Show(_text,_title );
        }

SKBL [2009]

Three years ago, I did one of my first project that is used to secure .NET Applications. It is called SKBL – Serial Key Builder Library. It works in both Visual Basic and C#.

You may download it here! (The documentaion file is included.)
SKBL Setup

If you have some questions, please ask me at “artem.los at nilssons.ws”

Happy New Year!
Artem Los.

Tip of 22 December

In this thread I will show you two good Java Script functions. With both of them you can calculate how many chars you have.

The first one will calculate a specific element.

function calcspec(element)
{
	if (element.innerHTML != "")  // this function must be first!
	{
		return element.innerHTML.length;
	}

	if (element.value.length > 0)
	{
		return element.value.length;
	}
}

 

The second one will calculate all chars between HTML tags. HTML tags are not included.

function calcall(tag)
{
	return document.documentElement.innerHTML.length;
}

or

function calchtml()
{
	return document.getElementsByTagName("html")[0].innerHTML.length;
}

 

You might however replace innerHTML to outerHTML as shown bellow. This will therefore calculate all document. (does not work in Firefox)

function calcall(tag)
{
	return document.documentElement.outerHTML.length;
}
function calchtml()
{
	return document.getElementsByTagName("html")[0].outerHTML.length;
}

Another example of an encryption algorithm [JS]

In this post I will go through a simple encryption method I’ve done few years ago. It is actually so simple, but I’ve heard that some currently-used encryption algorithms are based on this principle.

Here is the code: (function “ec_t” – encryption method; function “dc_t” – decryption method)

/*
 * Sample encryption and
 * decryption by Artem Los
 * 
 */ 

  //Encrypt
    function ec_t(t, key) {
        var c = "";
        var d = "";
        var _key2 = StringToAsc(key);
        var e = genKey(_key2 , t.length);
        var _keyA =  new String (key.length);

        for (i = 0; i < t.length; i++) {

            var _def_t = eval(t.charCodeAt(i));
            var _def_e = eval(e.charAt(i));
            var _def_a = eval(_keyA.charAt(0));

            c = _def_e + _def_t +_def_a;
            d += String.fromCharCode (c);

        }
        return d;
    }

    function genKey(key, l) {
        var _vectorA = key;
        while (_vectorA.length < l) {
            _vectorA += _vectorA;
        }
        return _vectorA;
    }
    function StringToAsc(text) {
        var _vectorA = "";

        for (i = 0; i < text.length; i++) {
            _vectorA +=  text.charCodeAt(i);
        }
        return _vectorA;
    }

    //Decrypt
    function dc_t(t, key) {
        var c = "";
        var d = "";
        var _key2 = StringToAsc(key);
        var _keyA = new String(key.length);
        var e = genKey(_key2, t.length);

        for (i = 0; i < t.length; i++) {

            c = t.charCodeAt(i) - e.charAt(i) - _keyA.charAt (0);
            d +=  String.fromCharCode (c);
        }
        return d;

    }

    function do_e() {

        var _vecB = unescape (  in01.value);
        var _passA = pass.value;

        if (_passA != "") {
            if (ra01.checked == true) {
                var _vecC = ec_t(_vecB, _passA );
                in02.value = escape ( _vecC);
            }

            else {
                    var _vecC = dc_t(_vecB , _passA);
                    in02.value = _vecC;
            }
        }
    }

If you want to view this code in-work, enter the link below:
Encrypter

Why and how did it work?
The code above works on a simple principle, I discovered it without looking at internet, though! šŸ˜›

Say we have a table where all letters have an id, i.e., a number. The table looks as follows: A-1, B-2, C-3 …
First of all, we need to define our message, say we have “BAC” corresponding to “213”.
Then we need a key, say “CBA” – “321”. (it is in the same format as the message).

B(2) A(1) C(3)
C(3) B(2) A(1)
D(4) C(3) E(5)

The result is therefore 435 or DCE. When we decrypt we have to do the same but backwards, using the key, i.e., instead of addition use subtraction.

This is done with function “ec_t” and “dc_t”.
If the message is longer than the key, then we need to duplicate the key so it matches the message length. All that is done in function “getKey”.

It is better that you use function “do_e” instead of “ec_t”, because then all letters will go through a escape function, which means that the text will be more copy-friendly, i.e., easier to copy without missing some letters which can affect the result.

Good luck!

Create a HTML document

Tips of 23 November [JS]

So, today I will show some useful codes that might be used for protecting information with Java Script.

1. First of all, the most common protection method is to disable the right-clicking, i.e. disallow user to copy stuff from a webpage.

   
   /*
    * This script is create by Artem Los
    * Copyright (C) Artem Los, All rights reserved
    * You may use this code in commercial and
    * personal usage. You have no rights to
    * sell this code.
    * NOTE: Author must have credits for this code.
    */

    var r = "True";
    var text = "This site is protected!";

function IE(e) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        if (event.button == "2" || event.button == "3") {
             alert(text);
             return false;
          }
     }
}
function NS(e) {
    if (document.layers || document.getElementById && !document.all) {
        if (e.which == "2" || e.which == "3") {
            alert(text);
        }
        else if (e.which == "1") {
        if (r == "True") { return false; }
        }
    }
    return false;
}

document.onselectstart = new Function('if(r=="True"){return false;}');document.onmousedown = IE;document.onmouseup = NS;document.oncontextmenu = new Function("return false");

So, by using the code above you can be ensured that your information is safe at a basic level. It can still be taken by looking at the source, and copying it therefrom.
I will try to publish another code that actually protects the whole website, but remember that even though you got your page encrypted, it might still be cracked. Like with all kinds of protections, it is all about the time!

2. Another tip that I will talk about is links. It is so often that robots takes the, for instance, email address, and use it for spamming. However, we may solve this issue! Here I’ve got to methods that may help you to protect your links.

 /*
    * This script is create by Artem Los
    * Copyright (C) Artem Los, All rights reserved
    * You may use this code in commercial and
    * personal usage. You have no rights to
    * sell this code.
    * NOTE: Author must have credits for this code.
    */

    var AllLinks = new Array();
    AllLinks[0] = "http://www.clizware.net/";
function email(x) {

    if (x != "" || "undefined") {
        window.location = AllLinks [x];
        var a = document.getElementById("elink" + x);

    }
    else {
        alert("This link does not exist!");
    }

In the body of the page, enter:

<a href ="#" onclick ="email(0);" id="elink1">Link</a>

To improve this code, you can for example encrypt the link. I would recommend the escape/unescape function, but It actually does not matter. Once again, it’s all about the time.

The second method is not so useful, though. It is only working in Internet Explorer, and it have actually now function, just to hide the status bar.

   /*
    * This script is create by Artem Los
    * Copyright (C) Artem Los, All rights reserved
    * You may use this code in commercial and
    * personal usage. You have no rights to
    * sell this code.
    * NOTE: Author must have credits for this code.
    */

    window.defaultStatus = "All links are protected";

Tips of the day 21 November 2011

Welcome to Tips of the day! Here our some cssĀ components people asks about.
Code 1:

/* This code will effect the whole page */

*
{
/* Code here*/
}

Code 2:

/* This will place an object in center */

margin:auto;
padding:auto;

Code 3:

/* This will reset basic browser stuff */

margin:0;
padding:0;
border:0;

/* usually placed between *{ /* here */} */

Straddling Checkerboard [Java Script]

This tutorial will show you how you can encrypt/decrypt text (string) with Java Script or JScript.

First we need a table, even called for matrix. To create a matrix I chose to combine three array-lists into one single. (Note that it also works without the combining of arrays!)

        //generating 3 arrays
        var _matrixA = new Array();
        var _matrixB = new Array();
        var _matrixC = new Array();
        var _mA, _mB;

        _matrixA = new Array("A", "", "", "B", "C", "D", "E", "F", "G", "H");   // 0
        _matrixB = new Array("I", "J", "K", "L", "M", "N", "O", "P", "Q", "R"); // 1
        _matrixC = new Array("S", "T", "U", "V", "W", "X", "Y", "Z", ".", "/"); // 2

        //assign values of columns
        _mA = 1;
        _mB = 2;

        //combining matrix
        var _matrix = new Array(_matrixA, _matrixB, _matrixC); //this is the table, to get data, use _matrix[x][y]

Encryption Method:

        // Encryption method
        function encrypt(_text) {
        	var _returnVal = "";
            for (i = 0; i < _text.length; i++) {

                if (_matrixA.indexOf(_text.charAt(i)) != -1) {

                    _returnVal += (_matrixA.indexOf(_text.charAt(i)));

                }
                if (_matrixB.indexOf(_text.charAt(i)) != -1) {
                    _returnVal += (_mA); //default 1
                    _returnVal += (_matrixB.indexOf(_text.charAt(i)));

                }
                if (_matrixC.indexOf(_text.charAt(i)) != -1) {
                    _returnVal += (_mB); //default 2
                    _returnVal += (_matrixC.indexOf(_text.charAt(i)));

                }

            }
            return _returnVal;

        }

Decryption Method:

        // Decryption Method
        function decrypt(_text)
        {
        	var _returnVal = "";
        	for (i=0; i < _text.length; i++)
        	{
        		if (_text.charAt(i) != _mA || _text.charAt(i) != _mB )//the char has to be in 'first' matrix, _matrixA
        		{
        			_returnVal += _matrixA[_text.charAt(i)];
        		}

        		if (_text.charAt(i) == _mA) //the char has to be in 'secend' matrix, _matrixB
        		{
        			i++;
        			_returnVal += _matrixB[_text.charAt(i)];
        		}

        		if (_text.charAt(i) == _mB) //the char has to be in 'third' matrix, _matrixC
        		{
        			i++;
        			_returnVal += _matrixC[_text.charAt(i)];
        		}

        	}

        	return _returnVal;
        }

Copyright (C) 2011 Artem Los

CRC32 Method [VB.NET]

Please take a look over the code I’ve founded atĀ http://dotnet-snippets.com/dns/calculate-crc32-hash-from-file-SID587.aspx.
The author of this code is Tim Hartwig. If you want to read more about the code/algorithm, please go here!

' This code have to be included in the header of file
Imports System.IO
Public Function GetCRC32(ByVal sFileName As String) As String
    Try
        Dim FS As FileStream = New FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
        Dim CRC32Result As Integer = &HFFFFFFFF
        Dim Buffer(4096) As Byte
        Dim ReadSize As Integer = 4096
        Dim Count As Integer = FS.Read(Buffer, 0, ReadSize)
        Dim CRC32Table(256) As Integer
        Dim DWPolynomial As Integer = &HEDB88320
        Dim DWCRC As Integer
        Dim i As Integer, j As Integer, n As Integer

        'Create CRC32 Table
        For i = 0 To 255
            DWCRC = i
            For j = 8 To 1 Step -1
                If (DWCRC And 1) Then
                    DWCRC = ((DWCRC And &HFFFFFFFE)  2&) And &H7FFFFFFF
                    DWCRC = DWCRC Xor DWPolynomial
                Else
                    DWCRC = ((DWCRC And &HFFFFFFFE)  2&) And &H7FFFFFFF
                End If
            Next j
            CRC32Table(i) = DWCRC
        Next i

        'Calcualting CRC32 Hash
        Do While (Count > 0)
            For i = 0 To Count - 1
                n = (CRC32Result And &HFF) Xor Buffer(i)
                CRC32Result = ((CRC32Result And &HFFFFFF00)  &H100) And &HFFFFFF
                CRC32Result = CRC32Result Xor CRC32Table(n)
            Next i
            Count = FS.Read(Buffer, 0, ReadSize)
        Loop
        Return Hex(Not (CRC32Result))
    Catch ex As Exception
        Return ""
    End Try
End Function
Page 4 of 4:« First« 1 2 3 4