Click here to Skip to main content
15,797,721 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a school project in which the text content of an HTML document is encrypted without changing its layout. Then the encrypted HTML file is sent to an Android device where it is decrypted and displayed.
The content is stored in pairs of tag , like this : "< span style=...>(text_to_get)< /span >". My idea is using Regex to retrieve (1) and to replace each text part with the encrypted/decrypted text (2). I finished the encryption part in C#, thanks to this source [^]. Now I get trouble in using regex and AES decryption in Java.
Here's the decryption code in C# :
public string DecryptSpanContent(AESEncryption aes)
            string text = Text;
            string pattern = "<span style=(?<style>.*?)>(?<content>.*?)</span>";
            text = Regex.Replace(text, pattern,
                                       m => "<span style=" + m.Groups["style"] + ">" + Decrypt(m.Groups["content"].Value) + "</span>");
            return text;

(1) How can I write this method in Java ?
(2) AES in C# and Java is 2 different way. I encrypted a text in C# with all the parameters defined by me, and this is important. I searched for the Java AES decryption but none uses such parameters, like in C#, as the key is generated automatically. I may change the C# encryption code if that could help work with the Java decryption code, but now I haven't got any solution yet.

Can anyone help me with these 2 problems, please? Thanks a lot.

Update: Problem 2 was solved. For the problem 1, I suppose the test code below works:
Pattern pattern = Pattern.compile("<span style=(.*?)>(.+?)</span>");
        Matcher m = pattern.matcher(the_html_text);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(sb, "<span style=" + + ">" + aesInstance.doDecrypt( + "</span>");

Anyway, thanks for ur attention to my questions.
Updated 15-May-12 5:12am

1 solution


you can use Java Cryptography Extension (JCE) to pass special parameters as in C#,
writing the method is your homework ;).[^][^]

Does it help?

Best Regards
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900