I am getting a Radix out of range exception on performing decryption

I am generating a keypair and converting one of the same into string which later is inserted into the database using the following code:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair generatedKeyPair = keyGen.genKeyPair();
PublicKey pubkey = generatedKeyPair.getPublic();
PrivateKey prvkey = generatedKeyPair.getPrivate();
System.out.println("My Public Key>>>>>>>>>>>"+pubkey);
System.out.println("My Private Key>>>>>>>>>>>"+prvkey);
String keyAsString = new BigInteger(prvkey.getEncoded()).toString(64);

I then retrieve the string from the database and convert it back to the original key using the following code (where rst is my ResultSet):

String keyAsString = rst.getString("privateKey").toString();
byte[] bytes = new BigInteger(keyAsString, 64).toByteArray();
//byte k[] = "HignDlPs".getBytes(); 
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(bytes); 
KeyFactory rsaKeyFac = KeyFactory.getInstance("RSA");  
PrivateKey privKey = rsaKeyFac.generatePrivate(encodedKeySpec);

On using the privKey for RSA decryption, I get the following exception

java.lang.NumberFormatException: Radix out of range
at java.math.BigInteger.<init>(BigInteger.java:294)
at com.util.SimpleFTPClient.downloadFile(SimpleFTPClient.java:176)
at com.Action.FileDownload.processRequest(FileDownload.java:64)
at com.Action.FileDownload.doGet(FileDownload.java:94)

Please guide.

Jon Skeet
people
quotationmark

You're getting an exception because the radix you're providing is greater than Character.MAX_RADIX (which is 36). In other words, it's entirely predictable.

Don't use BigInteger as an encoding class. That's not what it's there for. There are plenty of decent ways of performing base64 encoding. Personally I like this public domain library.

String keyAsString = Base64.encode(prvkey.getEncoded());

Then later:

byte[] bytes = Base64.decode(keyAsString);

people

See more on this question at Stackoverflow