ECC密钥生成错误

时间:2015-04-06 17:16:24

标签: android cryptography bouncycastle public-key-encryption

您好我正在尝试在Android上的Flexiprovider - How to encrypt/de with formatted keypair中实现anindonesian提供的代码。

我收到了kpg.initialize(brainpoolP160R1)的NullPointerException;

我是Android和密码学的新手,所以任何帮助都会受到赞赏。 在这里,我刚刚创建了一个生成ECC密钥的页面,并对一些数据进行加密/解密,并将其显示在文本框中。

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class ECC_page extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ecc_page);

        Security.addProvider(new BouncyCastleProvider());

        KeyPairGenerator kpg = null;
        try {
            kpg = KeyPairGenerator.getInstance("ECIES", "BC");
        } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
            e.printStackTrace();
        }
        ECGenParameterSpec brainpoolP160R1 = new ECGenParameterSpec("brainpoolP160R1");

        try {
            assert kpg != null;
            kpg.initialize(brainpoolP160R1); //I am getting the error here
        } catch (InvalidAlgorithmParameterException ignored) {


        }

        KeyPair kp = kpg.generateKeyPair();

        PublicKey publicKey = kp.getPublic();
        PrivateKey privateKey = kp.getPrivate();

        byte[] PublicKey = publicKey.getEncoded();
        byte[] PrivateKey = privateKey.getEncoded();

        Cipher c = null;
        try {
            c = Cipher.getInstance("ECIESWithAES/DHAES/NoPadding", "BC");
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException e) {
            e.printStackTrace();
        }

        try {
            c.init(Cipher.ENCRYPT_MODE, publicKey);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

        byte[] cipher = new byte[0];
        try {
            cipher = c.doFinal("This is the message".getBytes());
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        TextView eccencoded = (TextView) findViewById(R.id.eccencoded);
        eccencoded.setText("[ENCODED]:\n" +
                Base64.encodeToString(cipher, Base64.DEFAULT) + "\n");


        try {
            c.init(Cipher.DECRYPT_MODE, privateKey); 
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

       byte[] plaintext = new byte[0];
        try {
            plaintext = c.doFinal(cipher);
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        TextView eccdecoded = (TextView) findViewById(R.id.eccdecoded);
        eccdecoded.setText("[DECODED]:\n" +
                Base64.encodeToString(plaintext, Base64.DEFAULT) + "\n");


    }

}

我在我的libs文件夹中插入了 apacheds-all-1.5.5.jar ..

错误是 java.security.NoSuchAlgorithmException:未找到KeyPairGenerator ECIES实现  引起:java.lang.NullPointerException  在com.example.vinay.myapplication.ECC_page.onCreate(ECC_page.java:47)

如果可能的话,请指出代码中的任何错误...... 要插入的jar文件是由android studio提出的。

1 个答案:

答案 0 :(得分:0)

FlexiProvider不是Bouncy Castle("BC")。

尝试:

kpg = KeyPairGenerator.getInstance("ECIES");

或:

kpg = KeyPairGenerator.getInstance("ECIES", "FlexiEC");

用于特定提供商选择。

请注意,Elliptic Curve密钥对生成对于ECDH(密钥协商),ECDSA(数字签名生成)和ECIES(混合加密)是同义词。所以你也可以尝试:

kpg = KeyPairGenerator.getInstance("EC");

或:

kpg = KeyPairGenerator.getInstance("EC", "FlexiEC");

添加flexi提供程序而不是Bouncy Castle提供程序也可能有所帮助:

Security.addProvider(new BouncyCastleProvider());

显然也是不正确的。