regex 从证书PEM字符串开始和结束的正则表达式

h5qlskok  于 2023-04-13  发布在  其他
关注(0)|答案(5)|浏览(218)

证书PEM文件包含如下开头和结尾:

-----BEGIN CERTIFICATE----- [Base64 of certificate] -----END CERTIFICATE

我只需要提取[Base64 of certificate]部分,也就是说,去掉"-----BEGIN CERTIFICATE----- "" -----END CERTIFICATE-----"字符串,我想知道这是否可以用正则表达式来完成,如果可以,正则表达式看起来会是什么样子?
我试过了

-----BEGIN CERTIFICATE----- (.*) -----END CERTIFICATE-----

但是,它不是给我证书的Base64,而是返回所有内容。
谢谢,吉姆

mepcadol

mepcadol1#

由于我不知道你使用的语言,我给予你一个相对可移植的模式(必须支持lookahead和lookbehind):

(?<=-----BEGIN CERTIFICATE----- )(?:\S+|\s(?!-----END CERTIFICATE-----))+(?=\s-----END CERTIFICATE-----)

结果是整个图案,因为查找只是检查。

yxyvkwin

yxyvkwin2#

在perl中:

my base64_cert_data;
if ($certbuf=~/(-+BEGIN CERTIFICATE-+)(.*?)(-+END CERTIFICATE-+)/s) {
    base64_cert_data = $2;
}

Regex解释:

/(-+BEGIN CERTIFICATE-+)(.*?)(-+END CERTIFICATE-+)/s

1st Capturing group (-+BEGIN CERTIFICATE-+)
    -+ matches the character - literally
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    BEGIN CERTIFICATE matches the characters BEGIN CERTIFICATE literally (case sensitive)
    -+ matches the character - literally
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
2nd Capturing group (.*?)
    .*? matches any character
        Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
3rd Capturing group (-+END CERTIFICATE-+)
    -+ matches the character - literally
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    END CERTIFICATE matches the characters END CERTIFICATE literally (case sensitive)
    -+ matches the character - literally
        Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
s modifier: single line. Dot matches newline characters
0ejtzxu1

0ejtzxu13#

下面是一个示例Perl代码来支持您的需求。

my $Str = "-----BEGIN CERTIFICATE-----
MIIBuTCCASKgAwIBAgIQNdNhtuV5GbNHYZsf+LvM0zANBgkqhkiG9w0BAQUFADAb
MRkwFwYDVQQDExBFZGlkZXYgU21va2VUZXN0MB4XDTA4MTExMjE5NTEzNVoXDTM5
MTIzMTIzNTk1OVowGzEZMBcGA1UEAxMQRWRpZGV2IFNtb2tlVGVzdDCBnzANBgkq
hkiG9w0BAQEFAAOBjQAwgYkCgYEAm6zGzqxejwswWTNLcSsa7P8xqODspX9VQBuq
5W1RoTgQ0LNR64+7ywLjH8+wrb/lB6QV7s2SFUiWDeduVesvMJkWtZ5zzQyl3iUa
CBpT4S5AaO3/wkYQSKdI108pXH7Aue0e/ZOwgEEX1N6OaPQn7AmAB4uq1h+ffw+r
RKNHqnsCAwEAATANBgkqhkiG9w0BAQUFAAOBgQCZmj+pgRsN6HpoICawK3XXNAmi
cgfQkailX9akIjD3xSCwEQx4nG6tZjTz30u4NoSffW7pch58SxuZQDqW5NsJcQNq
Ngo/dMoqqpXdi2/0BYEcJ8pjsngrFm+fM2BnyGpXH7aWuKsWjVFGlWlF+yi8I35Q
8wFJt2Z/XGA7WWDjvw==
-----END CERTIFICATE-----";
if($Str =~ /^\W+\w+\s+\w+\W+\s(.*)\s+\W+.*$/s) {
    print "$1" . "\n\n";
} else {
    print "No\n" . "\n\n";
}

输出:

MIIBuTCCASKgAwIBAgIQNdNhtuV5GbNHYZsf+LvM0zANBgkqhkiG9w0BAQUFADAb
MRkwFwYDVQQDExBFZGlkZXYgU21va2VUZXN0MB4XDTA4MTExMjE5NTEzNVoXDTM5
MTIzMTIzNTk1OVowGzEZMBcGA1UEAxMQRWRpZGV2IFNtb2tlVGVzdDCBnzANBgkq
hkiG9w0BAQEFAAOBjQAwgYkCgYEAm6zGzqxejwswWTNLcSsa7P8xqODspX9VQBuq
5W1RoTgQ0LNR64+7ywLjH8+wrb/lB6QV7s2SFUiWDeduVesvMJkWtZ5zzQyl3iUa
CBpT4S5AaO3/wkYQSKdI108pXH7Aue0e/ZOwgEEX1N6OaPQn7AmAB4uq1h+ffw+r
RKNHqnsCAwEAATANBgkqhkiG9w0BAQUFAAOBgQCZmj+pgRsN6HpoICawK3XXNAmi
cgfQkailX9akIjD3xSCwEQx4nG6tZjTz30u4NoSffW7pch58SxuZQDqW5NsJcQNq
Ngo/dMoqqpXdi2/0BYEcJ8pjsngrFm+fM2BnyGpXH7aWuKsWjVFGlWlF+yi8I35Q
8wFJt2Z/XGA7WWDjvw==
rekjcdws

rekjcdws4#

base64字符集是:
[A-Za-z0-9+/\r\n]+={0,2}-这是对PEM文件(base64证书)通常使用的内容的准确描述。=用于填充(在末尾),\r\n是新行字符。
把所有这些放在一起,我们得到:

"-+BEGIN\\s+.*CERTIFICATE[^-]*-+(?:\\s|\\r|\\n)+" // Header
 + "([A-Za-z0-9+/\r\n]+={0,2})"                   // Base64 text
 + "-+END\\s+.*CERTIFICATE[^-]*-+"                // Footer

如果你想成为语言无关的,你可以期望页眉/页脚是一个或多个-字符,后跟大写字母,再后跟一个或多个-字符。

wnrlj8wa

wnrlj8wa5#

下面是regex:

(?<=-----BEGIN CERTIFICATE-----)[\s\S]*?(?=-----END CERTIFICATE-----)

Java示例:

final String text = Files.readString("file.txt") // file with certs ;
        final List<String> results = new ArrayList<>();
        final Matcher matcher = Pattern.compile("(?<=-----BEGIN CERTIFICATE-----)[\\s\\S]*?(?=-----END CERTIFICATE-----)")
                .matcher(text);
        while (matcher.find()) {
            final String m = matcher.group()
                    .replaceAll("\\n", "")
                    .replaceAll("\\r", "");
            results.add(m);
        }

相关问题