Artem's blog

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

Archives for Old clizware.net blog

The right modulo

Recently, I’ve published an article here about the modular operator, and its defect. I’m not quite sure if the content is  hundred pro cents right, though. The previous post was about an encryption in Java Script, where I’ve used a certain function to get the modulo.

Number.prototype.mod = function(n) {
return ((this%n)+n)%n;
}

The prototype above is solving the entire problem. By writing a number, ex, “5.mod(3)” you will perform an modular operation 5 mod 3. The code itself is from http://javascript.about.com/od/problemsolving/a/modulobug.htm where you also might read more about that particular issue.

To see the code in work, please view this example: http://editor.clizware.net/?id=7

Tip of this weekend (25/2-12)

Another good method to encrypt ASCII based text. To view this example in work, http://editor.clizware.net/?id=7 (remember to press compile)

//Copyright (C) 2012 Artem Los, All rights reserved

function mod_c(text,key,enc_dec)
{
	//only for 128 ascii values
	if (key == "")
	{
		return "Error: no key given";
	}
	if (enc_dec === true)
	{
		var result = "";

		for (var i=0; i < text.length; i++)
		{
			result += String.fromCharCode( (text.charCodeAt(i) + key.charCodeAt(i % key.length)).mod(128)    );
		}
		return result;
	}
	else if(enc_dec === false)
	{
		var result = "";

		for (var i=0; i < text.length; i++)
		{
			result += String.fromCharCode( (text.charCodeAt(i) - key.charCodeAt(i % key.length)).mod(128)  );
		}
		return result;
	}
	else
	{
		return "Error: enc_dec is not given";
	}
}

Number.prototype.mod = function(n) {
return ((this%n)+n)%n;
}

Investigating mod

Almost all languages have operators. Example of these are, “+” – addition, “-” – subtraction, “*” – multiplication, “/” division. There are also more advanced operators such as “Mod”, “Exponent”, and “And”. Let us investigate the mod.

I have seen that there are much misunderstanding about mod. Some claims that it performs a modulo operation, some claims that it is the reminder. Actually it is a reminder, and not a modulo operation.

In this investigation, I chosen Visual Basic as the default language. So, let us try to use mod operation as a modulo operation.

'default mod, "reminder"
MsgBox(3 - 4 Mod 6) ' result is -1
MsgBox(-1 + 4 Mod 6) ' result is 3

I would like to add that there is no problem to get the same answer back, i.e., 3 -4 mod 6 is -1, and then by adding 4 and again mod, will result 3. However, that is not the modulo. To display the modulo, use the function below:

   Function mod_r(ByVal x As Integer, ByVal y As Integer) As Integer
        'the "real" modulo operation
        Return x - y * Int(x / y)
    End Function

In arrays, tables, indexes, where the index should be positive, we can use the function above.

        MsgBox(mod_r(3 - 4, 6)) ' result is 5
        MsgBox(mod_r(5 + 4, 6)) ' result is 3

Visual Studio 2012 is comming

 Visual Studio will soon be upgraded to a new version, 2012. It will be more integrated into the new Windows 8 operating system, with all designing features available in Windows. For designers, it will be more easier to only use XAML as a language for the UI. XAML is also integrated into other designing products of Microsoft, especially in Expression Blend. Visual Studio’s IDE is also changing a lot. Now, you will have the same designer features for all supported languages, for example, C#, C++, VB. XAML designer is a new designing tool that should make it easier to synchronize Visual Studio with Expression Blend.

Source:
http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-504T

.NET Languages, etc.

Currently, I am reading about the CLR (common language runtime), and I will briefly describe it.

CLR ease the programming process for all languages that target CLR. Languages like Visual Basic, Visual C#, Visual F#, et cetera, targets CLR. Actually, these languages are only a subset of those possibilities that are available in CLR. That means that you might not be available to express your programming ideas through only one language, or, that others won’t be able to use your work, simply because of that. For example, if you’ve created a dynamic link library in the language C#, which is case-sensitive, where the functions can be so similar that one capital letter will separate them, i.e., “static void Hello”, “static void hello” or “static void HELLO” (c#-static. vb- shared member) it will work fine inside the C# environment. But outside, ex, when the function is to be called outside that environment, for example through Visual Basic, it may cause an exception. Note, that is also because of that the member is set to static/shared. If the member would be private or only accessible inside that assembly, everything would work fine. A great tip for all .NET developers is to always write in CLS (common language specification). Just include this peace of code in your assembly:

[assembly:CLSCompliant(true)]

That will always check your code and it will report when it does not follow the CLS.

Private information lock

When you are working for instance, on a project that should be hidden from other eyes, it is a good idea to secure it. All that can be done by TrueCrypt.

TrueCrypt – is an encrypting software that provides you with all functions that are needed to secure your secret information. There are several main features, that are pretty useful:

  • Creating a virtual disc (a file that is your disc; where you can put other files.)
  • Encrypting entire partition, storage device such as USB, or hard drive.
  • Encrypting a partition that contains Windows. (pre-boot)
  • Creating a hidden volume.

(there are more features as well!)

After some few experiments, I would say that the best way to secure your files is by “Creating a virtual disc“. However, it depends on the purpose as well. If your entire computer is to be protected, it is better to “Encrypt the Windows partition” and all other internal discs on the computer. By doing this kind of encryption, you will be prompted to enter a secret password each time the Windows boots. The good thing is, if you get your computer stolen, the information will remain inaccessible for all except you. Correctly speaking, it will remain accessible for anyone with the password. If you suddenly forget the main password, you can say “bye” to the information of yours.

By creating a virtual drive, your information (folder system with all files) will remain safe in one big file (the size is assigned when creating it). When you will encrypt your entire drive (Windows partition) it is good to take a backup of all your information, because it might be something that causes data lose (eventually a bug).

There are also an opportunity to hide a volume. It is also a good advanced feature that is build into TrueCrypt.”The principle is that a TrueCrypt volume is created within another TrueCrypt volume (within the free space on the volume). Even when the outer volume is mounted, it should be impossible to prove whether there is a hidden volume within it or not*, because free space on any TrueCrypt volume is always filled with random data when the volume is created** and no part of the (dismounted) hidden volume can be distinguished from random data. Note that TrueCrypt does not modify the file system (information about free space, etc.) within the outer volume in any way.“, (read more).

TrueCrypt is free for use, and can be downloaded here. If you like it, please donate to keep this program active! http://www.truecrypt.org/

Spotify is growing fast

Spotify is a music streaming service that is available in a big part of Europe and USA. The software itself consist of different versions: a free – “Open”, an “Unlimited”, and “Premium”. One of the big differences in-between these version is the advertising. The Open version contains advertising and a limited amount of listening hours. The Unlimited version is as Premium, but without certain mobile features. (Read more here…).

As Financial Times reported, the amount of premium users has grown from 15% to 20% in Mars 2011, which means ca 2.5 million premium users.

More about spotify at spotify.com
Source: http://blogs.ft.com/fttechhub/2012/01/spotify-hits-3m-subscribers/#axzz1kbJWfv67

Chaos on the Web

The Web is changing all the time. From a technical perspective, more functions are added, which opens doors to new possibilities. From a developer perspective though, it is getting more expensive to test web pages in all browsers. Before, when Internet Explorer had more users, it was worth to only check the page in IE and still make it available for most of the web users. Now, when all old/new minor browsers are getting big, you cannot be sure that all will view the page as you want to. That means that more time is needed to make a page compatible for all browsers.

HTML5 – which should not be platform or web-browser based, is shown differently depending on web-browser, web-browser version and platform. With other words, a page on, for instance,  windows using Firefox, would not be displayed the same on a Firefox browser on Android.

The big issue with all kinds of web-browsers is that more time is needed to test a project, which might be expensive. Once again, if a project is to be tested in all web-browsers, it may cause project delay, or, to solve the first issue of time, it might require more web-developers, tester, which might therefore cost more.

I do not think that this issue may be solved in the nearest future, because we are getting more and more devices, such as smartphone, tablet-PC, TV, et cetera. Everything listed has an pre-build web-browser. In the current situation, we cannot take it for sure, that the web-page will be displayed correctly on all devices.

Encrypting a website

EDIT: Please check out the updated version of this article.

When encrypting a website, we need to understand the way a browser interprets our input, i.e. html/javascript. In this case, we can produce code that is only readable by a browser.

There are several ways to encrypt a page, the main idea behind all methods is it to convert a human-readable code to ONLY computer-readable one.

    • By HTML only
    • By JavaScript
      • Using hexadecimal values
      • Using “escape/unescape”
      • Using an advanced algarithm

By HTML Only
In HTML, all letters(chars) can be written in a ASCII form, i.e., if we have a small “a” that is to be converted, we simply look up its ASCII value and write it instead of the actual char: &#97;, and so on.To sum up, the prefix is “&#”, then the ASCII code, and lastly a semicolon, “;”.

function encToASCII(_text)
{
	var _newText="";

	for(var i=0; i < _text.length;i++)
	{
		_newText += "&#" + _text.charCodeAt(i) + ";";
	}

	return _newText;
}

 

By JavaScript
JavaScript website encryption can be divided into several parts. One way is by using the hexadecimal value of a char, and write it instead of the actual char. We have to use JavaScript to perform this action, as it is shown below:

function encToHex(_text)
{
	var _newText="";
	for (var i=0; i < _text.length; i++)
	{
		_newText += "%" + decimalToHex(_text.charCodeAt(i));
	}
	return _newText;
}
function decimalToHex(_num)
{
	return _num.toString(16);
}

This method will only encrypt the given text, though, but, the user has to be able to see the encrypted text as a result (the browser won’t understand this code unless we have a function that will interpret it). This method is based on JavaScript, and we therefore need to use JavaScript to decrypt. Let us say that we encrypt “Hello World!”, which results “%48%65%6c%6c%6f%20%57%6f%72%6c%64%21”. As you might see, the prefix is “%”, and then the hexadecimal number. If the result above is our webpage, the source of that webpage should look similar to the example below:

<script type="text/javascript">
document.write(unescape("%48%65%6c%6c%6f%20%57%6f%72%6c%64%21"));
</script>
NOTE: There is a big difference between the HTML Only encryption and JavaScript encryption. For example, if you encrypt your whole page with HTML Only encryption, including “tags”, the result in the web-browser will be the source of the page. Encoding “<p>Hi</p>” will result “<p>Hi</p>”. However, by using JavaScript, the result will be executed, as a normal page would. Encoding “<p>Hi</p>” will result “Hi”.


There is also, as you might see, an opportunity to use functions escape and unescape as they are. Their actual usage is to encode/decode the url, i.e., replacing spaces, “special chars.” to url friendly one. All letters and numbers will stay the same, they will not be changed.

document.write(escape("Hello World!"));

The result you will get is Hello%20World%21. It replaces spaces with %20, and “!” with %21. Both of them are special characters(chars).

Unescape function will decode to a normal string.

document.write(unescape("Hello%20World%21"));

 

By JavaScript – Advanced algorithm
It took a while for me to define what an advanced algorithm in our case would be. I would say that the more advanced algorithm you have, the more time it takes to “crack it”. The method that will be described in this section of the post is my own little version of advanced algorithm. The method is using a logical operation, called XOR. If you already now what that is, jump to the next section.XOR is an operator, and works at a binary level. A definition in words is that the first or second value is true, but not both. Truth table shown below: 

Input Output
A B
0 0 0
0 1 1
1 0 1
1 1 0
XOR Truth table

An operation, 5 XOR 6, would result 3. The thing is, if we take either 5 XOR 3 = 6 or 6 XOR 3 = 5, we always return to our previous numbers. Conclusion, if two numbers are known, we always will get the third one.

As you might see, the decimal value 5 corresponds to 101; 6 corresponds to 110; 3 corresponds to 011.
1 0 1
1 1 0
1 1 0

By using XOR operation, we might, for instance, set a secret number, or a series of numbers, which always will return our third number. In this post, we will go though how to use a series of numbers, but also how to do it with only one number.

function en_doc(t,k)
{
	var a;
	var b;
	var r=new Array();
	for (var i=0; i<t.length ;i++)
	{
		a=t.charCodeAt(i);
		b=k.charAt(i%k.length);
		a^=b;
		r[i]=String.fromCharCode(a);
	}
	return r.join("");
}

The code shown above is my own advanced algorithm. First, we declare “a”, which is a char at certain place in string t; and b, which is a digit at certain place in k. The text to encrypt is placed in t (all chars), and the key to encrypt with is placed in k (only digits). The variable r is an array of the result/output.

First of all, we assign a to the char code at i. The variable b is then assigned to the char at i mod the length of key.

The reason why I put  a modular operation there is simple. If the key length we use is smaller than the text length, we will not be able to encrypt the whole string without it. When we are encrypting something, the key has to be of the same length as the text. By using a modular operation, even if the key is smaller than the text, it will be repeated. For example, if the key is 567 (key length=3), and the text length is 6, the output key will result 567567.

Secondly, an logical operation is executed. In other words, variable a equals to itself XOR b. Variable a has therefore a new value which is later assigned to r[i], i.e., a place in the array r which corresponds to the index value i.

Lastly, we join our array without some joining char, and that will be our encoded text. To decrypt, use the same function; place your encrypted text in t, and the key in k.

You can, however, remove this key opportunity, or as I previously defined it, using a key series,  to a single key digit. You might still use this method, but only having a key, for instance, 6, or, you might remove it at all, and set the secret digit inside the function.

If you want to include this code in your project without typing the code above, include this code before all scripts (see this code in action):

Source 1:

<script src="https://dev.clizware.net/lib/scrypto1.js"></script>

Source 2:

<script src="https://clizware.webs.com/lib/scrypto1.js"></script>

I hope this article was useful, and that you hopefully have learned something new! Below you find some useful links:

Encrypter – (for html only encryption)
http://www.iwebtool.com/html_encrypter – (encrypting to hexadecimal values, javascript)
Bitwise Operators – (about logical operators)
Escape and Unescape
Eval – (advanced encryption, etc.)

This article was revised 17.03.2013.

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
Page 5 of 9:« First« 2 3 4 5 6 7 8 »Last »