Java Encryption Technology-the Highest ECC of Asymmetric Encryption Algorithms

Keywords: Java Junit Mac SSL

Links to the original text: https://my.oschina.net/mohaiyong/blog/221240
ECC
ECC-Elliptic Curves Cryptography (ECC-Elliptic Curves Cryptography) is one of the most intensive public key cryptosystems available for each bit. It plays an important role in the protection of software registration. Generally, the serial number is generated by this algorithm.
When I started to collate "Java Encryption Technology (2)", I was already beginning to study ECC, but there is too little information about the implementation of ECC algorithm in Java. Whether domestic or foreign, whether official or unofficial, there is only one answer in the end - ECC algorithm after jdk1.5. At present, only key generation and parsing can be completed by adding support. If you want to achieve ECC algorithm, you need to call hardware to complete encryption/decryption (ECC algorithm is quite resource-consuming, if only using CPU for encryption/decryption, inefficient), involving the Java Card field, PKCS#11. In fact, the configuration of PKCS # 11 is very simple, but lack of hardware equipment, can not try!

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

Implemented through java code as follows: See Coder class Java Encryption Technology (I)
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 Secure Coding Component
 * 
 * @author Liang Dong
 * @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 Private Key
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] data, String key) throws Exception {
		// Key decryption
		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());

		// Data Decryption
		// TODO Chipher does not support EC algorithm but fails to implement it
		Cipher cipher = new NullCipher();
		// Cipher.getInstance(ALGORITHM, keyFactory.getProvider());
		cipher.init(Cipher.DECRYPT_MODE, priKey, ecPrivateKeySpec.getParams());

		return cipher.doFinal(data);
	}

	/**
	 * Encryption < br >
	 * Encryption with public key
	 * 
	 * @param data
	 * @param privateKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] data, String privateKey)
			throws Exception {
		// Public key decryption
		byte[] keyBytes = decryptBASE64(privateKey);

		// Obtain the 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 but fails to implement it
		Cipher cipher = new NullCipher();
		// Cipher.getInstance(ALGORITHM, keyFactory.getProvider());
		cipher.init(Cipher.ENCRYPT_MODE, pubKey, ecPublicKeySpec.getParams());

		return cipher.doFinal(data);
	}

	/**
	 * Get the 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 the 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;
	}

}


Note that in the above code TODO content, again remind me, Chipher does not support EC algorithm. The above code is for reference only. Chipher, Signature, KeyPair Generator, KeyAgreement and SecretKey do not support EC algorithm. To ensure the normal execution of the program, we use the NullCipher class to verify the program.

Still provide a test class:
import static org.junit.Assert.*;

import java.math.BigInteger;
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.util.Map;

import org.junit.Test;

/**
 * 
 * @author Liang Dong
 * @version 1.0
 * @since 1.0
 */
public class ECCCoderTest {

	@Test
	public void test() throws Exception {
		String inputStr = "abc";
		byte[] data = inputStr.getBytes();

		Map<String, Object> keyMap = ECCCoder.initKey();

		String publicKey = ECCCoder.getPublicKey(keyMap);
		String privateKey = ECCCoder.getPrivateKey(keyMap);
		System.err.println("public key: \n" + publicKey);
		System.err.println("Private key: \n" + privateKey);

		byte[] encodedData = ECCCoder.encrypt(data, publicKey);

		byte[] decodedData = ECCCoder.decrypt(encodedData, privateKey);

		String outputStr = new String(decodedData);
		System.err.println("Before Encryption: " + inputStr + "\n\r" + "After decryption: " + outputStr);
		assertEquals(inputStr, outputStr);
	}
}


Console output:
public key: 
MEAwEAYHKoZIzj0CAQYFK4EEAAEDLAAEAv4TwFN7vBGsqgfXk95ObV5clO7oAokHD7BdOP9YMh8u
gAU21TjM2qPZ

//Private key: 
MDICAQAwEAYHKoZIzj0CAQYFK4EEAAEEGzAZAgEBBBTYJsR3BN7TFw7JHcAHFkwNmfil7w==

//Before encryption: abc

//After decryption: abc




Related links:
Java Encryption Technology (1) - BASE64 and One-way Encryption Algorithms MD5 & SHA & MAC
Java Encryption Technology (2) - Symmetric Encryption DES&AES
Java Encryption Technology (3) - PBE Algorithms
Java Encryption Technology (4) - Asymmetric Encryption Algorithms RSA
Java Encryption Technology (5) - Origin of Asymmetric Encryption Algorithms
Java Encryption Technology (6) - Digital Signature Algorithms DSA
Java Encryption Technology (7) - Highest ECC of Asymmetric Encryption Algorithms
Java Encryption Technology (8) - Digital Certificate
Java Encryption Technology (9) - Preliminary Exploration of SSL
Java Encryption Technology (10) - One-way Authentication
Java Encryption Technology (11) - Two-way Authentication
Java Encryption Technology (12) - *. PFX (*. p12) - Personal Information Exchange File

Reproduced in: https://my.oschina.net/mohaiyong/blog/221240

Posted by stormszero on Sun, 08 Sep 2019 23:20:29 -0700