RSA 개인 키와 인증서가 한 줄에 있는데 이를 .key 및 .crt 파일로 분할해야 합니다.
sed 또는/및 awk가 작동할 수 있나요?
EDI1: 예(보시다시피 실제로는 한 줄입니다)
-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAoigkOEwqD/d5VsRgCZx8ZjTG+2lMZ+q1QyRaOXZ0OXIFWoKq JlFKI2BauRYl83pM1CLcwrdmnPGGyU9ld5JkWgj9zwI5O7kvyl0sW9ekiDGRdyT+ AEGV5E5aCiki9hgrj61CyvGV4audP3jG+dNi07yFnm40uC1HkrKtFDgiTYMDb2L4 m/UP8pszFiar9rBY0AEhL55gLREX4a/QDxbmP5puUvTErgt0mfbe444+ke5fxxTZ BKhZbVhaPEVVcUVgPlw1EH9wmgj4fTnlNDZBPVqvej8CEt4y/RcEOZ6nPddjBZm+ inr3QbOHc9bvUy5El8RlkqCU/kWkgj1sZlkMGwIDAQABAoIBAG3c6H1EDRN4Rbtl nPOmFBpjE5VuiBe3y0tMK9zWxnPNLpdeA82mMaa6N31C5j+J0te/Lva1r04RzZJC uqpCgr8FbIB+LyypkRl3+v/WoGCtxLKGW5js1pMyCorryAYX5huX9bLt7vBQh19j gtvsPf7asWTfodROyTk42U7joUoFr4TiqBftjVGl3gLB1AidLMHukbiCgRoqGC3v H80VAmc8bt8S46MOvfw1p1Wj/hfyz57pwrU/8/+FHFExzjtkA5Kd8KidzcBc6OnA 3dfsdfsdfdghhhrdfghgfhfghfghhNR0ze6qvlQc6+QQcVIuKxOhmObtBsagnAHJ eqr7FXECgYEAzy881viDqAmsbFC5BN/4GRdUK5nKWFbjD8bo8PbCFMh8Mb+K/GKc dHhUZSMKGQ4u2NNI8fcnh3M0F2zdoaVqZndwr9GxMWyPoB+KBeelDFIWHcY7z55c N2r4CRyWAOMU4lkvBn5RQz1Qxv2de/Tbr3VdxT1tkbWIruIl+wisfwUCgYEAyFz2 xJRugSIBH89haeoHs4JqIKiuHm7+UHkawtfGqrg9jeJDfs7N4hlha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qc912MFmb6KevdBs4dz1u9TLkSXxGWSoJT+RaSCPWxmWQwbAgMBAAGjggEnMIIB
IzAOBgNVHQ8BAf8EBAMCA6gwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMC
MB0GA1UdDgQWBBTkq4+xrl/ODW8rrp3BdTRw849a9jAfBgNVHSMEGDAWgBRbQpEM
sHkbVhoSNGSHqhu+gJBgWTBRBggrBgEFBQcBAQRFMEMwQQYIKwYBBQUHMAKGNWh0
dHA6Ly9zZWN1cml0eV9wb2xpY3lfbWdsfdssdfdsfdsfdsdsfsdfsdffZWNlcnRz
L2NhMBwGA1UdEQQVMBOCEXdvcmtlcm5vZGUubWljYWRvMEEGA1UdHwQ6MDgwNqA0
oDKGMGh0dHA6Ly9zZWN1cml0eV9wb2xpY3lfbWFuYWdlcjo1MDAzL3YxLjAvbm9k
ZWNybDANBgkqhsdfsdfsdfsdfdsffQEAtnBFseEJBzMGMuqRYeXa0rFGcZUXK4S8
HXvpGU95IX988hlv6d4lKc9gEIFBZrqdSk2Q3+GVy3mAGhRpdwwpY5fwlG6wBlOk
4iQOzMVzjYSdsfdsfsdfsfsdfsfsdfdggtz+6Bc+Rs3o2y9L82O14fYBeDPKKh/1
/VBDvVnGlXjXy8FJWgp4Zfsdfdsfdsfsdfsdfsdfdsdsfddfsdsfsdf0UKplawwP
V9Jh+Ruj0Q1MVPjYisNypyZ5F/snO2v9Tgy9/7FID378ZoWRAHYkMljow+ZSt9pF
+p7GR/BLA+vfcWV2482KeYdsfsdfsdfdsfdsfdsfsdfsfd2Or3Q==
-----END CERTIFICATE-----
답변1
delimiter='-{5}[^-]+-{5}'
key=1
crt=2
# get the key
grep -o -P "\\${delimiter}.*?${delimiter}" cert | cut -d $'\n' -f $key | fmt -w 64 > the.key
# get the certificate
grep -o -P "\\${delimiter}.*?${delimiter}" cert | cut -d $'\n' -f $crt | fmt -w 64 > the.crt
출력 파일은 the.key
및 the.crt
.
.key
나는 당신의 의도 가 .crt
파일을 숨기기보다는 확장하는 것이라고 생각합니다 .
또한 cert
입력 파일에 키와 인증서가 한 줄에 포함되어 있습니까?
답변2
fmt -w 35 file
where is 한 줄 파일을 사용하면 file
해당 파일이 질문에 표시되는 정확한 형식으로 변환됩니다. 는 긴 줄을 접어 한 줄에 35자로 축소하려고 시도 하지만 너무 긴 단어를 분리하지 않는 fmt
간단한 텍스트 포맷터입니다. 이것이 바로 이 경우에 작동하는 이유입니다.fmt -w 35
file
awk
명령
awk 'BEGIN { of="file.pem" } { print >of } /^-----END/ { of="file.crt" }'
이것을 읽고 첫 번째 비트를 로 출력 file.pem
하고 두 번째 비트를 로 출력합니다 file.crt
. of
첫 번째 파일 이름 시작 부분에 변수("출력 파일")를 정의한 다음 ^-----END
패턴과 일치하는 줄이 발견되면 이를 두 번째 파일 이름으로 변경하여 이를 수행합니다 . 출력의 모든 줄은 포함된 파일 이름 fmt
으로 인쇄됩니다 .of
모든 것을 한 번에:
fmt -w 35 file |
awk 'BEGIN { of="file.pem" } { print >of } /^-----END/ { of="file.crt" }'