I have created a private key:
openssl genrsa [-out file] After that I have generated a public key: openssl rsa â pub-private key [out file] I have some messages with my private key I want to sign and want to verify some other messages with my public key, using code like this:
is a public string Not (String message) sign exception puts {try {signature sign = signature.getInstance ( "SHA1withRSA"); Sign.initSign (privateKey); Sign.update (message.getBytes ("UTF-8"); Return the new string (base 64.ex.svg (sign.sign ()), "UTF-8"); } Hold (exception before) {new sign exception exception (ex); }} Verify Public Bullion (string message, string signature) sign exceptions {try {signature sign = signature.getInstance ("SHA1withRSA"); Sign.initVerify (publicKey); Sign.update (message.getBytes ("UTF-8"); Return sign.verify (Base64.decodeBase64 (signature.getBytes ("UTF-8")); } Hold (exception before) {new sign exception exception (ex); }} I found the solution to convert my private key to PKCS8 format and load it. This works with some code like this:
Public PrivateKey getPrivateKey (string filename) throws an exception {file f = new file (filename); FileInputStream fis = New FileInputStream (f); DataInputStream dis = new data inputstream (FIS); Byte [] keyBytes = new byte [(int) f.length ()]; Dis.readFully (keyBytes); Dis.close (); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (keyBytes); Keyfiler kf = keyfactor.just instance ("RSA"); Back kf.generatePrivate (spec); } And finally my question is: How do I load my RSA public with a file? I think that maybe I convert my public key file to x509 format, and use X509EncodedKeySpec but how can I do this?
The relevant information given below is provided on generate 2048-bit RSA private key
$ openssl genrsa -out private_key.pem 2048 private key Convert format to PKCS # 8 (so that Java can read it)
$ openssl pkcs8 -topk8 -inform PMM-Producer DER-Private_keep_outout personal_kid- -non-code < / P> Output of the public key part in DER format (so that Java can read it)
$ openssl RSA -in personal_keypack -pout-outform DER-person publi C_key.der private key import java.io. *; Import java.nio *; Import java.security *; Import java.security.spec. *; Public Square PrivateKeyReader {Public Fixed PrivateKey get throws exception (string filename) {BITE [] keyBytes = Files.readAllBytes (Paths.get (filename)); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec (keyBytes); Keyfiler kf = keyfactor.just instance ("RSA"); Back kf.generatePrivate (spec); }} public key import java.io. *; Import java.nio *; Import java.security *; Import java.security.spec. *; Public class public care reader {receives public static public (string filename) exceptions {Bite [] keybites = files Readelbates (path gate (filename)); X509 Encoded Keyspeak Tip = New X509 Encoded Keyspeak (Keybites); Keyfiler kf = keyfactor.just instance ("RSA"); Return kf.generatePublic (spec); }}
Comments
Post a Comment