1

我有一个格式如下的字符串:-----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 类,但也无法弄清楚该方法。谢谢!

4

5 回答 5

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

不优雅,但它清楚地表明了正在发生的事情。

另一种方法是使用 aScanner读取每一行并决定如何处理它。三行——头、尾和空行——将进行特殊处理并影响处理。否则,只需在阅读时将每一行附加到StringBuffer.

于 2013-04-10T00:36:12.443 回答
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-----

是吗?

于 2013-04-10T00:34:23.580 回答
0

newline?

-----BEGIN MESSAGE-----并从第一个值和-----END MESSAGE-----最后一个值中删除。

于 2013-04-10T00:26:23.097 回答
0
String[] parts = string.split("\r?\n");
sessionKey = parts[1];
encryptedMessage = parts[3]; 
digitalSignature = parts[5]; 

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

于 2013-04-10T00:29:38.313 回答
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
于 2013-04-10T00:34:34.813 回答