用于base64编码的crt文件的正则表达式

时间:2014-10-05 21:15:01

标签: c# regex certificate x509

我正在尝试使用c#中的regex从x509 .crt文件接收base64证书(没有 ----- BEGIN / END CERTIFICATE ----- )。 但没有成功。 我正在使用链式证书。 我做错了什么? 这是我使用的正则表达式:

  

:*(([\ NA-ZA-Z0-9 + /] {4}?):[A-ZA-Z0-9 + / {2} == | [A-ZA-z0- 9 + /] {3} =)

输入:

-----BEGIN CERTIFICATE-----

MIIGzDCCBLSgAwIBAgIBATANBgkqhkiG9w0BAQUFADCBpjELMAkGA1UEBhMCVVMx

DjAMBgNVBAgTBVRleGFzMRQwEgYDVQQHEwtTYW4gQW50b25pbzEaMBgGA1UEChMR

R2xvYmFsU0NBUEUsIEluYy4xFDASBgNVBAsTC0VuZ2luZWVyaW5nMRUwEwYDVQQD

EwxtaWtlLXJvb3QtY2ExKDAmBgkqhkiG9w0BCQEWGW1oYW1iaWRnZUBnbG9iYWxz

Y2FwZS5jb20wHhcNMTAxMTE4MjEyMzA4WhcNMTUxMTE3MjEyMzA4WjCBmDELMAkG

A1UEBhMCVVMxDjAMBgNVBAgTBVRleGFzMRowGAYDVQQKExFHbG9iYWxTQ0FQRSwg

SW5jLjEUMBIGA1UECxMLRW5naW5lZXJpbmcxHTAbBgNVBAMTFG1pa2UtaW50ZXJt

ZWRpYXRlLWNhMSgwJgYJKoZIhvcNAQkBFhltaGFtYmlkZ2VAZ2xvYmFsc2NhcGUu

Y29tMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAsF+vlQfZnssDsqFx

IXCGHST1jiTHJGGHiiwc9Qb1NPDbyvcdNXvcfkdyYjd8VlYyo3/jnj6xx3PxzJhG

NmnBGJ0I7h/RFJaG7nmGfeWUHCLsVjGfQeEjC++d6zzE3unPOiLVIhv9abD6kISa

hLdltOBcT19mqg1yG4Q4XExjeYmSYGFiDIdv+WwwUssTyPdppaaWcsjNaFcmuopU

RfmcfULPFwvFN6LsgvSTYwYe9l8421fA5c+WiR1JomjGuJT/0sITpzQRCenWi0S0

WZuftT61+fU0/OxINhgO4yK6C1eOoaxmoEG2oVm2o4Bjy9ceYN2UqdRGt8t/23/h

Wog3vEwdoHqghrjeiGWWs98qfzINKokiMd7APcxdkZ1SzyvOEWht4V3/XedleiMx

8WGjbVtRg/k4Hgf2TGwxcw==

-----END CERTIFICATE-----

-----BEGIN CERTIFICATE-----

MIIG4jCCBMqgAwIBAgIJAJjguYVnU08GMA0GCSqGSIb3DQEBBQUAMIGmMQswCQYD

VQQGEwJVUzEOMAwGA1UECBMFVGV4YXMxFDASBgNVBAcTC1NhbiBBbnRvbmlvMRow

GAYDVQQKExFHbG9iYWxTQ0FQRSwgSW5jLjEUMBIGA1UECxMLRW5naW5lZXJpbmcx

FTATBgNVBAMTDG1pa2Utcm9vdC1jYTEoMCYGCSqGSIb3DQEJARYZbWhhbWJpZGdl

QGdsb2JhbHNjYXBlLmNvbTAeFw0xMDExMTgyMTE5NDdaFw0xNTExMTcyMTE5NDda

MIGmMQswCQYDVQQGEwJVUzEOMAwGA1UECBMFVGV4YXMxFDASBgNVBAcTC1NhbiBB

bnRvbmlvMRowGAYDVQQKExFHbG9iYWxTQ0FQRSwgSW5jLjEUMBIGA1UECxMLRW5n

aW5lZXJpbmcxFTATBgNVBAMTDG1pa2Utcm9vdC1jYTEoMCYGCSqGSIb3DQEJARYZ

bWhhbWJpZGdlQGdsb2JhbHNjYXBlLmNvbTCCAiIwDQYJKoZIhvcNAQEBBQADggIP

xYK3mO1034kBdDxmVoBeEwfjWWPyC/uyFGwCNZCzoAQGMxNAnj33NBiCLHJRo1Z5

BxirSSMxOT4LEkmkOhuTyKB0TJZf+8wP8pK5BsO3xjO+uM1K3LY=

-----END CERTIFICATE-----

1 个答案:

答案 0 :(得分:3)

我只想编写一个在 START END 之间返回任何内容的正则表达式

var START = "-----BEGIN CERTIFICATE-----";
var END = "-----END CERTIFICATE-----";
var certs = Regex.Matches(DATA, START+ "(.+?)" + END,RegexOptions.Singleline)
                 .Cast<Match>()
                 .Select(m => Convert.FromBase64String(m.Groups[1].Value))
                 .ToList();