Java – Elliptic Curve Cryptography (ECC) with elastic castle for asymmetric encryption
I want to use ECC to exchange session keys for long-term data transmission Ecc-192 bit (curvename: prime192v1) shall be used to encrypt this key exchange This means that I want to implement my own hybrid encryption model
So I use java to inflate the castle I implemented ECDSA and it works normally I implemented aes - 128 bit symmetric encryption, which is also very good But I can't use ECC to implement simple asymmetric encryption
So my question is: can this asymmetric encryption be implemented with an inflatable castle?
This is my attempt to implement ECC encryption using the asymmetricblockcipher interface But it doesn't work
Do I really have to implement my own eccengine, just like the implementation of rsaeengine (rsacore engine)?
This 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,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,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. }
Solution
BC has such a solution See example org bouncycastle. crypto. test. ECIESTest. Or look here http://www.flexiprovider.de/examples/ExampleECIES.html (another provider)