【问题标题】:ECC key generation errorECC 密钥生成错误
【发布时间】:2015-04-06 17:16:24
【问题描述】:

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

我收到 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)

如果可能,请指出代码中的任何错误... android studio建议插入的jar文件..

【问题讨论】:

    标签: android cryptography bouncycastle public-key-encryption


    【解决方案1】:

    FlexiProvider 不是充气城堡 ("BC")。

    试试:

    kpg = KeyPairGenerator.getInstance("ECIES");
    

    或:

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

    用于特定提供商的选择。

    请注意,椭圆曲线密钥对的生成对于 ECDH(密钥协商)、ECDSA(数字签名生成)和 ECIES(混合加密)是相同的。所以你也可以试试:

    kpg = KeyPairGenerator.getInstance("EC");
    

    或:

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

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

    Security.addProvider(new BouncyCastleProvider());
    

    显然也不正确。

    【讨论】:

    • 我使用的是充气城堡本身,而不是 FlexiProvider。在我提到的链接中,有一个 Bouncy Castle 实现的答案。你能告诉我充气城堡本身的答案吗?我插入的jar文件也是正确的吗?
    • 这在上下文中并不是很清楚。不,那个 .jar 不正确,毫无疑问它是 Apache Directory Service .jar 代替(至少如果我没记错的话)。您需要 Bouncy Castle 或 Spongy Castle .jar。此外,您需要注册提供商。 Bouncy Castle 代码没有太多信息,但我很确定安装说明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2012-11-30
    • 2019-12-14
    • 1970-01-01
    • 2020-03-15
    • 2017-03-12
    • 2014-01-04
    相关资源
    最近更新 更多