Java encryption Technology (VII)

Source: Internet
Author: User
Tags decrypt

Ecc

Ecc-elliptic Curves cryptography, Elliptic curve cryptography, is a system with the highest encryption strength for each bit in the known public key system. In the software registration protection plays a great role, the general serial number is usually generated by the algorithm.

When I started to sort out the Java encryption Technology (II), I'm already starting to study ECC, but there's so little information about Java implementing ECC algorithms, whether it's domestic or foreign, whether it's official or unofficial, and ultimately there's only one answer- ECC algorithm is added to support after jdk1.5, only the key generation and parsing can be completed at present.

However, I still provide the corresponding Java implementation code, for your reference.

The Java code is implemented as follows: Coder class See Java Encryption Technology (i)

Java code

import Java.math.BigInteger;


import Java.security.Key;


import java.security.KeyFactory;


import Java.security.interfaces.ECPrivateKey;


import Java.security.interfaces.ECPublicKey;


import java.security.spec.ECFieldF2m;


import Java.security.spec.ECParameterSpec;


import Java.security.spec.ECPoint;


import Java.security.spec.ECPrivateKeySpec;


import Java.security.spec.ECPublicKeySpec;


import Java.security.spec.EllipticCurve;


Import Java.security.spec.PKCS8EncodedKeySpec;


import Java.security.spec.X509EncodedKeySpec;


import Java.util.HashMap;


import Java.util.Map;





import Javax.crypto.Cipher;


import Javax.crypto.NullCipher;





import sun.security.ec.ECKeyFactory;


import Sun.security.ec.ECPrivateKeyImpl;


import Sun.security.ec.ECPublicKeyImpl;





/**


* ECC Security Coding Component


  *


* @author Liangdong


* @version 1.0


* @since 1.0


  */


public abstract class Ecccoder extends coder {





public static final String algorithm = "EC";


private static final String Public_key = "Eccpublickey";


private static final String Private_key = "Eccprivatekey";





     /**


* Decryption <br>


* Decryption with the private key


      *


* @param data


* @param key


* @return


* @throws Exception


      */


public static byte[] Decrypt (byte[] data, String key) throws Exception {


//decryption of key


byte[] keybytes = decryptBASE64 (key);





//Get the private key


Pkcs8encodedkeyspec pkcs8keyspec = new Pkcs8encodedkeyspec (keybytes);


keyfactory keyfactory = eckeyfactory.instance;





Ecprivatekey Prikey = (ecprivatekey) keyfactory


. Generateprivate (PKCS8KEYSPEC);





Ecprivatekeyspec ecprivatekeyspec = new Ecprivatekeyspec (Prikey.gets (),


prikey.getparams ());





Decrypt the data


//TODO Chipher does not support EC algorithm failure to implement


Cipher Cipher = new NullCipher ();


//Cipher.getinstance (algorithm, Keyfactory.getprovider ());


Cipher.init (Cipher.decrypt_mode, Prikey, Ecprivatekeyspec.getparams ());





return cipher.dofinal (data);


     }





     /**


* Encryption <br>


* with public key encryption


      *


* @param data


* @param privatekey


* @return


* @throws Exception


      */


public static byte[] Encrypt (byte[] data, String Privatekey)


throws Exception {


//decryption of public key


byte[] keybytes = decryptBASE64 (Privatekey);





//Obtain public key


X509encodedkeyspec X509keyspec = new X509encodedkeyspec (keybytes);


keyfactory keyfactory = eckeyfactory.instance;





Ecpublickey PubKey = (ecpublickey) keyfactory


. Generatepublic (X509KEYSPEC);





Ecpublickeyspec ecpublickeyspec = new Ecpublickeyspec (PUBKEY.GETW (),


pubkey.getparams ());





//Encryption of data


//TODO Chipher does not support EC algorithm failure to implement


Cipher Cipher = new NullCipher ();


//Cipher.getinstance (algorithm, Keyfactory.getprovider ());


Cipher.init (Cipher.encrypt_mode, PubKey, Ecpublickeyspec.getparams ());





return cipher.dofinal (data);


     }





     /**


* Obtain private key


      *


* @param keymap


* @return


* @throws Exception


      */


public static String Getprivatekey (map<string, object> keymap)


throws Exception {


key key = (key) keymap.get (Private_key);





return encryptBASE64 (key.getencoded ());


     }





     /**


* Obtain public key


      *


* @param keymap


* @return


* @throws Exception


      */


public static String Getpublickey (map<string, object> keymap)


throws Exception {


key key = (key) keymap.get (Public_key);





return encryptBASE64 (key.getencoded ());


     }





     /**


* Initialization Key


*


* @return


* @throws Exception


      */


public static map<string, Object> Initkey () throws Exception {


BigInteger x1 = new BigInteger (


"2fe13c0537bbc11acaa07d793de4e6d5e5c94eee8", 16);


BigInteger x2 = new BigInteger (


"289070fb05d38ff58321f2e800536d538ccdaa3d9", 16);





Ecpoint g = new Ecpoint (x1, x2);





//The Order of generator


BigInteger n = new BigInteger (


"5846006549323611672814741753598448348329118574063", 10);


//The cofactor


int h = 2;


int m = 163;


int[] ks = {7, 6, 3};


ecfieldf2m Ecfield = new ecfieldf2m (m, KS);


Y^2+xy=x^3+x^2+1


BigInteger a = new BigInteger ("1", 2);


BigInteger B = new BigInteger ("1", 2);





Ellipticcurve ellipticcurve = new Ellipticcurve (Ecfield, A, b);





Ecparameterspec ecparameterspec = new Ecparameterspec (Ellipticcurve, G,


N, h);


//Public key


Ecpublickey PublicKey = new Ecpublickeyimpl (g, ecparameterspec);





BigInteger s = new BigInteger (


"1234006549323611672814741753598448348329118574063", 10);


//private key


Ecprivatekey Privatekey = new Ecprivatekeyimpl (s, ecparameterspec);





map<string, object> keymap = new hashmap<string, object> (2);





keymap.put (Public_key, PublicKey);


Keymap.put (Private_key, Privatekey);





return keymap;


     }





}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.