Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java remove public key header and footer from string

Hi I am trying to remove the header and footer off a public key that is being stored as a string. I have managed to work out how to remove the "begin" header by removing everything up to the first "\n",

-----BEGIN PUBLIC KEY-----\n

with this code.

String s1 = pKey.substring(pKey.indexOf("\n")+1);

I am however struggling to remove the footer from the end up to the last "\n".

\n-----END PUBLIC KEY-----

There must be a cleaner way to remove them both.

like image 621
kjbaker Avatar asked Nov 01 '25 10:11

kjbaker


1 Answers

Public key can be Some thing like this "-----BEGIN PUBLIC KEY----\nkey\n-----END PUBLIC KEY-----"

So to remove header and footer from string you can use pKey.substring(pKey.indexOf("\n")+1, pKey.lastIndexOf("\n"));

or other way just replace header and footer directly by empty , since it won't be present in key

like image 115
Cosmic Traveller Avatar answered Nov 02 '25 23:11

Cosmic Traveller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!