In this Post I am going to talk about the Elliptic curve encryption.First I will tell you why we use ECC. ECC is public-key Cryptography. ECC is used for the Authentication and authorization.Mainly The structure is like this way.In here(http://www.johannes-bauer.com/compsci/ecc/) you can see very good explanation how ECC works. .First Using public key we have sign data.and then using private key we can verify data.In here we verify that compare the signing data and what are the we input data.So I used bouncy castle for ECC. In here (http://www.bouncycastle.org/latest_releases.html) You can download the bouncy castle jar and add to the Project.In here I First sign my data using Private key.Then I Verify the signing data using Public key.
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.Security;
import java.security.interfaces.ECPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECParameterSpec;
public class PublickeyEncryption {
public static void main(String args[]) {
try {
/**
* ECC signature sign and verify
*/
ECParameterSpec ecSpec = ECNamedCurveTable
.getParameterSpec("prime192v1");
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
g.initialize(ecSpec, new SecureRandom());
// Genarate keypair
KeyPair pair = g.generateKeyPair();
System.out.println("Provider Name :"+BouncyCastleProvider.PROVIDER_NAME);
/**
* Genarate Private and Public keys
*/
PublicKey pubKey = pair.getPublic();
PrivateKey prikey = pair.getPrivate();
byte[] inputData = new byte[] { (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
(byte) 0x01, (byte) 0x01 };
Signature sig = Signature.getInstance("ECDSA", "BC");
/**
* Initialize the sign and update data to be Sign
*/
sig.initSign(prikey);
sig.update(inputData);
/**
* SIGN the data using private key
*/
byte[] signatureBytes = sig.sign();
System.out.println("Sign byte arry:" + Arrays.toString(signatureBytes));
/**
* Initialize the verify and update data to be Verify
*/
sig.initVerify(pubKey);
sig.update(inputData);
/**
* Check verify true or False
*/
System.out.println("VERIFY :" + sig.verify(signatureBytes));
} catch (Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment