• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert String data as PublicKey or PrivateKey

 
Bala Raju Mandala
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I need your help in converting String to PublicKey or PrivateKey type.

My plan is to generate two keys using following code.
------------------------------------------------------------------
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
------------------------------------------------------------------

I want to store these keys in text files.

If i want to encrypt/decrypt some data, i will read the text file for keys. So i want to know how i can convert the string data (from file) can be casted as public/private keys?
 
Aryan Khan
Ranch Hand
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the Key.getEncoded().


Also try Cipher.wrap and unwrap. This will provide you an encrypted byte array representing the key (Note this will encrypt your key. another key?)

Don't store your keys as String. Suggest store them as Base64.

Aryan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic