A
A
alpa_kz2017-05-30 13:12:07
Java
alpa_kz, 2017-05-30 13:12:07

How to run java class implementation of elliptic curve encryption?

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.ECGenParameterSpec;
import javax.crypto.Cipher;
import org.bouncycastle.jce.spec.IEKeySpec;
import org.bouncycastle.jce.spec.IESParameterSpec;

public class ECIESexample {
    private SecureRandom random;
    private int keySize;
    private KeyPair akey;
    private KeyPair bkey;
    
  public ECIESexample () throws Exception{  
    this.random = new SecureRandom();
  }
  
  public void establishKeys(String keysize) throws Exception {
  
      ECGenParameterSpec     ecGenSpec = new ECGenParameterSpec(keysize);
      KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");

      keyGen.initialize(ecGenSpec, random);

      this.akey = keyGen.generateKeyPair();
      this.bkey = keyGen.generateKeyPair();
    this.keySize = Integer.valueOf( (ecGenSpec.getName().substring(4, 7)) ).intValue();
  }
  
  
  public byte[] encrypt(byte[] plainText) throws Exception {

      // get ECIES cipher objects
      Cipher acipher = Cipher.getInstance("ECIES");
  
      //  generate derivation and encoding vectors
        byte[]  d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        byte[]  e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
        IESParameterSpec param = new IESParameterSpec(d, e, 256);
        
        // encrypt the plaintext using the public key
      acipher.init(Cipher.ENCRYPT_MODE, new IEKeySpec(akey.getPrivate(), bkey.getPublic()), param);
      return acipher.doFinal(plainText);
  }
   
  public byte[] decrypt(byte[] cipherText) throws Exception {
      
      // get ECIES cipher objects
      Cipher bcipher = Cipher.getInstance("ECIES");
  
      //  generate derivation and encoding vectors
        byte[]  d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        byte[]  e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
        IESParameterSpec param = new IESParameterSpec(d, e, 256);

        // decrypt the text using the private key
      bcipher.init(Cipher.DECRYPT_MODE, new IEKeySpec(bkey.getPrivate(), akey.getPublic()), param);
      return bcipher.doFinal(cipherText); 
  }
  
  public byte[] sign(byte[] plainText) throws Exception {
      
    Signature sig = Signature.getInstance("SHA1WithECDSA");
    sig.initSign(akey.getPrivate());
    sig.update(plainText);
    return sig.sign();
  }

  public boolean verify(byte[] plainText, byte[] signature) throws Exception {
      
    Signature sig = Signature.getInstance("SHA1WithECDSA");
    sig.initVerify(akey.getPublic());
    sig.update(plainText);
    try {
      if (sig.verify(signature)) {
        return true;
      } else return false;
    } catch (SignatureException se) {
      System.out.println( "Signature failed" );
    }
    return false;
  }

  public int getKeySize() {
    return keySize;
  }
  
}

As I understand it, this is a class implemented for encryption, now I need to create a file (a class to implement what was written above), right?
public class encryption {
public static void main(String[] args) throws Exception{
ECIESexample en = new ECIESexample();
byte[] messageForEncrypt = "hello world -- a nice day today".getBytes(); 
en.encrypt(messageForEncrypt);
}
}

so wrong, how will it be right?
this error:
Exception in thread "main" java.security.NoSuchAlgorithmException: Cannot find any provider supporting ECIES
at javax.crypto.Cipher.getInstance(Cipher.java:540)
at ECIESexample.encrypt(ECIESexample.java:37)
at encryption. main(encryption.java:6)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2017-05-30
@aol-nnov

> so wrong
what is wrong? right! you just need to look at the class with a critical eye, and understand which methods in the created object to pull and in what order.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question