2015-04-06 180 views
-4

嗨,我想在Android上实现由anindonesian在Flexiprovider - How to encrypt/de with formatted keypair提供的代码。ECC密钥生成错误

我得到一个NullPointerException为kpg.initialize(brainpoolP160R1);

我是新来的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"); 


    } 

} 

我已插入ApacheDS中,全1.5.5.jar在我的libs文件夹 ..

的误差 java.security.NoSuchAlgorithmException:KeyPairGenerator的ECIES执行未发现 产生的原因:显示java.lang.NullPointerException 在com.example.vinay.myapplication.ECC_page.onCreate(ECC_page.java:47)

也请在代码中指出任何错误,如果可能的话...... JAR文件来樱雪rt是由android studio建议的。

回答

0

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

尝试:

kpg = KeyPairGenerator.getInstance("ECIES"); 

或:

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

针对特定供应商的选择。请注意,椭圆曲线密钥对的生成对于ECDH(密钥协商),ECDSA(数字签名生成)和ECIES(混合加密)来说是同等的。所以,你也可以尝试:

kpg = KeyPairGenerator.getInstance("EC"); 

或:

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

添加的Flexi提供商,而不是充气城堡供应商也可能帮助:

​​

显然也是不正确。

+0

我使用充气城堡本身而不是FlexiProvider。在我提到的链接中,有关Bouncy Castle实施的答案。你能否以弹性城堡本身的方式告诉我答案?也是我插入正确的jar文件? –

+0

这从上下文来看并不是那么清楚。不,那个.jar是不正确的,毫无疑问,它是Apache Directory Service .jar(至少如果我没有记错的话)。你需要充气城堡或Spongy城堡.jar的。此外,您需要注册提供者。 Bouncy Castle的代码没有太多的信息,但我很确定安装是解释的。 –