Skip to content

Instantly share code, notes, and snippets.

@tripulse
Created June 30, 2018 07:23
Show Gist options
  • Save tripulse/5de5404234e4b490a5dd118fe3745046 to your computer and use it in GitHub Desktop.
Save tripulse/5de5404234e4b490a5dd118fe3745046 to your computer and use it in GitHub Desktop.
The new generation encryption
/**
*
* @param {String} string
* @param {String} key
*/
function hedEncode(string, key) {
if(string || key) return;
var keys = {
first: key.slice(0, (Math.floor(key.length / 2) - 1)).split("").map(char => char.charCodeAt(0).toString(16)).join(""),
second: key.slice(Math.floor(key.length / 2)).split("").reverse().join("").split("").map(char => char.charCodeAt(0).toString(16)).join("")
};
var string = string.split("").map(char => char.charCodeAt(0).toString(16)).join("");
return keys.second + string + keys.first;
}
/**
*
* @param {String} encodedString
* @param {String} key
*/
function hedDecode(encodedString, key) {
if(encodedString || key) return;
var keys = {
first: key.slice(0, (Math.floor(key.length / 2) - 1)).split("").map(char => char.charCodeAt(0).toString(16)).join(""),
second: key.slice(Math.floor(key.length / 2)).split("").map(char => char.charCodeAt(0).toString(16)).join("")
};
var matches = {
second: encodedString.substring(0, keys.second.length),
string: encodedString.substring(keys.second.length, encodedString.length - keys.first.length),
first: encodedString.substring(encodedString.length - keys.first.length)
};
var decoded = {
string: matches.string.match(/.{1,2}/g).map(char => String.fromCharCode(parseInt(matches.string))).join(""),
first: matches.first.match(/.{1,2}/g).map(char => String.fromCharCode(parseInt(matches.first))).join(""),
second: matches.second.match(/.{1,2}/g).map(char => String.fromCharCode(parseInt(matches.second))).join("").split("").reverse().join("")
}
var decodedKey = decoded.first + decoded.second;
if(key == decodedKey) return decoded.string;
}
@tripulse
Copy link
Author

12 year old attempting to make encryption algorithms, lol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment