具有用于非对称加密的充气城堡的椭圆曲线密码学 (ECC)

发布于 2024-11-08 12:16:57 字数 4791 浏览 0 评论 0原文

我想使用 ECC 来交换会话密钥以进行长期数据传输。此密钥交换应使用 ECC-192 位进行加密(曲线名称:prime192v1)。这意味着我想实现一个自己的混合加密模型。

因此我在JAVA中使用了充气城堡。我实施了 ECDSA,效果很好。我实现了 AES-128 位对称加密,效果也很好。但我无法使用 ECC 实现简单的非对称加密。

所以我的问题是:这种非对称加密可以用充气城堡实现吗?

这是我尝试使用 AsymmetryBlockCipher 接口实现 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.


}

I want to use ECC in order to exchange a Session Key for a long-term data transmission. This key exchange should be encrypted using ECC-192bit (curvename: prime192v1). That means that i want to implement an own hybrid encryption model.

Therefore i used bouncy castle for JAVA. I implemented ECDSA and it works fine. I implemented the AES-128 bit symmetric encryption and this also works fine. But i cant implement a simple Asymmetric encryption using ECC.

So my question: Can this asymmetric encryption implemented with bouncy castle at all?

This is my try to implement an ECC encryption using the AsymmetricBlockCipher interface. But this doesn't work.

Do I really have to implement my own ECCEngine like there is an implementation for the RSAEngine (RSACoreEngin)?

Here is my code:

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.


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文