java - ECKeyAgreement in Generating public and private key using ecc -
i working on concept of encryption , decryption using ecc. generated public , private key. while encrypting text getting error:
java.security.invalidkeyexception: eckeyagreement requires ecprivatekey @ org.bouncycastle.jce.provider.jceecdhkeyagreement.engineinit(jceecdhkeyagreement.java:121) @ javax.crypto.keyagreement.init(keyagreement.java:462) @ javax.crypto.keyagreement.init(keyagreement.java:436) @ rbl2015.encryec.main(encryec.java:67)
this encryption java file:
import java.io.file; import java.io.fileinputstream; import java.security.key; import java.security.keyfactory; import java.security.keypair; import java.security.keypairgenerator; import java.security.privatekey; import java.security.publickey; import java.security.security; import java.security.spec.ecparameterspec; import java.security.spec.ellipticcurve; import java.security.spec.keyspec; import java.security.spec.pkcs8encodedkeyspec; import java.security.spec.x509encodedkeyspec; import java.util.scanner; import javax.crypto.cipher; import javax.crypto.keyagreement; import javax.crypto.secretkeyfactory; import javax.crypto.spec.deskeyspec; import org.apache.commons.codec.binary.base64; import org.bouncycastle.jce.provider.bouncycastleprovider; public class encryec { keypairgenerator kpg; ellipticcurve curve; ecparameterspec ecspec; keypair akeypair; static keyagreement akeyagree; keypair bkeypair; keyagreement bkeyagree; keyfactory keyfac; static string msg; public static void main(string args[]) { security.addprovider(new bouncycastleprovider()); scanner ss=new scanner(system.in); try{ string path = "d:\\rp"; file filepublickey = new file(path+"\\public.key"); fileinputstream fis = new fileinputstream(path+"\\public.key"); byte[] encodedpublickey = new byte[(int) filepublickey.length()]; fis.read(encodedpublickey); fis.close(); // read private key. file fileprivatekey = new file(path+"\\private.key"); fis = new fileinputstream(path+"\\private.key"); byte[] encodedprivatekey = new byte[(int) fileprivatekey.length()]; fis.read(encodedprivatekey); fis.close(); keyfactory keyfactory = keyfactory.getinstance("ecdh"); x509encodedkeyspec publickeyspec = new x509encodedkeyspec( encodedpublickey); publickey publickey = keyfactory.generatepublic(publickeyspec); pkcs8encodedkeyspec privatekeyspec = new pkcs8encodedkeyspec(encodedprivatekey); privatekey privatekey = keyfactory.generateprivate(privatekeyspec); akeyagree = keyagreement.getinstance("ecdh", "bc"); akeyagree.init(privatekey); // exception line akeyagree.dophase(publickey, true); byte[] abys = akeyagree.generatesecret(); keyspec akeyspec = new deskeyspec(abys); secretkeyfactory afactory = secretkeyfactory.getinstance("des"); key asecretkey = afactory.generatesecret(akeyspec); cipher acipher = cipher.getinstance(asecretkey.getalgorithm()); acipher.init(cipher.encrypt_mode, asecretkey); byte[] enctext = acipher.dofinal("its rahul".getbytes()); system.out.println(base64.encodebase64string(enctext)); system.out.println(enctext); } catch(exception e) { e.printstacktrace(); } } }
i don't know missing. tried can ecprivatekey.
this code generating public , private key:
import java.io.*; import java.security.*; import java.security.spec.*; public class rahul { public static void main(string args[]) { rahul rahul = new rahul(); try { string path = "d:\\rp"; keypairgenerator keygen = keypairgenerator.getinstance("dsa"); keygen.initialize(1024); keypair generatedkeypair = keygen.genkeypair(); system.out.println("generated key pair"); rahul.dumpkeypair(generatedkeypair); rahul.savekeypair(path, generatedkeypair); keypair loadedkeypair = rahul.loadkeypair(path, "dsa"); system.out.println("loaded key pair"); rahul.dumpkeypair(loadedkeypair); } catch (exception e) { e.printstacktrace(); return; } } private void dumpkeypair(keypair keypair) { publickey pub = keypair.getpublic(); system.out.println("public key: " + gethexstring(pub.getencoded())); privatekey priv = keypair.getprivate(); system.out.println("private key: " + gethexstring(priv.getencoded())); } private string gethexstring(byte[] b) { string result = ""; (int = 0; < b.length; i++) { result += integer.tostring((b[i] & 0xff) + 0x100, 16).substring(1); } return result; } public void savekeypair(string path, keypair keypair) throws ioexception { privatekey privatekey = keypair.getprivate(); publickey publickey = keypair.getpublic(); // store public key. x509encodedkeyspec x509encodedkeyspec = new x509encodedkeyspec( publickey.getencoded()); fileoutputstream fos = new fileoutputstream(path + "/public.key"); fos.write(x509encodedkeyspec.getencoded()); fos.close(); // store private key. pkcs8encodedkeyspec pkcs8encodedkeyspec = new pkcs8encodedkeyspec( privatekey.getencoded()); fos = new fileoutputstream(path + "/private.key"); fos.write(pkcs8encodedkeyspec.getencoded()); fos.close(); } public keypair loadkeypair(string path, string algorithm) throws ioexception, nosuchalgorithmexception, invalidkeyspecexception { // read public key. file filepublickey = new file(path + "/public.key"); fileinputstream fis = new fileinputstream(path + "/public.key"); byte[] encodedpublickey = new byte[(int) filepublickey.length()]; fis.read(encodedpublickey); fis.close(); // read private key. file fileprivatekey = new file(path + "/private.key"); fis = new fileinputstream(path + "/private.key"); byte[] encodedprivatekey = new byte[(int) fileprivatekey.length()]; fis.read(encodedprivatekey); fis.close(); // generate keypair. keyfactory keyfactory = keyfactory.getinstance(algorithm); x509encodedkeyspec publickeyspec = new x509encodedkeyspec( encodedpublickey); publickey publickey = keyfactory.generatepublic(publickeyspec); pkcs8encodedkeyspec privatekeyspec = new pkcs8encodedkeyspec( encodedprivatekey); privatekey privatekey = keyfactory.generateprivate(privatekeyspec); return new keypair(publickey, privatekey); } }
you should try , create ec(dh) key pair instead of dsa key pair. although general method of operation identical (both ecdsa , dsa based on diffie-hellman problem) key types not.