椭圆曲线密码学(ECC)与弹性城堡用于非对称加密

问题描述:

我想使用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

有公元前这样的解决方案。看例org.bouncycastle.crypto.test.ECIESTest。或者看看http://www.flexiprovider.de/examples/ExampleECIES.html(另一家供应商)。