View Full Version : rot13 Encrypting

12-28-2011, 02:40 AM
Hey, I've got this script that aparantly encrypts plain text to rot13. Here it is:

<script type="text/javascript">
function rot13(txt) {

var map = []
var tmp = "abcdefghijklmnopqrstuvwxyz"
var buf = ""

for (j = 0; j < tmp.length; j++) {
var x = tmp.charAt(j); var y = tmp.charAt((j + 13) % 26)
map[x] = y; map[x.toUpperCase()] = y.toUpperCase()

for (j = 0; j < txt.length; j++) {
var c = txt.charAt(j)
buf += (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' ? map[c] : c)

return buf

<script>var foo = rot13('<?php rec9(); ?>')</script>

I won't lie, my Javascript is terrible. I need to embed that script into a page, and have it encrpyt a PHP variable. I have no idea how because the script is assuming you're loading it externally. Can somebody help reconstruct the script so I can easily parse a PHP variable into it, and then output the result?

I really do need help, I've spent quite some time attempting and I've got pretty much nowhere. :thumbsup:

<?php rec9(); ?> rec9(); is a function in PHP which retrieves whatever the user posted in the textbox (we can just assume that code really mean "12345") - as you can see, I've attempted to parse it but I just don't know where to start. Hopefully I haven't scrambled it too much so somebody who knows Javascript can't help.

I really appreciate any help.

12-28-2011, 04:03 AM
I don't know much about PHP, but here is a much simpler version of the encryption/decryption function.
See: http://jsfromhell.com/string/rot13

<title> Rot13 Encryption</title>
<script type="text/javascript">
// From: http://jsfromhell.com/string/rot13

String.prototype.rot13 = function(){
return this.replace(/[a-zA-Z]/g, function(c){
return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);

function codeDcode() {
var s = document.getElementById('src').value;
var e = s.rot13();
document.getElementById('encrypt').value = e;
document.getElementById('decrypt').value = e.rot13();
<textarea id="src"></textarea><br>
<button onclick="codeDcode()">Code &amp; Decode</button><br>
<textarea id="encrypt"></textarea><br>
<textarea id="decrypt"></textarea>

12-28-2011, 04:24 AM
I genuinely don't think I could thank you enough. You've saved me big time mate, thanks a lot.

12-28-2011, 04:40 AM
You're most welcome.
Happy to help.
Good Luck!