嗨,我想在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建議的。
我使用充氣城堡本身而不是FlexiProvider。在我提到的鏈接中,有關Bouncy Castle實施的答案。你能否以彈性城堡本身的方式告訴我答案?也是我插入正確的jar文件? –
這從上下文來看並不是那麼清楚。不,那個.jar是不正確的,毫無疑問,它是Apache Directory Service .jar(至少如果我沒有記錯的話)。你需要充氣城堡或Spongy城堡.jar的。此外,您需要註冊提供者。 Bouncy Castle的代碼沒有太多的信息,但我很確定安裝是解釋的。 –