Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

RSA Cipher

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to pass messages to and from the server/client and use a RSA cipher, but I can't figure out how to generate a pair, and pass the public key to the other, and then use it.

I am trying to modify this code that I found via Google:
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code already generates a key pair, and extracts both private and public key, no? The data to be used can be gotten from either key by calling the "getEncoded" method.
 
John Carr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know, but I want to specifically set a public key that coincides with the private key the server generates.

I can't figure out how to set a key...
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you have the bytes that make up a key, you can create a Key object from it like this:
 
John Carr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Ulf, I tried fiddling with it for a bit, and googling the error, and I can't seem to get it...

Here is what I have atm:


And here is the error:
Exception in thread "main" java.security.InvalidKeyException: Neither a public nor a private key
at sun.security.rsa.RSAKeyFactory.engineTranslateKey(RSAKeyFactory.java:193)
at sun.security.rsa.RSAKeyFactory.toRSAKey(RSAKeyFactory.java:111)
at com.sun.crypto.provider.RSACipher.init(RSACipher.java:260)
at com.sun.crypto.provider.RSACipher.engineInit(RSACipher.java:205)
at javax.crypto.Cipher.init(Cipher.java:986)
at javax.crypto.Cipher.init(Cipher.java:935)
at TestRsa2.encrypt(TestRsa2.java:32)
at TestRsa2.main(TestRsa2.java:70)
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Once you have the bytes that make up a key, you can create a Key object from it like this:



That is for a secret (symmetric) key. For a public key, you create an X509EncodedKeySpec from the result of PublicKey.getEncoded(). You supply the X509EncodedKeySpec instance to a KeyFactory instance. Using the getInstance() method to get an RSA instance. Then use the generatePublic() method and supply the X509EncodedKeySpec to it to get the public key.
 
John Carr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Greg, I finally got it to work.
 
Would you like to try a free sample? Today we are featuring tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic