Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save denyspozniak/d103b1a86d60de318b6b18ae63f5e544 to your computer and use it in GitHub Desktop.
Save denyspozniak/d103b1a86d60de318b6b18ae63f5e544 to your computer and use it in GitHub Desktop.
Converting multi line PEM file to single line string
  1. Generate private key
openssl genrsa -out privatekey.pem 1024
Generating RSA private key, 1024 bit long modulus (2 primes)
......................+++++
....................+++++
e is 65537 (0x010001)
  1. Check the private key
cat privatekey.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDWC28RrWlGM7Q1EpYyiwqW7Yk7NLLfnlKiTHJBu8jw6tgxiMui
l69oRyWCjs0/K7GND1Y5KvhMoHjVeug5dYq0BXH1p2qQJdnqlYNDIpgAMLuE+oJm
6L8stsnpwixcKCp9Lc/cdZ7xy472lZZPVrb/cHn0MpDeY0Kbh9GxRzWCyQIDAQAB
AoGACHmtbTLDn17+vLc+sUOmKLHBJFIC3y3iscB7KbUo0LlL1DJJdeexr1xZ1OCY
qe6t+hroXB0idc5I2pMiTNwloMaZlhmDkRvtWMey+GvsrGLyg7RMJW6S+D5QS2kp
KK5V92RlyZRbW7UHEAMImVXz4PrCx9N4PVwLMl/vTVe0kQ0CQQD9i5OD6tlAwO35
OZxp2qAZFx8guRaKQmaC4wBDmiirxRU8sWmgF2rBLUfLStdlbsBQpR75SNfHb3K3
xK1zSqsTAkEA2B30IfQPuyA3mXOQ7hnH6S/s0v6Df372ak3uYc6woCSFNJQkSyC6
AVQQpIy9PWt4xbATpTMHqngQIwxlS2SaMwJBAMg58J3t2fG8KnJ136LspyAJVWi3
Iii/dUsJz0yTsmir9DCA/qQRuhmiE59klCOjSbamH4bH0rfJuHONm1h/8AcCQEY8
feInApHLJ6asY1audET5uVrdMnlWFtl3mFibJtX06IGs/5qW+TyDSKFhyALVEbwV
vFy9F+mz+XKajbUR7fcCQF2zZLvIJFAe100J/Uz+rTMuT7GZJUCkLgeaEvrG/Kjv
uhhb76VDgjoKXr+oglL2W0p9nFO85mDoZqYNQgEjUGA=
-----END RSA PRIVATE KEY-----
  1. Convert to single line
awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' privatekey.pem > privatekey.cer
  1. Check the single line private key
cat privatekey.cer
-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQDWC28RrWlGM7Q1EpYyiwqW7Yk7NLLfnlKiTHJBu8jw6tgxiMui\nl69oRyWCjs0/K7GND1Y5KvhMoHjVeug5dYq0BXH1p2qQJdnqlYNDIpgAMLuE+oJm\n6L8stsnpwixcKCp9Lc/cdZ7xy472lZZPVrb/cHn0MpDeY0Kbh9GxRzWCyQIDAQAB\nAoGACHmtbTLDn17+vLc+sUOmKLHBJFIC3y3iscB7KbUo0LlL1DJJdeexr1xZ1OCY\nqe6t+hroXB0idc5I2pMiTNwloMaZlhmDkRvtWMey+GvsrGLyg7RMJW6S+D5QS2kp\nKK5V92RlyZRbW7UHEAMImVXz4PrCx9N4PVwLMl/vTVe0kQ0CQQD9i5OD6tlAwO35\nOZxp2qAZFx8guRaKQmaC4wBDmiirxRU8sWmgF2rBLUfLStdlbsBQpR75SNfHb3K3\nxK1zSqsTAkEA2B30IfQPuyA3mXOQ7hnH6S/s0v6Df372ak3uYc6woCSFNJQkSyC6\nAVQQpIy9PWt4xbATpTMHqngQIwxlS2SaMwJBAMg58J3t2fG8KnJ136LspyAJVWi3\nIii/dUsJz0yTsmir9DCA/qQRuhmiE59klCOjSbamH4bH0rfJuHONm1h/8AcCQEY8\nfeInApHLJ6asY1audET5uVrdMnlWFtl3mFibJtX06IGs/5qW+TyDSKFhyALVEbwV\nvFy9F+mz+XKajbUR7fcCQF2zZLvIJFAe100J/Uz+rTMuT7GZJUCkLgeaEvrG/Kjv\nuhhb76VDgjoKXr+oglL2W0p9nFO85mDoZqYNQgEjUGA=\n-----END RSA PRIVATE KEY-----\n%
  1. Revert the single line to multilne
echo -ne $(cat privatekey.cer)
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDWC28RrWlGM7Q1EpYyiwqW7Yk7NLLfnlKiTHJBu8jw6tgxiMui
l69oRyWCjs0/K7GND1Y5KvhMoHjVeug5dYq0BXH1p2qQJdnqlYNDIpgAMLuE+oJm
6L8stsnpwixcKCp9Lc/cdZ7xy472lZZPVrb/cHn0MpDeY0Kbh9GxRzWCyQIDAQAB
AoGACHmtbTLDn17+vLc+sUOmKLHBJFIC3y3iscB7KbUo0LlL1DJJdeexr1xZ1OCY
qe6t+hroXB0idc5I2pMiTNwloMaZlhmDkRvtWMey+GvsrGLyg7RMJW6S+D5QS2kp
KK5V92RlyZRbW7UHEAMImVXz4PrCx9N4PVwLMl/vTVe0kQ0CQQD9i5OD6tlAwO35
OZxp2qAZFx8guRaKQmaC4wBDmiirxRU8sWmgF2rBLUfLStdlbsBQpR75SNfHb3K3
xK1zSqsTAkEA2B30IfQPuyA3mXOQ7hnH6S/s0v6Df372ak3uYc6woCSFNJQkSyC6
AVQQpIy9PWt4xbATpTMHqngQIwxlS2SaMwJBAMg58J3t2fG8KnJ136LspyAJVWi3
Iii/dUsJz0yTsmir9DCA/qQRuhmiE59klCOjSbamH4bH0rfJuHONm1h/8AcCQEY8
feInApHLJ6asY1audET5uVrdMnlWFtl3mFibJtX06IGs/5qW+TyDSKFhyALVEbwV
vFy9F+mz+XKajbUR7fcCQF2zZLvIJFAe100J/Uz+rTMuT7GZJUCkLgeaEvrG/Kjv
uhhb76VDgjoKXr+oglL2W0p9nFO85mDoZqYNQgEjUGA=
-----END RSA PRIVATE KEY-----
  1. Check if the reverted format is ok
echo -ne $(cat privatekey.cer) | openssl rsa -check
RSA key ok
writing RSA key
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDWC28RrWlGM7Q1EpYyiwqW7Yk7NLLfnlKiTHJBu8jw6tgxiMui
l69oRyWCjs0/K7GND1Y5KvhMoHjVeug5dYq0BXH1p2qQJdnqlYNDIpgAMLuE+oJm
6L8stsnpwixcKCp9Lc/cdZ7xy472lZZPVrb/cHn0MpDeY0Kbh9GxRzWCyQIDAQAB
AoGACHmtbTLDn17+vLc+sUOmKLHBJFIC3y3iscB7KbUo0LlL1DJJdeexr1xZ1OCY
qe6t+hroXB0idc5I2pMiTNwloMaZlhmDkRvtWMey+GvsrGLyg7RMJW6S+D5QS2kp
KK5V92RlyZRbW7UHEAMImVXz4PrCx9N4PVwLMl/vTVe0kQ0CQQD9i5OD6tlAwO35
OZxp2qAZFx8guRaKQmaC4wBDmiirxRU8sWmgF2rBLUfLStdlbsBQpR75SNfHb3K3
xK1zSqsTAkEA2B30IfQPuyA3mXOQ7hnH6S/s0v6Df372ak3uYc6woCSFNJQkSyC6
AVQQpIy9PWt4xbATpTMHqngQIwxlS2SaMwJBAMg58J3t2fG8KnJ136LspyAJVWi3
Iii/dUsJz0yTsmir9DCA/qQRuhmiE59klCOjSbamH4bH0rfJuHONm1h/8AcCQEY8
feInApHLJ6asY1audET5uVrdMnlWFtl3mFibJtX06IGs/5qW+TyDSKFhyALVEbwV
vFy9F+mz+XKajbUR7fcCQF2zZLvIJFAe100J/Uz+rTMuT7GZJUCkLgeaEvrG/Kjv
uhhb76VDgjoKXr+oglL2W0p9nFO85mDoZqYNQgEjUGA=
-----END RSA PRIVATE KEY-----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment