java

位置:IT落伍者 >> java >> 浏览文章

谈谈Java加密技术(七)


发布日期:2018年05月28日
 
谈谈Java加密技术(七)

ECC

ECCElliptic Curves Cryptography椭圆曲线密码编码学是目前已知的公钥体制中对每比特所提供加密强度最高的一种体制在软件注册保护方面起到很大的作用一般的序列号通常由该算法产生

当我开始整理《Java加密技术(二)》的时候我就已经在开始研究ECC了但是关于Java实现ECC算法的资料实在是太少了无论是国内还是国外的资料无论是官方还是非官方的解释最终只有一种答案——ECC算法在jdk后加入支持目前仅仅只能完成密钥的生成与解析

尽管如此我照旧提供相应的Java实现代码以供大家参考

通过java代码实现如下

import javamathBigInteger;

import javasecurityKey;

import javasecurityKeyFactory;

import javasecurityinterfacesECPrivateKey;

import javasecurityinterfacesECPublicKey;

import javasecurityspecECFieldFm;

import javasecurityspecECParameterSpec;

import javasecurityspecECPoint;

import javasecurityspecECPrivateKeySpec;

import javasecurityspecECPublicKeySpec;

import javasecurityspecEllipticCurve;

import javasecurityspecPKCSEncodedKeySpec;

import javasecurityspecXEncodedKeySpec;

import javautilHashMap;

import javautilMap;

import javaxcryptoCipher;

import javaxcryptoNullCipher;

import sunsecurityecECKeyFactory;

import sunsecurityecECPrivateKeyImpl;

import sunsecurityecECPublicKeyImpl;

/**

* ECC安全编码组件

*

* @author 梁栋

* @version

* @since

*/

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;

/**

* 解密<br>

* 用私钥解密

*

* @param data

* @param key

* @return

* @throws Exception

*/

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

// 对密钥解密

byte[] keyBytes = decryptBASE(key);

// 取得私钥

PKCSEncodedKeySpec pkcsKeySpec = new PKCSEncodedKeySpec(keyBytes);

KeyFactory keyFactory = ECKeyFactoryINSTANCE;

ECPrivateKey priKey = (ECPrivateKey) keyFactory

generatePrivate(pkcsKeySpec);

ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(priKeygetS()

priKeygetParams());

// 对数据解密

// TODO Chipher不支持EC算法 未能实现

Cipher cipher = new NullCipher();

// CiphergetInstance(ALGORITHM keyFactorygetProvider());

cipherinit(CipherDECRYPT_MODE priKey ecPrivateKeySpecgetParams());

return cipherdoFinal(data);

}

/**

* 加密<br>

* 用公钥加密

*

* @param data

* @param privateKey

* @return

* @throws Exception

*/

public static byte[] encrypt(byte[] data String privateKey)

throws Exception {

// 对公钥解密

byte[] keyBytes = decryptBASE(privateKey);

// 取得公钥

XEncodedKeySpec xKeySpec = new XEncodedKeySpec(keyBytes);

KeyFactory keyFactory = ECKeyFactoryINSTANCE;

ECPublicKey pubKey = (ECPublicKey) keyFactory

generatePublic(xKeySpec);

ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(pubKeygetW()

pubKeygetParams());

// 对数据加密

// TODO Chipher不支持EC算法 未能实现

Cipher cipher = new NullCipher();

// CiphergetInstance(ALGORITHM keyFactorygetProvider());

cipherinit(CipherENCRYPT_MODE pubKey ecPublicKeySpecgetParams());

return cipherdoFinal(data);

}

/**

* 取得私钥

*

* @param keyMap

* @return

* @throws Exception

*/

public static String getPrivateKey(Map<String Object> keyMap)

throws Exception {

Key key = (Key) keyMapget(PRIVATE_KEY);

return encryptBASE(keygetEncoded());

}

/**

* 取得公钥

*

* @param keyMap

* @return

* @throws Exception

*/

public static String getPublicKey(Map<String Object> keyMap)

throws Exception {

Key key = (Key) keyMapget(PUBLIC_KEY);

return encryptBASE(keygetEncoded());

}

/**

* 初始化密钥

*

* @return

* @throws Exception

*/

public static Map<String Object> initKey() throws Exception {

BigInteger x = new BigInteger(

fecbbcacaaddeedeceee );

BigInteger x = new BigInteger(

fbdfffedccdaad );

ECPoint g = new ECPoint(x x);

// the order of generator

BigInteger n = new BigInteger(

);

// the cofactor

int h = ;

int m = ;

int[] ks = { };

ECFieldFm ecField = new ECFieldFm(m ks);

// y^+xy=x^+x^+

BigInteger a = new BigInteger( );

BigInteger b = new BigInteger( );

EllipticCurve ellipticCurve = new EllipticCurve(ecField a b);

ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve g

n h);

// 公钥

ECPublicKey publicKey = new ECPublicKeyImpl(g ecParameterSpec);

BigInteger s = new BigInteger(

);

// 私钥

ECPrivateKey privateKey = new ECPrivateKeyImpl(s ecParameterSpec);

Map<String Object> keyMap = new HashMap<String Object>();

keyMapput(PUBLIC_KEY publicKey);

keyMapput(PRIVATE_KEY privateKey);

return keyMap;

}

请注意上述代码中的TODO内容再次提醒注意Chipher不支持EC算法 以上代码仅供参考ChipherSignatureKeyPairGeneratorKeyAgreementSecretKey均不支持EC算法为了确保程序能够正常执行我们使用了NullCipher类验证程序

照旧提供一个测试类

import static orgjunitAssert*;

import javamathBigInteger;

import javasecurityspecECFieldFm;

import javasecurityspecECParameterSpec;

import javasecurityspecECPoint;

import javasecurityspecECPrivateKeySpec;

import javasecurityspecECPublicKeySpec;

import javasecurityspecEllipticCurve;

import javautilMap;

import orgjunitTest;

/**

*

* @author 梁栋

* @version

* @since

*/

public class ECCCoderTest {

@Test

public void test() throws Exception {

String inputStr = abc;

byte[] data = inputStrgetBytes();

Map<String Object> keyMap = ECCCoderinitKey();

String publicKey = ECCCodergetPublicKey(keyMap);

String privateKey = ECCCodergetPrivateKey(keyMap);

Systemerrprintln(公钥: \n + publicKey);

Systemerrprintln(私钥 \n + privateKey);

byte[] encodedData = ECCCoderencrypt(data publicKey);

byte[] decodedData = ECCCoderdecrypt(encodedData privateKey);

String outputStr = new String(decodedData);

Systemerrprintln(加密前: + inputStr + \n\r + 解密后: + outputStr);

assertEquals(inputStr outputStr);

}

}

控制台输出

公钥:

MEAwEAYHKoZIzjCAQYFKEEAAEDLAAEAvTwFNvBGsqgfXkObVclOoAokHDBdOPYMhu

gAUTjMqPZ

私钥

MDICAQAwEAYHKoZIzjCAQYFKEEAAEEGzAZAgEBBBTYJsRBNTFwJHcAHFkwNmfilw==

加密前: abc

解密后: abc

               

上一篇:关于Java主线程

下一篇:Java开发为什么需要UML