This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Security and the fly likes convert String data as PublicKey or PrivateKey Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » Security
Reply Bookmark "convert String data as PublicKey or PrivateKey" Watch "convert String data as PublicKey or PrivateKey" New topic
Author

convert String data as PublicKey or PrivateKey

Bala Raju Mandala
Ranch Hand

Joined: Sep 21, 2006
Posts: 40
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

Joined: Sep 12, 2004
Posts: 289
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


OCP/MCP/SCJP/SCWCD/IBM XML/SCMAD/SCEA-1
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: convert String data as PublicKey or PrivateKey
 
Similar Threads
Generating Public and Private keys.
Return Token with Private/Public keys
How to store PublicKey and PrivateKey in database??
RSA key retrieval from String representation
How to store PublicKey and PrivateKey in database??