Skip to content

Instantly share code, notes, and snippets.

@alterebro
Last active March 25, 2016 11:30
Show Gist options
  • Save alterebro/7483083 to your computer and use it in GitHub Desktop.
Save alterebro/7483083 to your computer and use it in GitHub Desktop.
Encode a string flipping and rot47ing it
(function() {
var obfuscate = {
flip : function(x) {
var flipped = '';
for ( var i=(x.length)-1; i >= 0; i--) {
flipped += x[i];
};
return flipped;
},
// http://rot47.net/_js/rot47.js
rot47 : function(x) {
var s = [];
for (var i = 0; i < x.length; i ++) {
var j = x.charCodeAt(i);
if ((j >= 33) && (j <= 126)) {
s[i] = String.fromCharCode(33 + ((j + 14) % 94));
} else {
s[i] = String.fromCharCode(j);
}
}
return s.join('');
},
encode : function(str) {
return this.rot47( this.flip(str) );
},
decode : function(str) {
return this.flip( this.rot47(str) );
}
}
String.prototype.obfuscate = function() {
return {
flip : obfuscate.flip(this),
rot47 : obfuscate.rot47(this),
encode : obfuscate.encode(this),
decode : obfuscate.decode(this)
}
}
})();
var str = 'This is a sample';
var encoded = str.obfuscate().encode;
console.log( encoded );
console.log( encoded.obfuscate().decode );
// Convert to entities
function entitizy(x) {
var out_str = '';
for (var i = 0; i < x.length; i++) {
out_str += '&#' + x.charCodeAt( i ) + ';';
}
return out_str;
}
// Custom rot31
function r(s) {
return s.replace(/[A-Za-z0-9]/g, function (c) {
return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".charAt(
"FGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDE".indexOf(c)
);
} );
}
console.log( r('Lorem ipsum dolor sit amet') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment