从Java字符串解析文本

时间:2013-04-10 00:22:25

标签: java regex

我有一个以下格式的字符串:----- BEGIN MESSAGE -----,后跟一个可变长度的加密会话密钥,后面跟一个换行符,后跟一个加密的消息,后面跟一个换行符,然后是数字签名,然后是----- END MESSAGE -----。

-----BEGIN MESSAGE-----
SNyeWtz8QD8AKdioMG11wu7U6gG2wD9tekvVrx6VYW+6oJj4Wl8NE+7i5MHbu4Au
+vN1Z886lOWka7ekgPF8N7t9MpiFo2pBPHuFcOsaY5ETYuEyk5gaX7BYP7qT6wKG
BRILmX6DblWqGxG2tKs/AdcHDqQ5QBXrP03uhN68wgo=

U2FsdGVkX18gtpQSqyH4H5242SZzcZrb0oH7FWw7/MSCxo7h7BVaesZV2N38sr9y

kVr+wabiNn4RfAB4nNi9gAZHQLok4uxRMALGF2kZk2zpVNPQo6jcdz85fy68gylX
OCQIIdk8JPIwxzHfVvRZqNHDRADZRlNHUMYScjRPU+DB8avghYAVKMJhLgA/2Tdp
a59uBMBg/yB1yqA5FivxPzOhq92Y4nZuP1R9/yGE9O8K
-----END MESSAGE-----

解析三条信息(会话密钥,加密邮件和数字签名)的最佳方法是什么?

我尝试使用Scanner类,但我还没弄清楚要用什么作为分隔符。我也试过使用Pattern类,但也无法想出那个方法。谢谢!

5 个答案:

答案 0 :(得分:1)

对,像Sergii说的那样删除开头和结尾。然后针对“\ s +”进行正则表达式拆分 例如在.NET中:

Regex.Split(Regex.Replace(strCert, "(?i)\s*-{5}(BEGIN|END)\sMESSAGE-{5}\s*", ""), "\s+")

也就是说,假设您的示例在每个数据正文中具有单行错误的唯一原因是格式化,因为据我所知,实际证书中不存在这些。实际证书看起来像:

-----BEGIN MESSAGE-----
SNyeWtz8QD8AKdioMG11wu7U6gG2wD9tekvVrx6VYW+6oJj4Wl8NE+7i5MHbu4Au+vN1Z886lOWka7ekgPF8N7t9MpiFo2pBPHuFcOsaY5ETYuEyk5gaX7BYP7qT6wKGBRILmX6DblWqGxG2tKs/AdcHDqQ5QBXrP03uhN68wgo=

U2FsdGVkX18gtpQSqyH4H5242SZzcZrb0oH7FWw7/MSCxo7h7BVaesZV2N38sr9y

kVr+wabiNn4RfAB4nNi9gAZHQLok4uxRMALGF2kZk2zpVNPQo6jcdz85fy68gylXOCQIIdk8JPIwxzHfVvRZqNHDRADZRlNHUMYScjRPU+DB8avghYAVKMJhLgA/2Tdpa59uBMBg/yB1yqA5FivxPzOhq92Y4nZuP1R9/yGE9O8K
-----END MESSAGE-----

雅?

答案 1 :(得分:1)

您实际上在各个部分中嵌入了换行符。分隔它们的是空白行 - 连续两行。我假设您希望每个部分都删除换行符。我建议采用蛮力方法:

StringBuilder sb = new StringBuilder();
String[] parts = input.split("\\r?\\n\\r?\\n"); // should be 3 long
// strip out header and newlines from session key
String[] lines = parts[0].split("\\r?\\n");
for (int i = 1; i < lines.length; ++i) { // skip first line
    sb.append(lines[i]);
}
parts[0] = sb.toString();
// strip out header and newlines from message
sb.setLength(0);
lines = parts[1].split("\\r?\\n");
for (int i = 0; i < lines.length; ++i) {
    sb.append(lines[i]);
}
parts[1] = sb.toString();
// finally, deal with the signature
sb.setLength(0);
lines = parts[2].split("\\r?\\n");
for (int i = 0; i < lines.length - 1; ++i) {
    sb.append(lines[i]);
}
parts[2] = sb.toString();

不优雅,但它清楚地表明发生了什么。

另一种方法是使用Scanner读取每一行并决定如何处理它。三条线 - 标题,拖车和空行 - 将有特殊处理并影响处理。否则,只需在读取每一行时将其附加到StringBuffer

答案 2 :(得分:0)

newline

从第一个值中删除-----BEGIN MESSAGE-----,从上一个值中删除-----END MESSAGE-----

答案 3 :(得分:0)

String[] parts = string.split("\r?\n");
sessionKey = parts[1];
encryptedMessage = parts[3]; 
digitalSignature = parts[5]; 

\r?允许Windows EOL(\r\n)或Unix EOL(\n)。

答案 4 :(得分:0)

代码:

public class MessageParser {

   public static void main( String[] args ) {
      String message =
         "-----BEGIN MESSAGE-----\n" +
         "SNyeWtz8QD8AKdioMG11wu7U6gG2wD9tekvVrx6VYW+6oJj4Wl8NE+7i5MHbu4Au\n" +
         "+vN1Z886lOWka7ekgPF8N7t9MoiFo2pBPHuFcOsaY5ETYuEyk5gaX7BYP7qT6wKG\n" +
         "BRILmX6DblWqGxG2tKs/AdcHDqQ5QBXrP03uhN68wgo=\n" +
         "\n" +
         "U2FsdGVkX18gtpQSqyH4H5242gZzcZrb0oH7FWw7/MSCxo7h7BVaesZV2N38sr9y\n" +
         "\n" +
         "kVr+wabiNn4RfAB4nNi9gAZHQLok4uxRMALGF2kZk2zpVNPQo6jcdz85fy68gylX\n" +
         "OCQIIdk8JPIwxzHfVvRZqNHDRFDZRlNHUMYScjRPU+DB8avghYAVKMJhLgA/2Tdp\n" +
         "a59uBMBg/yB1yqA5FivxPzOhq92Y4nZuP1R9/yGE9O8K\n" +
         "-----END MESSAGE-----\n";
      String[] lines = message.split( "\n" );
      int i = 1;
      String sessionKey = "";
      String line = lines[i];
      while( i < lines.length && line.length() > 0 ) {
         sessionKey += line;
         line = lines[++i];
      }
      String encryptedMessage = "";
      line = lines[++i];
      while( i < lines.length && line.length() > 0 ) {
         encryptedMessage += line;
         line = lines[++i];
      }
      String digitalSignature = "";
      line = lines[++i];
      while( i < lines.length && ! line.equals( "-----END MESSAGE-----" )) {
         digitalSignature += line;
         line = lines[++i];
      }
      System.out.println( "sessionKey      : " + sessionKey );
      System.out.println( "encryptedMessage: " + encryptedMessage );
      System.out.println( "digitalSignature: " + digitalSignature );
   }
}

输出:

sessionKey      : SNyeWtz8QD8AKdioMG11wu7U6gG2wD9tekvVrx6VYW+6oJj4Wl8NE+7i5MHbu4Au+vN1Z886lOWka7ekgPF8N7t9MoiFo2pBPHuFcOsaY5ETYuEyk5gaX7BYP7qT6wKGBRILmX6DblWqGxG2tKs/AdcHDqQ5QBXrP03uhN68wgo=
encryptedMessage: U2FsdGVkX18gtpQSqyH4H5242gZzcZrb0oH7FWw7/MSCxo7h7BVaesZV2N38sr9y
digitalSignature: kVr+wabiNn4RfAB4nNi9gAZHQLok4uxRMALGF2kZk2zpVNPQo6jcdz85fy68gylXOCQIIdk8JPIwxzHfVvRZqNHDRFDZRlNHUMYScjRPU+DB8avghYAVKMJhLgA/2Tdpa59uBMBg/yB1yqA5FivxPzOhq92Y4nZuP1R9/yGE9O8K