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!
Leave a Reply