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

Posted by user3672391 on Stack Overflow See other posts from Stack Overflow or by user3672391
Published on 2014-06-02T21:19:46Z Indexed on 2014/06/02 21:25 UTC
Read the original article Hit count: 474

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about exception