2011-05-18 83 views
4

我想使用ECC來交換長期數據傳輸的會話密鑰。這個密鑰交換應該使用ECC-192bit(curvename:prime192v1)進行加密。這意味着我想實現一個自己的混合加密模型。橢圓曲線密碼學(ECC)與彈性城堡用於非對稱加密

因此我用JAVA充氣城堡。我實現了ECDSA,它工作正常。我實現了AES-128位對稱加密,這也很好。但我不能使用ECC實現簡單的非對稱加密。

所以我的問題:這個非對稱加密可以用彈性城堡來實現嗎?

這是我嘗試使用AsymmetricBlockCipher接口實現ECC加密。但這不起作用。

難道我真的必須實現我自己的ECCEngine嗎?就像RSAEngine(RSACoreEngin)的實現一樣嗎?

這裏是我的代碼:

import org.bouncycastle.jce.interfaces.ECPublicKey; 
import org.bouncycastle.jce.interfaces.ECPrivateKey; 
import org.bouncycastle.crypto.AsymmetricBlockCipher; 
import org.bouncycastle.crypto.InvalidCipherTextException; 
import org.bouncycastle.crypto.engines.AESEngine; 
import org.bouncycastle.crypto.modes.CBCBlockCipher; 
import org.bouncycastle.crypto.params.ECDomainParameters; 
import org.bouncycastle.jce.ECNamedCurveTable; 
import org.bouncycastle.jce.spec.ECParameterSpec; 
import org.bouncycastle.crypto.params.ECPrivateKeyParameters; 
import org.bouncycastle.crypto.params.ECPublicKeyParameters; 
import javax.crypto.Cipher; 



public class ASymCrypto { 

    //cipher init 
    private static AsymmetricBlockCipher bc = null; 
// private static PaddedBufferedBlockCipher cipher = null; 


    //keys and info parameter 
    private static ECPublicKeyParameters publicParam = null; 
    private static ECPrivateKeyParameters privParam = null; 



    /** 
    * Constructor 
    */ 
    ASymCrypto(ECPublicKey pubKey, ECPrivateKey privKey) { 


// //default paddedBufferedBlockCipher with PKCS5/7 padding 
// cipher = new PaddedBufferedBlockCipher(bc); 
     System.out.println("remotePrivateKey: " + privKey + " -(format): "+ privKey.getFormat() + " algo: " + privKey.getAlgorithm()); 
     System.out.println("remotePrivateKey: " + pubKey + " -(format): "+ pubKey.getFormat() + " algo: " + pubKey.getAlgorithm()); 

    //get the key and the EC parameters 
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("prime192v1"); 
    ECDomainParameters domainParam = new ECDomainParameters(
     ecSpec.getCurve() , 
     ecSpec.getG(), 
     ecSpec.getN()); 

    //ECPublicKeyParameters(ECPoint Q, ECDomainParameters params) 
    publicParam = new ECPublicKeyParameters(pubKey.getQ() , domainParam); 
    if(publicParam == null) 
     System.out.println("ERROR: Initializing ASymCrpto failed at ECPublicKeyParam."); 

    //ECPrivateKeyParameters(java.math.BigInteger d, ECDomainParameters params) 
    privParam = new ECPrivateKeyParameters(privKey.getD(), domainParam); 
    if(privParam == null) 
     System.out.println("ERROR: Initializing ASymCrpto failed at ECPrivateKeyParam."); 

    bc = new AsymmetricBlockCipher(new AESEngine()); 
    } 

    /** 
    * encryptEC192 function 
    * @param input: byte array with the message to encrypt 
    * @param output: byte array with the encrypted message using the public key of the partner 
    * @return bool true if successfully encrypted 
    * @throws InvalidCipherTextException 
    */ 
    public boolean encryptEC192(byte[] input, byte[] output) throws InvalidCipherTextException{ 

    if(publicParam == null) 
     System.out.println("ERROR2: Initializing ASymCrpto failed at ECPublicKeyParam."); 
    bc.init(true, publicParam); 

    System.out.println("InputBS: " + bc.getInputBlockSize() + " OutputBS: " + bc.getOutputBlockSize() + "\n"); 
    output = bc.processBlock(input, 0, input.length); 

    return true; 
    } 


    /** 
    * encryptEC192 function 
    * @param input: byte array with the message to encrypt 
    * @param output: byte array with the encrypted message using the public key of the partner 
    * @return bool true if successfully encrypted 
    * @throws InvalidCipherTextException 
    */  
    public boolean decryptEC192(byte[] input, byte[] output) throws InvalidCipherTextException{ 

    if(privParam == null) 
     System.out.println("ERROR2: Initializing ASymCrpto failed at ECPrivateKeyParam."); 
    bc.init(false, privParam); 
    System.out.println("InputBS: " + bc.getInputBlockSize() + " OutputBS: " + bc.getOutputBlockSize() + "\n"); 
    output = bc.processBlock(input, 0, input.length); 

    return true; 
    } 


// INFORMATION PURPOSE ONLY: 
// public byte[] processBlock(byte[] in, 
//        int inOff, 
//        int len) 
//      throws InvalidCipherTextException 
// process the block of len bytes stored in in from offset inOff. 
// Parameters: 
// in - the input data 
// inOff - offset into the in array where the data starts 
// len - the length of the block to be processed. 
// Returns: 
// the resulting byte array of the encryption/decryption process. 
// Throws: 
// InvalidCipherTextException - data decrypts improperly. 
// DataLengthException - the input data is too large for the cipher. 


} 
+0

你確定你已經足夠熟悉Java嗎?你試圖像一個班級那樣對待一個interace,他們不是一回事。你根本沒有任何代碼來執行加密。你確定你對橢圓曲線密碼足夠了解嗎? – 2011-05-18 22:51:57

+0

Thanky迴應,併爲遲到的迴應。 是的,我知道接口和類之間的區別。上面的代碼示例應該只能解釋我的問題,並且充滿錯誤並且不完整。 否則我必須實現自己的EC引擎對我來說真的是一個挑戰,但我認爲不是不可能。 如果我理解橢圓曲線密碼足夠好嗎? 那是我的問題。我當然更喜歡實施的API解決方案。我想知道是否有一個已經存在的? – Manuel 2011-05-24 08:28:26

回答

相關問題