ECC
ECC
當我開始整理《Java加密技術(二)》的時候
盡管如此
通過java代碼實現如下
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import java
import javax
import javax
import sun
import sun
import sun
/**
* ECC安全編碼組件
*
* @author 梁棟
* @version
* @since
*/
public abstract class ECCCoder extends Coder {
public static final String ALGORITHM =
private static final String PUBLIC_KEY =
private static final String PRIVATE_KEY =
/**
* 解密<br>
* 用私鑰解密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] data
// 對密鑰解密
byte[] keyBytes = decryptBASE
// 取得私鑰
PKCS
KeyFactory keyFactory = ECKeyFactory
ECPrivateKey priKey = (ECPrivateKey) keyFactory
ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(priKey
priKey
// 對數據解密
// TODO Chipher不支持EC算法 未能實現
Cipher cipher = new NullCipher();
// Cipher
cipher
return cipher
}
/**
* 加密<br>
* 用公鑰加密
*
* @param data
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data
throws Exception {
// 對公鑰解密
byte[] keyBytes = decryptBASE
// 取得公鑰
X
KeyFactory keyFactory = ECKeyFactory
ECPublicKey pubKey = (ECPublicKey) keyFactory
ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(pubKey
pubKey
// 對數據加密
// TODO Chipher不支持EC算法 未能實現
Cipher cipher = new NullCipher();
// Cipher
cipher
return cipher
}
/**
* 取得私鑰
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String
throws Exception {
Key key = (Key) keyMap
return encryptBASE
}
/**
* 取得公鑰
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String
throws Exception {
Key key = (Key) keyMap
return encryptBASE
}
/**
* 初始化密鑰
*
* @return
* @throws Exception
*/
public static Map<String
BigInteger x
BigInteger x
ECPoint g = new ECPoint(x
// the order of generator
BigInteger n = new BigInteger(
// the cofactor
int h =
int m =
int[] ks = {
ECFieldF
// y^
BigInteger a = new BigInteger(
BigInteger b = new BigInteger(
EllipticCurve ellipticCurve = new EllipticCurve(ecField
ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve
n
// 公鑰
ECPublicKey publicKey = new ECPublicKeyImpl(g
BigInteger s = new BigInteger(
// 私鑰
ECPrivateKey privateKey = new ECPrivateKeyImpl(s
Map<String
keyMap
keyMap
return keyMap;
}
請注意上述代碼中的TODO內容
照舊提供一個測試類
import static org
import java
import java
import java
import java
import java
import java
import java
import java
import org
/**
*
* @author 梁棟
* @version
* @since
*/
public class ECCCoderTest {
@Test
public void test() throws Exception {
String inputStr =
byte[] data = inputStr
Map<String
String publicKey = ECCCoder
String privateKey = ECCCoder
System
System
byte[] encodedData = ECCCoder
byte[] decodedData = ECCCoder
String outputStr = new String(decodedData);
System
assertEquals(inputStr
}
}
公鑰:
MEAwEAYHKoZIzj
gAU
私鑰
MDICAQAwEAYHKoZIzj
加密前: abc
解密後: abc
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27542.html