熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

談談Java加密技術(七)

2022-06-13   來源: 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


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27542.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.