- Joined
- Sep 16, 2012
- Messages
- 453
- Reaction score
- 211
- First Language
- English
- Primarily Uses
- RMMV
This thread is an exercise to learn more about encryption and improve my experiments the purpose is not to make a solid JavaScript encryption method. Anyway my latest experiment involves a dual pass method, a string and a number. The string pass can take virtually any character (giving it thousands of options for just a single character). The number can be any number in JavaScript (including negative and floating point numbers).
The decrypt function can also be used to encrypt and the encrypt function to decrypt. Now while my description above might sound like this is a good method for encryption it has at least one major flaw. Assuming one knows the number one could potentially figure out a password that works.
As you can see the passwords do not match in this case but it still decrypts the string successfully. Granted this is harder to pull off with longer passwords but it is still possible. I am interested to see how the above functions could be improved (namely to remove the aforementioned flaw).
Yes this actually does decrypt into an actual sentence.
Code:
function encrypt(str, en, num) {
var str2 = '', c1;
for (let i = 0; i < str.length; i++) {
c1 = str.charCodeAt(i);
for (let p = 0; p < en.length; p++) {
c1 = c1 + en.charCodeAt(p) - num;
}
str2 += String.fromCharCode(c1);
}
return str2;
};
function decrypt(str, en, num) {
var str2 = '', c1;
for (let i = 0; i < str.length; i++) {
c1 = str.charCodeAt(i);
for (let p = 0; p < en.length; p++) {
c1 = c1 - en.charCodeAt(p) + num;
}
str2 += String.fromCharCode(c1);
}
return str2
};
Code:
var string = encrypt('Hello World', 'b1', 900);
//=> "陸藺立立狀嶺梨狀什立璘"
decrypt(string, 'c0', 900);
//=> "Hello World"
Code:
""
-289110
Try doing it without using the pass though. (>_<)
Try doing it without using the pass though. (>_<)
Code:
""

