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.

5
  • You want to "decrypt" text in possibly every single node of the DOM? Doesn't that seem like a bad idea? Commented Dec 29, 2011 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... Commented Dec 29, 2011 at 16:28
  • this reminds of one of those "can geico save you money on car insurance? was XYZ a bad idea?" commercials. Commented Dec 29, 2011 at 16:58
  • 1
    @MarcB, your comment is not applicable; the OP does not control the target page. This is a userscript application/question. Commented Dec 29, 2011 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. Commented Dec 29, 2011 at 22:45

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

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.
i also would like some links to read about regexp so i can learn.thank you
Here is a regex tutorial for JS: regular-expressions.info/javascript.html And here is a site to help you test your regex: gskinner.com/RegExr (and one specifically for the preg functions in PHP, if you use it, is fullonrobotchubby.co.uk/random/preg_tester )
@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. stackoverflow.com/questions/487509/….
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.

1 Comment

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);
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.

3 Comments

then you can run your secure decryption function on the variable "b". oh wait.
@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.
You will need to do it across all nodes using jQuery. Its a performance degrader, moreover decryption at the client side is not recommended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.