在Java中使用带有HTML标签内容的Regex和AES解密 [英] Using Regex and AES decryption in Java with HTML tag content

查看:73
本文介绍了在Java中使用带有HTML标签内容的Regex和AES解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个学校项目,其中HTML文档的文本内容被加密而不更改其布局.然后,加密的HTML文件将发送到Android设备,并在其中解密并显示.
内容存储在成对的标签中,例如:< span style = ...>(text_to_get)</span>".我的想法是使用Regex检索(1)并将每个文本部分替换为加密/解密的文本(2).感谢此源 [

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)如何用Java编写此方法?
(2)C#和Java中的AES是2种不同的方式.我使用我定义的所有参数在C#中加密了文本,这很重要.我搜索了Java AES解密,但是没有一个像C#中那样使用此类参数,因为密钥是自动生成的.如果可以帮助使用Java解密代码,我可以更改C#加密代码,但是现在我还没有任何解决方案.

有人可以帮我解决这两个问题吗?非常感谢.

更新:问题2已解决.对于问题1,我认为以下测试代码有效:


(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=" + m.group(1) + ">" + aesInstance.doDecrypt(m.group(2)) + "</span>");
        }
        m.appendTail(sb);



无论如何,感谢您对我的问题的关注.



Anyway, thanks for ur attention to my questions.

推荐答案



您可以使用Java密码学扩展(JCE)来传递特殊参数,如C#中一样,
编写方法是您的功课;).

http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136007.html [ ^ ]

http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#Key [
Hi,

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

http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136007.html[^]

http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#Key[^]

Does it help?

Best Regards


这篇关于在Java中使用带有HTML标签内容的Regex和AES解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆