Illegal character in Base64 encoded data
Hi,
I'm trying to get a print out the public key as a String, which I am getting it in as a base64 encoded, but I get error:
Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in Base64 encoded data.
Code below:
publicstaticvoid main(String[] args){
File pubKeyFile =new File("C:\\usercert.pem");
StringBuffer buffer =new StringBuffer();
try{
FileInputStream fis =new FileInputStream(pubKeyFile);
try{
InputStreamReader isr =new InputStreamReader(fis,"UTF8");
Reader in =new BufferedReader(isr);
int ch;
while((ch = in.read()) > -1){
buffer.append((char)ch);
}
in.close();
String key = buffer.toString();
System.out.println("key is: " + key);
[b]//This is where the code fails:[/b]
[i]String keyDecode = Base64.decodeString(key);[/i]
System.out.println("key ecode is: " + keyDecode);
}catch (UnsupportedEncodingException e){
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
[2137 byte] By [
cup_joea] at [2007-11-27 5:09:09]

# 3
Hi ejp,
It's a rsa public, the usercert.pem looks like below, so since it's not an x509 certificate not sure if I can read it using a java.security.cert.CertificateFactory?
Basically I just want to print the public and private key out as a String. Any help is appreciated.
--BEGIN PUBLIC KEY--
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCg8yo6rDhsNiwUfVR37HgF4bWq
oG13Nd9XLT+Z0VLzCkWJZOdzGNQnnm7ujoQ8gbxeDvIo9RG5I3eZteBwD91Nf6P/
E9lvJQDL2Qnz4EXH/CVW9DeEfvY1UJN9kc6q6KkYEPWssvVvlDOp2slbEKZCJtaP
vVuGCAqfaps8J0FjOQIDAQAB
--END PUBLIC KEY--
# 5
> Hi ejp,
>
> It's a rsa public, the usercert.pem looks like below,
> so since it's not an x509 certificate not sure if I
> can read it using a
> java.security.cert.CertificateFactory?
> Basically I just want to print the public and private
> key out as a String. Any help is appreciated.
>
> --BEGIN PUBLIC KEY--
> MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCg8yo6rDhsNiwUfV
> R37HgF4bWq
> oG13Nd9XLT+Z0VLzCkWJZOdzGNQnnm7ujoQ8gbxeDvIo9RG5I3eZte
> BwD91Nf6P/
> E9lvJQDL2Qnz4EXH/CVW9DeEfvY1UJN9kc6q6KkYEPWssvVvlDOp2s
> lbEKZCJtaP
> vVuGCAqfaps8J0FjOQIDAQAB
> --END PUBLIC KEY--
import java.security.KeyFactory;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import sun.misc.BASE64Decoder;
public class MakeRSAPublicKeyFromPEM
{
public static void main(String[] args) throws Exception
{
final String publickeyAsString =
"--BEGIN PUBLIC KEY--\n"+
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCg8yo6rDhsNiwUfVR37HgF4bWq\n"+
"oG13Nd9XLT+Z0VLzCkWJZOdzGNQnnm7ujoQ8gbxeDvIo9RG5I3eZteBwD91Nf6P/\n"+
"E9lvJQDL2Qnz4EXH/CVW9DeEfvY1UJN9kc6q6KkYEPWssvVvlDOp2slbEKZCJtaP\n"+
"vVuGCAqfaps8J0FjOQIDAQAB\n"+
"--END PUBLIC KEY--\n";
final BASE64Decoder decoder = new BASE64Decoder();
final byte[] publicKeyAsBytes = decoder.decodeBuffer(publickeyAsString.replaceAll("-+.*?-+",""));
final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyAsBytes);
final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
final RSAPublicKey publicKey = (RSAPublicKey)keyFactory.generatePublic(publicKeySpec);
System.out.println("Modulus ........... " + publicKey.getModulus());
System.out.println("Public exponent ... " + publicKey.getPublicExponent());
}
}