开发者

Elliptic Curve Cryptography implementation in Java 7

I am trying to implement Elliptic Curve Cryptography (ECC) in java as java 7 provides native provider SunEC which supports Elliptic Curve Cryptography (ECC) But I am always getting an error java.security.InvalidKeyException: Invalid key length: 91 bytes because the Elliptic curve I am creating is not appropriate.

I am using java 7 my task is to use ECC to generate key for encryption and decryption

package com.acc;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.Signature;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECParameterSpec;
import java.security.spec.EllipticCurve;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.DESKeySpec;


public class TestECC {

    public static void main(String args[]) {
        try {
            Provider p[] = Security.getProviders();
            Provider p1 = Security.getProvider("SunEC");
            System.out.println(p1.getName());
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", "SunEC");
            System.out.println(kpg.getAlgorithm());

            Cipher cipher = Cipher.getInstance("DES");
            System.out.println("provider=" + cipher.getProvider());

            ECGenParameterSpec ecsp = new ECGenParameterSpec("sect163r2");

            kpg.initialize(ecsp);
            KeyPair kyp = kpg.genKeyPair();
            PublicKey pubKey = kyp.getPublic();

            PrivateKey privKey = kyp.getPrivate();
            System.out.println(cipher.getProvider());

            cipher.init(Cipher.ENCRYPT_MODE, pubKey);

            String cleartextFile = "cleartext.txt";
            String ciphertextFile = "ciphertextECIES.txt";

            byte[] block = new byte[64];
            FileInputStream fis = new FileInputStream(cleartextFile);
            FileOutputStream fos = new FileOutputStream(ciphertextFile);
            CipherOutputStream cos = new CipherOutputStream(fos, cipher);

            int i;
            while ((i = fis.read(block)) != -1) {
                cos.write(block, 0, i);
            }
            cos.close();

            // Decrypt

            String cleartextAgainFile = "cleartextAgainECIES.txt";

            cipher.init(Cipher.DECRYPT_MODE, privKey, ecsp);

            fis = new FileInputStream(ciphertextFile);
            CipherInputStream cis = new CipherInputStream(fis, cipher);
            fos = new FileOutputStream(cleartextAgainFile);

            while ((i = cis.read(block)) != -1) {
                fos.write(block, 0, i);
            }
            开发者_C百科fos.close();

        } catch (Exception e) {
            System.out.println(e);
        }
    }

}

Output: SunEC EC provider=SunJCE version 1.7 SunJCE version 1.7 java.security.InvalidKeyException: Invalid key length: 91 bytes

Can you please help with ECC curve and Algorithm to use


From what I read, you are trying to:

  • Generate a random key pair using ECC
  • Use the ECC private key as a DES symmetric key for encryption
  • Use the ECC public key as a DES symmetric key for decryption
  • Expect the result to be a round-trip

This will not work -- DES is a symmetric algorithm; it requires that the same 56-bit key be used for encryption and decryption. Throwing an ECC key at it won't magically make it accept a different key for the two operations!


As duskwuff already pointed out you can not mix-up Elliptic curve with DES encryption.

The problem is that the new SunEC provider does only implement Elliptic Curve Diffie-Hellman (ECDH) and Elliptic Curve Digital Signature Algorithm (ECDSA).

The encryption standard using EC would be Elliptic Curve Integrated Encryption Scheme (ECIES) - which is not implemented in Java 7. Therefore you can not use EC for encryption without using an external library.


What you can do is use ECDH to exchange a secret, symmetric key that you can then use for encrypting and decrypting DES.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜