0

For example i have this text

"!?vake 7EnEebjP8jXf JFyd5hpIVa6B  !?vake".

The starting and ending keyword is "!?vake" and i want to retrieve the encrypted content between the keywords,decrypt it and replace it.

The html code before replacing would be:

<span class="messageBody" data-ft="{&quot;type&quot;:3}">‎!?vake 7EnEebjP8jXf JFyd5hpIVa6B
Fu63LH23dAiB !?vake</span>

and after decrypting :

<span class="messageBody" data-ft="{&quot;type&quot;:3}">‎i am the decrypted text</span>

Replace should work in the whole html document without knowing the specific element the encrypted text is.

vkefallinos
  • 717
  • 1
  • 9
  • 24
  • You want to "decrypt" text in possibly every single node of the DOM? Doesn't that seem like a bad idea? – Andrew Whitaker Dec 29 '11 at 16:26
  • 2
    Why are you doing it this way? Since it's JS, that means you're doing the decryption on the client, which means you've sent the encrypted data AS WELL as the method to decrypt it to the client. You've installed a secure bank vault and then taped the key to the door... – Marc B Dec 29 '11 at 16:28
  • this reminds of one of those "can geico save you money on car insurance? was XYZ a bad idea?" commercials. – jbabey Dec 29 '11 at 16:58
  • 1
    @MarcB, your comment is not applicable; the OP does not control the target page. This is a *userscript* application/question. – Brock Adams Dec 29 '11 at 21:12
  • @MarcB The encryption and decryption scripts run on the user's browser using greasemonkey.The encryption and decryption keys are going to be stored on a user trusted space and are going to be called using an xmlhttprequest.So the key is not on the door. – vkefallinos Dec 29 '11 at 22:45

3 Answers3

2

You can achieve this using RegExp. See a demo here : http://jsfiddle.net/diode/KVqJS/4/

var body = $("body").html();

var matched;
while ((matched = body.match(/!\?vake (.*?) !\?vake/))) {
    if(matched.length > 1){
        body = body.replace(/\!\?vake (.*) \!\?vake/, decode(matched[1]));
    }

}

$("body").html(body);

function decode(encoded) {
    return "decoded " + encoded;
}
Diode
  • 24,570
  • 8
  • 40
  • 51
  • i just tried your code and it works just fine.i just want to know how can !/vake can be replaced by a variable.I want to search for a set of keywords.This code is supposed to be run in a userscript for social networks like facebook so the encryption keys will be different for every user.So if i have 10 posts from different users on my wall ,there will be a set of ten different keywords to be found that will decrypt their text with 10 different keys. – vkefallinos Dec 29 '11 at 23:29
  • i also would like some links to read about regexp so i can learn.thank you – vkefallinos Dec 29 '11 at 23:31
  • 1
    Here is a regex tutorial for JS: http://www.regular-expressions.info/javascript.html And here is a site to help you test your regex: http://gskinner.com/RegExr/ (and one specifically for the preg functions in PHP, if you use it, is http://www.fullonrobotchubby.co.uk/random/preg_tester/ ) – Grim... Dec 30 '11 at 09:38
  • 1
    @vkefallinos : Plenty of resources are available online to learn RegExp. To use a variable in a RegExp you have to use new RegExp() in Javascript. Please see this thread. http://stackoverflow.com/questions/487509/passing-variable-to-a-regexp-in-javascript. – Diode Dec 30 '11 at 09:43
1

You can use regex to find (and replace) the data.

Here is a quick and dirty JSFiddle: http://jsfiddle.net/z8rVc/1/

I'm sure someone will come up with a more elegant method, however.

Grim...
  • 16,518
  • 7
  • 45
  • 61
  • this works too.thanks var code = $("body").html().match(/!\?vake (.*?) !\?vake/); var allcode = code[0]; var innercode = code[1]; var decryptcode = innercode + 'decrypted'; var newCode = $("body").html().replace(allcode, decryptcode); $("body").html(newCode); alert (decryptcode); – vkefallinos Dec 29 '11 at 23:41
0

You can use the following regex search and replace code in JS:

var a = "!?vake 7EnEebjP8jXf JFyd5hpIVa6B  !?vake";
var b = a.replace(/\!\?vake(.*)\!\?vake/g, '$1');
/// b contains your string between the keywords which you can decrypt as required.
GeekTantra
  • 11,580
  • 6
  • 41
  • 55
  • then you can run your secure decryption function on the variable "b". oh wait. – jbabey Dec 29 '11 at 16:59
  • @GeekTantra Using your code someone has to know already the encrypted text.I want to search for encrypted text thats between some keywords,find it,decrypt and replace it. – vkefallinos Dec 29 '11 at 22:49
  • You will need to do it across all nodes using jQuery. Its a performance degrader, moreover decryption at the client side is not recommended. – GeekTantra Dec 30 '11 at 02:55